468,765 Members | 955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

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 1255

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by jeremygetsmail | last post: by
6 posts views Thread by scottyman | last post: by
8 posts views Thread by zefciu | last post: by
19 posts views Thread by zzw8206262001 | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.