Thread: Edit string problem

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    Edit string problem

    Hey!

    Im trying to replace one char in my string with another one. Obviously i don't know what's wrong because then i would fix it. :P W/e, here's my code:

    Code:
    textBox4.Text[Convert.ToInt32(var)] = Convert.ToChar(var);
    The compiler sais:
    Code:
    Error	1	Property or indexer 'string.this[int]' cannot be assigned to -- it is read only	C:\Users\HP-användare\Documents\Visual Studio 2008\Projects\Hangman\Hangman\Form1.cs	132	17	Hangman
    Thank you!

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I've googled it many times, but i can't find the answer. I can give you all the information about the code, just tell me.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Strings are immutable so you can't manually assign individual characters as you can in say C++. Look up the string methods Replace/Remove/Insert as they may be what you seek.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Thanks for your help!

  5. #5
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Code:
            /*########################################*/
            /*########################################*/
            /*########################################*/
            private String String_Replace(String strText, String strFind, String strReplace)
            {
                try
                {
                    int iPos = strText.IndexOf(strFind, System.StringComparison.CurrentCultureIgnoreCase);
                    String strReturn = "";
                    while (iPos != -1)
                    {
                        strReturn += strText.Substring(0, iPos) + strReplace;
                        strText = strText.Substring(iPos + strFind.Length);
                        iPos = strText.IndexOf(strFind);
                    }
                    if (strText.Length > 0) strReturn += strText;
                    return strReturn;
                }
                catch
                {
                    return strText;
                }
            }
            /*########################################*/
            /*########################################*/
            /*########################################*/
    Is that what you need?

    hm, You could I guess split your string into an array of bytes, replace your char there and then put it back together building a string.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Got a question...
    How can i store a word so i can get it from all classes? This doesn't work, but you might understand what i want.

    Code:
    public struct holder
    {
           int io;
    }
    public class save()
    {
          holder save = new holder;
          save.io = 1;
    }
    public class print()
    {
          holder print = new holder;
          if (print.io != 1)
          {
                //true
           }
    }
    Well, the int io should stay at 1 not change. How do i do that?

  7. #7
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    If you need to reach a variable from outside a class, you do it as "public ..." , if not "private ..."

    Code:
    public class MyClass(){
    public string ThisVariableCanBeReached;
    private string ThisVariableCanNOTbeReached;
    }
    So you'll be able to use
    MyClass TEST = new MyClass();
    TEST.ThisVariableCanBeReached = "bla bla";

    If you need something which "doesn't change", try "public const"
    public const string ThisCanNotBeChanged = "Can't touch this!";

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    When i try public const string... i get an error:
    Code:
    Error	1	Member 'Hangman.Form1.holder.word' cannot be accessed with an instance reference; qualify it with a type name instead	C:\Users\HP-användare\Documents\Visual Studio 2008\Projects\Hangman\Hangman\Form1.cs	71	37	Hangman
    Why?

  9. #9
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Show me the code, not that weird long error without a line break in it!

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Well since my computer crashed, it will take a while before... I must recode all!

  11. #11
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Quote Originally Posted by Livijn View Post
    Well since my computer crashed, it will take a while before... I must recode all!
    huh? computer crashed? What does that have to do with your code? Did the drive die?

    Dude, ever heard of BACKUPS ? DVDs? External Drives? Don't tell me you formatted your drive and "lost" everything! hahahha... god damn it, my stomac hurts from the laugh.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 11:52 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 08:50 AM
  5. Edit Control problem
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 06-16-2002, 01:12 AM