String Object Referrence
-
This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:
Dim a, b as string
a = b.Clone()
b = "123"I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC
The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
Get your facts first, and then you can distort them as much as you please Mark Twain -
This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:
Dim a, b as string
a = b.Clone()
b = "123"I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC
The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
Get your facts first, and then you can distort them as much as you please Mark TwainNotorious SMC wrote: Dim a, b as string a = b.Clone() b = "123" are you sure this is the code ? or this :
Dim a, b as string b = "123" a = b.Clone()
-
This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:
Dim a, b as string
a = b.Clone()
b = "123"I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC
The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
Get your facts first, and then you can distort them as much as you please Mark TwainTry creating a class with a string property.
α.γεεκ
Fortune passes everywhere.
Duke Leto Atreides -
This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:
Dim a, b as string
a = b.Clone()
b = "123"I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC
The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
Get your facts first, and then you can distort them as much as you please Mark TwainI know what your thinking with Clone on a string. Your getting a pointer that another string variable can be set with so two variables can point to the same memory location. You right, that's how it works! BUT! What you don't know is that in the .NET framework, STRINGS ARE IMMUTABLE. That means that once a string is created, you CANNOT modify it at all! How is this possible? You can do Search and Replace and change the value of, in your example, 'b'! Your right, you can! What you don't see is that when you change the value of 'b', your actually creating another string in memory and changing the 'b' pointer to look at the new string. 'a' is still looking at the old one and will NOT follow 'b' around. The same is true for replacing a character in the string. You are actually creating a new string (note that 'String .Replace(char,char)' actually returns a String!) and dumping the old one! The code you wrote is correct, the functionality you want is impossible in .NET, unless you want to go thru the hassle of writing your own String Class.
-
I know what your thinking with Clone on a string. Your getting a pointer that another string variable can be set with so two variables can point to the same memory location. You right, that's how it works! BUT! What you don't know is that in the .NET framework, STRINGS ARE IMMUTABLE. That means that once a string is created, you CANNOT modify it at all! How is this possible? You can do Search and Replace and change the value of, in your example, 'b'! Your right, you can! What you don't see is that when you change the value of 'b', your actually creating another string in memory and changing the 'b' pointer to look at the new string. 'a' is still looking at the old one and will NOT follow 'b' around. The same is true for replacing a character in the string. You are actually creating a new string (note that 'String .Replace(char,char)' actually returns a String!) and dumping the old one! The code you wrote is correct, the functionality you want is impossible in .NET, unless you want to go thru the hassle of writing your own String Class.
Yep, that's exactly what I wanted to do, though i figured out the string immutability thing after countless reference books later. :-O But thanks for the reply. This limitation of .net makes me long for those good old C++ days :) Notorious SMC
The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
Get your facts first, and then you can distort them as much as you please Mark Twain