how to make two references to one string that stay refered to the same string reguardless of the cha

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?
 
create a new class which contains these two references as private member.
then you can control the modification as you like.
 
Daniel said:
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.
 
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.)
 
Tom Wilson said:
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.)
 
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?
 
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?
 
Tom Wilson said:
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 :)
 
Back
Top