473,796 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1491
"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*******@free net.de> wrote in message
news:uy******** ******@TK2MSFTN GP12.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******** *******@TK2MSFT NGP14.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(sa ve Is st, "save is st")
Trace.WriteLine (st)
Mid(st, 2, 2) = "CC"
Debug.Assert(sa ve 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******** *******@TK2MSFT NGP14.phx.gbl.. .
| "Armin Zingler" <az*******@free net.de> wrote in message
| news:uy******** ******@TK2MSFTN GP12.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******** ******@tk2msftn gp13.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
6756
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... this number is always changing as I have hundreds of thousand of pages of text on my site. Problem: - in my opinion this just not only look weird, but the variable "n" (number)
5
31181
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 will split myString up using the delimiter of 1 space so that
9
8005
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., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
10
8185
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
2
4785
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
4325
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 substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
2
3179
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 a tasty burger"
15
50264
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
3071
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 spaces, but not in the inner string(\"and a inner string\\\"). Will anyone please tell me how to do this?
8
4744
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 add the unique words in the string to a map. Eg:
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.