|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
simple questionI know that string is a reference type. I have the following code: Dim s As String Dim s2 As String s = "AAAAA" s2 = s MsgBox(s2) s = "BBBBB" MsgBox(s2) I am not sure I understand why the second MSGBOX still shows "AAAAA" since the two strings point to the same memory location. Please help. Carly Visual Basic treats strings as a special case among the reference types.
The statement "s2 = s" makes a distinct immutable copy of the original string. ----- Tim Patrick - www.timaki.com Start-to-Finish Visual Basic 2005 Show quoteHide quote > Hi there, > > I know that string is a reference type. > > I have the following code: > > Dim s As String > Dim s2 As String > s = "AAAAA" > s2 = s > MsgBox(s2) > s = "BBBBB" > MsgBox(s2) > I am not sure I understand why the second MSGBOX still shows "AAAAA" > since the two strings point to the same memory location. > > Please help. > > Carly > Thank you Tim
Tim Patrick wrote: Show quoteHide quote > Visual Basic treats strings as a special case among the reference types. > The statement "s2 = s" makes a distinct immutable copy of the original string. > > ----- > Tim Patrick - www.timaki.com > Start-to-Finish Visual Basic 2005 > > > Hi there, > > > > I know that string is a reference type. > > > > I have the following code: > > > > Dim s As String > > Dim s2 As String > > s = "AAAAA" > > s2 = s > > MsgBox(s2) > > s = "BBBBB" > > MsgBox(s2) > > I am not sure I understand why the second MSGBOX still shows "AAAAA" > > since the two strings point to the same memory location. > > > > Please help. > > > > Carly > > I thought when you do this:
Dim s as String Dim s2 as String s = "abcde" s2 = s Console.WriteLine(s is s2) --> True that the compiler sets s2 and s to point to the same place in memory, and it's only when you assign something different to one of them that a new reference to a different string in memory is created. And in fact, if you set two strings to the same value, like this: s = "abcdefgh" s2 = "abcd" & "efgh" Console.WriteLine(s is s2) --> True I thought that the compiler will recognize that they are the same and allocate only one block of memory for the string, with the two variables pointing to it. Did I learn something incorrectly? Thanks, Robin S. -------------------------- Show quoteHide quote "Tim Patrick" <inva***@invalid.com.invalid> wrote in message news:e3b4697634c78c8e81f144fd5a2@newsgroups.comcast.net... > Visual Basic treats strings as a special case among the reference types. > The statement "s2 = s" makes a distinct immutable copy of the original > string. > > ----- > Tim Patrick - www.timaki.com > Start-to-Finish Visual Basic 2005 > >> Hi there, >> >> I know that string is a reference type. >> >> I have the following code: >> >> Dim s As String >> Dim s2 As String >> s = "AAAAA" >> s2 = s >> MsgBox(s2) >> s = "BBBBB" >> MsgBox(s2) >> I am not sure I understand why the second MSGBOX still shows "AAAAA" >> since the two strings point to the same memory location. >> >> Please help. >> >> Carly >> > > On 2006-12-08, RobinS <RobinS@NoSpam.yah.none> wrote:
Show quoteHide quote > I thought when you do this: Robin,> > Dim s as String > Dim s2 as String > s = "abcde" > s2 = s > Console.WriteLine(s is s2) --> True > > that the compiler sets s2 and s to point to the same place in > memory, and it's only when you assign something different to one > of them that a new reference to a different string in memory > is created. > > And in fact, if you set two strings to the same value, like this: > > s = "abcdefgh" > s2 = "abcd" & "efgh" > Console.WriteLine(s is s2) --> True > > I thought that the compiler will recognize that they are the > same and allocate only one block of memory for the string, > with the two variables pointing to it. > > Did I learn something incorrectly? > > Thanks, > Robin S. You are correct - it's called interning. The runtime treats strings differently then other reference types. Basically, the compiler builds a table of all the litteral strings in your app and then the runtime will reuse those addresses rather then allocate new string space each time. -- Tom Shelton
Show quote
Hide quote
"Tom Shelton" <tom_shel***@comcastXXXXXXX.net> wrote in message Thanks. It's nice to know I can remember something that I thinknews:2JOdnYVQTdzGrufYnZ2dnUVZ_qvinZ2d@comcast.com... > On 2006-12-08, RobinS <RobinS@NoSpam.yah.none> wrote: >> I thought when you do this: >> >> Dim s as String >> Dim s2 as String >> s = "abcde" >> s2 = s >> Console.WriteLine(s is s2) --> True >> >> that the compiler sets s2 and s to point to the same place in >> memory, and it's only when you assign something different to one >> of them that a new reference to a different string in memory >> is created. >> >> And in fact, if you set two strings to the same value, like this: >> >> s = "abcdefgh" >> s2 = "abcd" & "efgh" >> Console.WriteLine(s is s2) --> True >> >> I thought that the compiler will recognize that they are the >> same and allocate only one block of memory for the string, >> with the two variables pointing to it. >> >> Did I learn something incorrectly? >> >> Thanks, >> Robin S. > > Robin, > > You are correct - it's called interning. The runtime treats strings > differently then other reference types. Basically, the compiler builds a > table of all the litteral strings in your app and then the runtime will > reuse > those addresses rather then allocate new string space each time. > > -- > Tom Shelton is sort of obscure. :-) Robin S.
DataTable FillSchema error - Pulling my hair out!
Populating combox from dataset Is this a bug I see before me, or an incomplete understanding of scope? Shared textfile...threading Namespace for stdole? Look at this debugging output about TreeViews Drop shadow under image Re: Turn off deprecated warnings? Re: Turn off deprecated warnings? Re: Turn off deprecated warnings? |
|||||||||||||||||||||||