By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & 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?

P: n/a
how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
> how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?


Off the top of my head, and from my Java background, You might consider
this:

// I think these are all "by-value" assignemtents
string x = "stuff;"
string y = x;
string z = x;

// I think these are all "by-reference" assignments
String x = "stuff";
String y = x;
String z= x;

but I could be mistaken. Too much transposing between VB, C# & Java lately
as turned my brain into chinese egg drop soup.
--
Peace & happy computing,

Mike Labosh, MCSD
"I have no choice but to believe in free will."
Nov 16 '05 #2

P: n/a
Daniel,
Short answer, you don't.

Strings are immutable, which means you never "change" their value, you
simply assign a new instance of the string to a variable.

Long answer: consider creating a String Holder class, that is mutable that
has a reference to the actual string. Every place you want a reference to
the "one string" you actually have a reference to a String Holder, when ever
you want to "change" the value of the String, you actually change the string
the String Holder is referencing...

Hope this helps
Jay

"Daniel" <so*******************@yahoo.com> wrote in message
news:uI**************@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?

Nov 16 '05 #3

P: n/a
You can use the string interning pool. All string constants are interned at
compile-time by default but you have runtime access to this facility if you
want to put other strings in there:

string s1 = String.Intern("foo");
string s2 = String.Intern("foo");

Both references point to the same "foo" in memory, rather than to two
different "foo"s. If one of them is reassigned later, that is properly
managed:

s2 = String.Intern("bar");
string s3 = String.Intern("foo");

Now s1 & s3 point to the same "foo", s2 points to bar.

This can be a tremendous memory saver. For example if you load 1,000 rows
from, say, a comma-delimited file into memory, with these fields:

Name
Address
City
State
Zip
VehicleYear
VehicleMake
VehicleModel

You could intern the last 6 fields and possibly save 90% or more of the
memory consumption for those values since there are likely to be relatively
few unique values in those fields.

Warning: String interning is considerably slower than simple assignment. I
haven't formally benchmarked it but I think the penalty is fairly stiff.

--Bob

"Daniel" <so*******************@yahoo.com> wrote in message
news:uI**************@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?

Nov 16 '05 #4

P: n/a
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?


String values don't change. The value of a variable can change, to
refer to another string, but that's slightly different.

It sounds like you might want a container class, eg:

public class StringHolder
{
public StringHolder (string value)
{
this.value = value;
}

public string Value
{
get
{
return value;
}

set
{
this.value = value;
}
}
}

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

P: n/a
Bob Grommes <bo*@bobgrommes.com> wrote:
You can use the string interning pool. All string constants are interned at
compile-time by default but you have runtime access to this facility if you
want to put other strings in there:

string s1 = String.Intern("foo");
string s2 = String.Intern("foo");

Both references point to the same "foo" in memory, rather than to two
different "foo"s. If one of them is reassigned later, that is properly
managed:

s2 = String.Intern("bar");
string s3 = String.Intern("foo");

Now s1 & s3 point to the same "foo", s2 points to bar.

This can be a tremendous memory saver. For example if you load 1,000 rows
from, say, a comma-delimited file into memory, with these fields:

Name
Address
City
State
Zip
VehicleYear
VehicleMake
VehicleModel

You could intern the last 6 fields and possibly save 90% or more of the
memory consumption for those values since there are likely to be relatively
few unique values in those fields.

Warning: String interning is considerably slower than simple assignment. I
haven't formally benchmarked it but I think the penalty is fairly stiff.


I think this is actually looking at the opposite problem from what the
OP is after. I believe he wants to do:

string s1 = "hello";
string s2 = s1;

s1 = "bar";
// I think the OP wants s2 to be bar here

However, just to talk about interning for a second - there's a problem
with this, which is that the intern pool won't be garbage collected
until the app domain is (IIRC).

A simpler solution is to just use a hashtable to do the same kind of
thing:

Hashtable table = new Hashtable();
string PseudoIntern (string x)
{
string ret = table[x] as string;
if (ret != null)
{
return ret;
}
else
{
table[x] = x;
return x;
}
}

This may well be faster than interning, too, as it doesn't require the
same kind of thread safety that the intern pool does. (Depending on
your usage pattern, of course...)

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

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

<snipped>

John - hopefully not a terribly dumb question - surely, if you want to save
room
and a value is likely to be repeated very often, you can use a code list,
provided
of course that the code value is less memory consuming than the value
returned
by the code list?

Nov 16 '05 #7

