473,394 Members | 1,749 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,394 software developers and data experts.

how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?

how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?
Jul 21 '05 #1
7 1409

create a new class which contains these two references as private member.
then you can control the modification as you like.

"Daniel" <so*******************@yahoo.com> дÈëÓʼþ
news:Os**************@TK2MSFTNGP10.phx.gbl...
how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?

Jul 21 '05 #2
Daniel <so*******************@yahoo.com> wrote:
how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?


Please see the responses to your identical question in the C# group.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Simply put: you can't with just the string type. When you modify a string in
..Net, I believe that the original string is *copied* to the new string,
complete with your changes. This means that the string reference changes when
you change your string.

What you CAN do is create a class with one member variable: your string. You
can create as many variables as you like that reference an instance of that
class.

Just do this:
Class SharedString
Public Text as String
End Class

Create a New instance of this class once (dim foo as new SharedString), then
reference it as often as you like. Just remember to use foo.Text to read your
string. (you could also crete a ToString function.)

"Daniel" wrote:
how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?

Jul 21 '05 #4
Tom Wilson <To*******@discussions.microsoft.com> wrote:
Simply put: you can't with just the string type. When you modify a string in
.Net, I believe that the original string is *copied* to the new string,
complete with your changes.
No, you just can't modify a string.
This means that the string reference changes when you change your string.


You can't change the string. You can change the value of your string
reference to a new one with text based on the first one:

tmp = tmp.Replace("a", "b");

for instance, but that's exactly the same as with any other class. It
just so happens that string is immutable.

That may well be what you were trying to say, but it wasn't entirely
clear. (The string reference doesn't change, for instance, unless you
specifically use the return value of the method in question in that
way.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
Hmm.... let's explore this a little. I've worked with C for years, where
there are no such strings, but only arrays of characters. So my perspective
of reference types comes from a pretty good understanding of C and C++
pointers. Hwever, .Net is still something of a mystery to me when it comes to
the internal workings of the compiler and runtime engine.

First, strings are reference types, correct? And any time we type "..." in
the source code, this creates a static string that is compiled in to the data
section of our program (or some CLR analogue of this). Unlike integers or
floats, strings are reference types, meaning that the variable doesn't hold
the value of our data, but rather holds the LOCATION of our data.

If I assign something to a string variable, like this:
A="Test1"
and later: A="Test2"

It's my impression that what you're actually doing is re-assigning the
reference variable "A" to point to the static string "Test1" or "Test2". If
we later said 'B=A', we would be setting B's reference to the same address as
A's reference.

Now, if we "modify" A, like this:
A=A & "Test3"
A now points to a string that has the value "Test2Test3", and B still points
to a string that has the value "Test2".

This is why, to make multiple references to a string, we'd need a wrapper
class. Then we can happily say this:
dim A as New WrapperClass
dim B as WrapperClass

A.Text="test1"
B=A
A.Text="Test2"

B.Text will now be Test2.

Does this make sense?
"Jon Skeet [C# MVP]" wrote:
Tom Wilson <To*******@discussions.microsoft.com> wrote:
Simply put: you can't with just the string type. When you modify a string in
.Net, I believe that the original string is *copied* to the new string,
complete with your changes.


No, you just can't modify a string.
This means that the string reference changes when you change your string.


You can't change the string. You can change the value of your string
reference to a new one with text based on the first one:

tmp = tmp.Replace("a", "b");

for instance, but that's exactly the same as with any other class. It
just so happens that string is immutable.

That may well be what you were trying to say, but it wasn't entirely
clear. (The string reference doesn't change, for instance, unless you
specifically use the return value of the method in question in that
way.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #6
Hmm.... let's explore this a little. I've worked with C for years, where
there are no such strings, but only arrays of characters. So my perspective
of reference types comes from a pretty good understanding of C and C++
pointers. Hwever, .Net is still something of a mystery to me when it comes to
the internal workings of the compiler and runtime engine.

First, strings are reference types, correct? And any time we type "..." in
the source code, this creates a static string that is compiled in to the data
section of our program (or some CLR analogue of this). Unlike integers or
floats, strings are reference types, meaning that the variable doesn't hold
the value of our data, but rather holds the LOCATION of our data.

If I assign something to a string variable, like this:
A="Test1"
and later: A="Test2"

It's my impression that what you're actually doing is re-assigning the
reference variable "A" to point to the static string "Test1" or "Test2". If
we later said 'B=A', we would be setting B's reference to the same address as
A's reference.

Now, if we "modify" A, like this:
A=A & "Test3"
A now points to a string that has the value "Test2Test3", and B still points
to a string that has the value "Test2".

This is why, to make multiple references to a string, we'd need a wrapper
class. Then we can happily say this:
dim A as New WrapperClass
dim B as WrapperClass

A.Text="test1"
B=A
A.Text="Test2"

B.Text will now be Test2.

Does this make sense?
"Jon Skeet [C# MVP]" wrote:
Tom Wilson <To*******@discussions.microsoft.com> wrote:
Simply put: you can't with just the string type. When you modify a string in
.Net, I believe that the original string is *copied* to the new string,
complete with your changes.


No, you just can't modify a string.
This means that the string reference changes when you change your string.


You can't change the string. You can change the value of your string
reference to a new one with text based on the first one:

tmp = tmp.Replace("a", "b");


Jul 21 '05 #7
Tom Wilson <To*******@discussions.microsoft.com> wrote:
Hmm.... let's explore this a little. I've worked with C for years, where
there are no such strings, but only arrays of characters. So my perspective
of reference types comes from a pretty good understanding of C and C++
pointers. Hwever, .Net is still something of a mystery to me when it comes to
the internal workings of the compiler and runtime engine.

First, strings are reference types, correct?
Correct.
And any time we type "..." in
the source code, this creates a static string that is compiled in to the data
section of our program (or some CLR analogue of this). Unlike integers or
floats, strings are reference types, meaning that the variable doesn't hold
the value of our data, but rather holds the LOCATION of our data.
Yes.
If I assign something to a string variable, like this:
A="Test1"
and later: A="Test2"

It's my impression that what you're actually doing is re-assigning the
reference variable "A" to point to the static string "Test1" or "Test2".
The value of A is a reference, first to the string containing the data
"Test1" and then to the string containing the data "Test2", yes.
If we later said 'B=A', we would be setting B's reference to the same
address as A's reference.
Yes.
Now, if we "modify" A, like this:
A=A & "Test3"
That's modifying *A* (it's changing the value of the variable) but it's
not modifying the string.
A now points to a string that has the value "Test2Test3", and B still points
to a string that has the value "Test2".
Yes.
This is why, to make multiple references to a string, we'd need a wrapper
class. Then we can happily say this:
dim A as New WrapperClass
dim B as WrapperClass

A.Text="test1"
B=A
A.Text="Test2"

B.Text will now be Test2.

Does this make sense?


Absolutely. It was only the idea of modifying a string I was objecting
to :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8

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

Similar topics

1
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
10
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
30
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
8
by: zefciu | last post by:
Hello! Where can I find a good explanation when does an interpreter copy the value, and when does it create the reference. I thought I understand it, but I have just typed in following...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
19
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...

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.