|
-
Aug 24th, 2004, 02:13 PM
#1
Thread Starter
Hyperactive Member
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.
-
Aug 25th, 2004, 02:20 AM
#2
Sleep mode
I don't understand your problem with strings but read about hashtables .
-
Aug 25th, 2004, 11:37 AM
#3
Thread Starter
Hyperactive Member
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".
-
Aug 25th, 2004, 05:14 PM
#4
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|