473,395 Members | 1,532 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,395 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?
Nov 16 '05 #1
10 1601
> 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
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
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
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
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

"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
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
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
"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
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 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: 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?
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;...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.