P: n/a
Zach <no*@this.address> wrote:
John - hopefully not a terribly dumb question - surely, if you want
to save room and a value is likely to be repeated very often, you can
use a code list, provided of course that the code value is less
memory consuming than the value returned by the code list?


I'm not entirely sure what you mean - could you clarify your question?

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

P: n/a
Jon,
However, just to talk about interning for a second - there's a problem
with this, which is that the intern pool won't be garbage collected
until the app domain is (IIRC).

A simpler solution is to just use a hashtable to do the same kind of
thing: Or an XmlNameTable if you are working with XML.

Hope this helps
Jay
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Bob Grommes <bo*@bobgrommes.com> wrote:
You can use the string interning pool. All string constants are interned
at
compile-time by default but you have runtime access to this facility if
you
want to put other strings in there:

string s1 = String.Intern("foo");
string s2 = String.Intern("foo");

Both references point to the same "foo" in memory, rather than to two
different "foo"s. If one of them is reassigned later, that is properly
managed:

s2 = String.Intern("bar");
string s3 = String.Intern("foo");

Now s1 & s3 point to the same "foo", s2 points to bar.

This can be a tremendous memory saver. For example if you load 1,000
rows
from, say, a comma-delimited file into memory, with these fields:

Name
Address
City
State
Zip
VehicleYear
VehicleMake
VehicleModel

You could intern the last 6 fields and possibly save 90% or more of the
memory consumption for those values since there are likely to be
relatively
few unique values in those fields.

Warning: String interning is considerably slower than simple assignment.
I
haven't formally benchmarked it but I think the penalty is fairly stiff.


I think this is actually looking at the opposite problem from what the
OP is after. I believe he wants to do:

string s1 = "hello";
string s2 = s1;

s1 = "bar";
// I think the OP wants s2 to be bar here

However, just to talk about interning for a second - there's a problem
with this, which is that the intern pool won't be garbage collected
until the app domain is (IIRC).

A simpler solution is to just use a hashtable to do the same kind of
thing:

Hashtable table = new Hashtable();
string PseudoIntern (string x)
{
string ret = table[x] as string;
if (ret != null)
{
return ret;
}
else
{
table[x] = x;
return x;
}
}

This may well be faster than interning, too, as it doesn't require the
same kind of thread safety that the intern pool does. (Depending on
your usage pattern, of course...)

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

Nov 16 '05 #9

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zach <no*@this.address> wrote:
John - hopefully not a terribly dumb question - surely, if you want
to save room and a value is likely to be repeated very often, you can
use a code list, provided of course that the code value is less
memory consuming than the value returned by the code list?


I'm not entirely sure what you mean - could you clarify your question?


I understood that the problem was that a certain value might be repeated
many times in different relationships and that you were providing a medicine
to keep memory usage down to a minimum.

Thag is why I thouht, rather than filing Monty Ville, you could file 12,
and have a conversion table to say that 12 translates into Monty Ville.
This is the point I was attempting to make, and wondered whether
I was missing the essence of what you were explaining. Obviously
my example implies that there are lot of, albeit limited number of,
names of towns.
Nov 16 '05 #10

P: n/a
Zach <no*@this.address> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zach <no*@this.address> wrote:
John - hopefully not a terribly dumb question - surely, if you want
to save room and a value is likely to be repeated very often, you can
use a code list, provided of course that the code value is less
memory consuming than the value returned by the code list?


I'm not entirely sure what you mean - could you clarify your question?


I understood that the problem was that a certain value might be repeated
many times in different relationships and that you were providing a medicine
to keep memory usage down to a minimum.

Thag is why I thouht, rather than filing Monty Ville, you could file 12,
and have a conversion table to say that 12 translates into Monty Ville.
This is the point I was attempting to make, and wondered whether
I was missing the essence of what you were explaining. Obviously
my example implies that there are lot of, albeit limited number of,
names of towns.


Given that you've got to store "Monty Ville" somewhere, you might as
well just have several references to the same string. If you know
you'll only have 256 town names, you could store each value as a byte
index, of course, and likewise if you have only 65536 names you could
store each value as a ushort. It's typically not going to be worth
doing that though.

The code I gave will make sure that only one copy of the town name is
used (with other copies just being temporary). Every reference to
"Monty Ville" will be a reference to the same string object. References
are pretty cheap in memory - four bytes in the current CLR
implementation.

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

This discussion thread is closed

Replies have been disabled for this discussion.