Results 1 to 4 of 4

Thread: variable reference in array... strings?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Question variable reference in array... strings?

    Hi,
    I am trying to access a variable I have stored in ListArray. If I use classes, like this:
    Code:
     MyClass mc = new MyClass();
     MyListArray.Add("mc",mc);
     mc.MyValue = 4;
    everything works, and ((MyClass)MyListArray["mc"]).MyValue is 4.

    However, if I store string (or eg. TimeSpan) like this, (object MyString = "") I can not modify it without creating new copy... and MyString contains something else than (string)MyArray["MyString"]

    How to access my string variable by name?

    Do I have to create my own class for every variable I cannot modify without creating copy?
    Last edited by Jhd.Honza; Aug 24th, 2004 at 02:18 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I don't understand your problem with strings but read about hashtables .

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Ok, let's simplify it a little bit:

    Code:
     Bitmap MyBitmap = new Bitmap(100,50);
     object MyVariableHolder = MyBitmap;
     MyBitmap.Height = 200;
    
     Console.WriteLine(MyBitmap.Height);
     Console.WriteLine(((Bitmap)MyVariableHolder).Height);
    
     results in:
       200
       200
    However
    Code:
     string MyString = "old string";
     object MyVariableHolder = MyString;
     MyString = "new string";
    
     Console.WriteLine(MyString);
     Console.WriteLine((string)MyVariableHolder);
    
     results in:
       new string
       old string
    I understand what causes this, but I don't know, how to modify MyString to force MyVariableHolder to keep the reference and thus, return "new string".

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    String obj is immutable because its value cannot be modified once it has been created .
    You might need to read up these links :
    http://msdn.microsoft.com/library/de...classtopic.asp
    http://www.developer.com/net/net/article.php/3304901 http://www.dotnetspider.com/Technolo...x?SampleId=249
    The last two links talk about stringbuilder which would be helpful in your case .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width