Home All Groups Group Topic Archive Search About
Author
7 Dec 2006 9:20 PM
Carly
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

Author
7 Dec 2006 9:59 PM
Tim Patrick
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
>
Author
7 Dec 2006 11:25 PM
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
> >
Author
8 Dec 2006 5:56 AM
RobinS
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
>>
>
>
Author
9 Dec 2006 3:54 AM
Tom Shelton
On 2006-12-08, RobinS <RobinS@NoSpam.yah.none> wrote:
Show quoteHide quote
> 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
Author
9 Dec 2006 7:12 AM
RobinS
Show quote Hide quote
"Tom Shelton" <tom_shel***@comcastXXXXXXX.net> wrote in message
news: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

Thanks. It's nice to know I can remember something that I think
is sort of obscure. :-)

Robin S.