Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Daniel
Guest
 
Posts: n/a
#1: Nov 16 '05
how to make two references to one string that stay refered to the same
string reguardless of the changing value in the string?



Mike Labosh
Guest
 
Posts: n/a
#2: Nov 16 '05

re: 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[color=blue]
> string reguardless of the changing value in the string?[/color]

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."


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#3: Nov 16 '05

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


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" <softwareengineer98037@yahoo.com> wrote in message
news:uIpdIIUwEHA.2568@TK2MSFTNGP10.phx.gbl...[color=blue]
> how to make two references to one string that stay refered to the same
> string reguardless of the changing value in the string?
>
>[/color]


Bob Grommes
Guest
 
Posts: n/a
#4: Nov 16 '05

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


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" <softwareengineer98037@yahoo.com> wrote in message
news:uIpdIIUwEHA.2568@TK2MSFTNGP10.phx.gbl...[color=blue]
> how to make two references to one string that stay refered to the same
> string reguardless of the changing value in the string?[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 16 '05

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


Daniel <softwareengineer98037@yahoo.com> wrote:[color=blue]
> how to make two references to one string that stay refered to the same
> string reguardless of the changing value in the string?[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#6: Nov 16 '05

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


Bob Grommes <bob@bobgrommes.com> wrote:[color=blue]
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Zach
Guest
 
Posts: n/a
#7: Nov 16 '05

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



"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1bf2a3ce34034e3898b86b@msnews.microsoft.c om...[color=blue]
>[/color]
<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?





Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#8: Nov 16 '05

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


Zach <not@this.address> wrote:[color=blue]
> 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?[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#9: Nov 16 '05

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


Jon,[color=blue]
> 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:[/color]
Or an XmlNameTable if you are working with XML.

Hope this helps
Jay


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1bf2a3ce34034e3898b86b@msnews.microsoft.c om...[color=blue]
> Bob Grommes <bob@bobgrommes.com> wrote:[color=green]
>> 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.[/color]
>
> 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 - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Zach
Guest
 
Posts: n/a
#10: Nov 16 '05

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


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1bf2f97a8da9b08b98b870@msnews.microsoft.c om...[color=blue]
> Zach <not@this.address> wrote:[color=green]
> > 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?[/color]
>
> I'm not entirely sure what you mean - could you clarify your question?[/color]

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.


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#11: Nov 16 '05

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


Zach <not@this.address> wrote:[color=blue]
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> news:MPG.1bf2f97a8da9b08b98b870@msnews.microsoft.c om...[color=green]
> > Zach <not@this.address> wrote:[color=darkred]
> > > 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?[/color]
> >
> > I'm not entirely sure what you mean - could you clarify your question?[/color]
>
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread