String in VB

A

AT

Can someone please explain me the following:

quote from documentation
"Unlike other intrinsic data types, String is a reference type. When a
variable of reference type is passed as an argument to a function or
subroutine, a reference to the memory address where the data is stored is
passed instead of the actual value of the string. "

Code

Module Module1
Sub Main()
Dim s As String = "AAAA"
Mid(s, 2, 2) = "BB"
Trace.WriteLine(s)
Test(s)
Trace.WriteLine(s)
End Sub
Sub Test(ByVal st As String)
Trace.WriteLine(st)
Mid(st, 2, 2) = "CC"
Trace.WriteLine(st)
End Sub
End Module

Result of trace
ABBA
ABBA
ACCA
ABBA

If String is a reference type then the copy of a pointer is passed into the
sub
Mid supposed to replace a peace of memory occupied by this string
and it does it in Sub Main
Within the Sub Test the string part is replaced too
After Sub exits string is restored
Why is it restored?
Does it mean that there was actually a copy of a string created somewhere?
Supposed to be only a pointer copy, not a string copy.
 
A

Armin Zingler

AT said:
Can someone please explain me the following:

quote from documentation
"Unlike other intrinsic data types, String is a reference type. When
a variable of reference type is passed as an argument to a function
or subroutine, a reference to the memory address where the data is
stored is passed instead of the actual value of the string. "

Code

Module Module1
Sub Main()
Dim s As String = "AAAA"
Mid(s, 2, 2) = "BB"
Trace.WriteLine(s)
Test(s)
Trace.WriteLine(s)
End Sub
Sub Test(ByVal st As String)
Trace.WriteLine(st)
Mid(st, 2, 2) = "CC"
Trace.WriteLine(st)
End Sub
End Module

Result of trace
ABBA
ABBA
ACCA
ABBA

If String is a reference type then the copy of a pointer is passed
into the sub
Mid supposed to replace a peace of memory occupied by this string
and it does it in Sub Main
Within the Sub Test the string part is replaced too
After Sub exits string is restored
Why is it restored?
Does it mean that there was actually a copy of a string created
somewhere? Supposed to be only a pointer copy, not a string copy.


Strings are immutable. Therefore the Mid statement returns a new string
containing the new content. As the String is passed ByVal to Sub Test, the
original variable s points still to the old string.


Armin
 
A

AT

Armin Zingler said:
Strings are immutable. Therefore the Mid statement returns a new string
containing the new content. As the String is passed ByVal to Sub Test, the
original variable s points still to the old string.


Armin

If Mid returned new String then
Mid(st, 2, 2) = "CC"
Trace.WriteLine(st) will not result in
ACCA
Mid then would not not have changed the st variable
unless the new string is created and st is assigned a new instance within
the Mid
but I do not understand how this is possible since MID does not get "CC" as
one of its parameters.
 
A

Armin Zingler

AT said:
If Mid returned new String then
Why?

Mid then would not not have changed the st variable
unless the new string is created and st is assigned a new instance within
the Mid
but I do not understand how this is possible since MID does not get "CC"
as one of its parameters.

?
"CC" is the value you assign. This is a special VB.Net syntax for historic
reasons (see <F1>). As you see, this is not the usual syntax. How can a
function be the destination of an assignment? Well it's not a function,
it's... it's... a "VB special". It's internally translated to a usual
procedure call where "CC" is one of the arguments.

I would never use the Mid statement anymore because it pretends to change
the content of the string.


Armin
 
J

Jay B. Harlow [MVP - Outlook]

AT,
| Mid then would not not have changed the st variable
Mid is a statement, it replaced the st variable with a new string.

Try the following Test function:

Sub Test(ByVal st As String)
Dim save As String = st
Debug.Assert(save Is st, "save is st")
Trace.WriteLine(st)
Mid(st, 2, 2) = "CC"
Debug.Assert(save Is st, "save is st")
Trace.WriteLine(st)
End Sub


The Debug.Assert function uses the Is operator to verify that the strings
are the same instances, rather then the same values. Of course the instances
changed as the value changed and as you Strings are immutable.

Hope this helps
Jay

| | >> Can someone please explain me the following:
| >>
| >> quote from documentation
| >> "Unlike other intrinsic data types, String is a reference type. When
| >> a variable of reference type is passed as an argument to a function
| >> or subroutine, a reference to the memory address where the data is
| >> stored is passed instead of the actual value of the string. "
| >>
| >> Code
| >>
| >> Module Module1
| >> Sub Main()
| >> Dim s As String = "AAAA"
| >> Mid(s, 2, 2) = "BB"
| >> Trace.WriteLine(s)
| >> Test(s)
| >> Trace.WriteLine(s)
| >> End Sub
| >> Sub Test(ByVal st As String)
| >> Trace.WriteLine(st)
| >> Mid(st, 2, 2) = "CC"
| >> Trace.WriteLine(st)
| >> End Sub
| >> End Module
| >>
| >> Result of trace
| >> ABBA
| >> ABBA
| >> ACCA
| >> ABBA
| >>
| >> If String is a reference type then the copy of a pointer is passed
| >> into the sub
| >> Mid supposed to replace a peace of memory occupied by this string
| >> and it does it in Sub Main
| >> Within the Sub Test the string part is replaced too
| >> After Sub exits string is restored
| >> Why is it restored?
| >> Does it mean that there was actually a copy of a string created
| >> somewhere? Supposed to be only a pointer copy, not a string copy.
| >
| >
| > Strings are immutable. Therefore the Mid statement returns a new string
| > containing the new content. As the String is passed ByVal to Sub Test,
the
| > original variable s points still to the old string.
| >
| >
| > Armin
| >
|
| If Mid returned new String then
| > Mid(st, 2, 2) = "CC"
| > Trace.WriteLine(st)
| will not result in
| > ACCA
| Mid then would not not have changed the st variable
| unless the new string is created and st is assigned a new instance within
| the Mid
| but I do not understand how this is possible since MID does not get "CC"
as
| one of its parameters.
|
|
 
A

AT

?
"CC" is the value you assign. This is a special VB.Net syntax for historic
reasons (see <F1>). As you see, this is not the usual syntax. How can a
function be the destination of an assignment? Well it's not a function,
it's... it's... a "VB special". It's internally translated to a usual
procedure call where "CC" is one of the arguments.

I would never use the Mid statement anymore because it pretends to change
the content of the string.


Armin

Any references to documentation where this is written?
Please, it is important for me

Thanks
 
J

Jay B. Harlow [MVP - Outlook]

AT,
| Any references to documentation where this is written?
| Please, it is important for me
Yes, Armin told you where:

| > This is a special VB.Net syntax for historic
| > reasons (see <F1>).

<F1> means place your cursor on the Mid statement & press the F1 key on your
keyboard.

Alternatively you can find info on the Mid statement at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmMid.asp

Hope this helps
Jay

|
| >> Mid then would not not have changed the st variable
| >> unless the new string is created and st is assigned a new instance
within
| >> the Mid
| >> but I do not understand how this is possible since MID does not get
"CC"
| >> as one of its parameters.
| >
| > ?
| > "CC" is the value you assign. This is a special VB.Net syntax for
historic
| > reasons (see <F1>). As you see, this is not the usual syntax. How can a
| > function be the destination of an assignment? Well it's not a function,
| > it's... it's... a "VB special". It's internally translated to a usual
| > procedure call where "CC" is one of the arguments.
| >
| > I would never use the Mid statement anymore because it pretends to
change
| > the content of the string.
| >
| >
| > Armin
|
| Any references to documentation where this is written?
| Please, it is important for me
|
| Thanks
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top