473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

String in VB

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.
--
Regards.
_____________________________________
Anatoli Trifonov
Software Developer & Consultant
Nov 21 '05 #1
6 1476
"AT" <an*****@avwork.com> schrieb
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

Nov 21 '05 #2
AT
"Armin Zingler" <az*******@freenet.de> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
"AT" <an*****@avwork.com> schrieb
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.
Nov 21 '05 #3
"AT" <an*****@avwork.com> schrieb im Newsbeitrag
news:%2***************@TK2MSFTNGP14.phx.gbl...

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.
If Mid returned new String then
Mid(st, 2, 2) = "CC"
Trace.WriteLine(st)

will not result in
ACCA


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

Nov 21 '05 #4
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

"AT" <an*****@avwork.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
| "Armin Zingler" <az*******@freenet.de> wrote in message
| news:uy**************@TK2MSFTNGP12.phx.gbl...
| > "AT" <an*****@avwork.com> schrieb
| >> 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.
|
|
Nov 21 '05 #5
AT
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
Nov 21 '05 #6
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/de...l/vastmMid.asp

Hope this helps
Jay

"AT" <an*****@avwork.com> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
|
| >> 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
|
|
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.