473,395 Members | 1,471 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.

Important refs question

Hi all,

When executing the following, is it a copy or a reference thats copied to
the array?

SqlCommand cmd = new SqlCommand("w", x);
myArrayList.Add(cmd); // A reference
or a copy?
The reason i ask is that I then want to go on and use the same variable name
to make a new cmd and then copy that to the array as well

eg:

SqlCommand cmd = new SqlCommand("y", z);
myArrayList.Add(cmd); // A reference
or a copy?
Is this possible? It does actually seem to work but I don't think it should.

Should I make a copy of the object before putting it in the array list

Many thanks

Simon
Jul 21 '05 #1
9 1470
Hi, Simon

in this case it is reference.
General rule is that all non-value types are references and value types are
exactly what they are - values.

So it is absolutely safe to use code like this:

for (...) {
SqlCommand cmd = new SqlCommand(...);
myList.Add(cmd);
}

You can get more information at
http://msdn.microsoft.com/library/de...rencetypes.asp

HTH
Alex

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
Hi all,

When executing the following, is it a copy or a reference thats copied to
the array?

SqlCommand cmd = new SqlCommand("w", x);
myArrayList.Add(cmd); // A reference or a copy?
The reason i ask is that I then want to go on and use the same variable name to make a new cmd and then copy that to the array as well

eg:

SqlCommand cmd = new SqlCommand("y", z);
myArrayList.Add(cmd); // A reference or a copy?
Is this possible? It does actually seem to work but I don't think it should.
Should I make a copy of the object before putting it in the array list

Many thanks

Simon

Jul 21 '05 #2
Hi Alex,

Thanks for your reply.

I would have thought that them being references would cause a problem.

For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case

Thanks again

Simon
Jul 21 '05 #3
Simon Harvey <si**********@the-web-works.co.uk> wrote:
When executing the following, is it a copy or a reference thats copied to
the array?

SqlCommand cmd = new SqlCommand("w", x);
myArrayList.Add(cmd); // A reference
or a copy?


It's a copy of the reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for info on
parameter passing and reference/value types.

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

this is not the case - you can think about references as of variables, which
store addresses of objects. So, changing reference by assigning new value
(new object instance address) you might lose old one. But in your case old
reference is kept in array list as item, so while array list item exists
corresponding reference exists too. At the same time cmd will keep only
reference to last object created.

HTH
Alex

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
Hi Alex,

Thanks for your reply.

I would have thought that them being references would cause a problem.

For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case

Thanks again

Simon

Jul 21 '05 #5
Simon Harvey <si**********@the-web-works.co.uk> wrote:
I would have thought that them being references would cause a problem.
No, because the value of the variable is the reference. When you assign
a new value to the variable, you've lost the connection with the
previous value.
For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case


Not if you create a new object each time. There's a difference between:

Foo foo = new Foo();
for (...)
{
foo.SomeProperty = someValue;
someList.Add(foo);
}

and

Foo foo;;
for (...)
{
foo = new Foo();
foo.SomeProperty = someValue;
someList.Add(foo);
}

--
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
Hi Alex,

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
Hi Alex,

Thanks for your reply.

I would have thought that them being references would cause a problem.

For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.

When you come to access the command, the references will only point to the
last object. The rest will have no references to them and may be garbage
collected at any moment

Is this not the case


No, they will be referenced by the ArrayList's internal list . that's why
they are not collected

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jul 21 '05 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
See http://www.pobox.com/~skeet/csharp/parameters.html for info on
parameter passing and reference/value types.


Great articles Jon. I added your site in my favs for future reference.
Keep up the good work!

- Michael S

Jul 21 '05 #8
"Simon Harvey" <si**********@the-web-works.co.uk> wrote in
news:u9**************@TK2MSFTNGP12.phx.gbl:
Hi Alex,

Thanks for your reply.

I would have thought that them being references would cause a problem.

For example, lets say you add multiple commands to a list, repeatedly
updating the same variable name as I demonstrated in the OP.


The variable by itself contains a reference.

Think of it this way.

When you construct the SqlCommand object, it gets placed at memory
location 1000. 1000 is then stored into cmd.

When you do myArrayList.Add(cmd), you add 1000 to this list. You don't
add a reference to the variable itself, you just copy the value it
contains.

Next you do it all over again, but since the memory location 1000 and
forward is still (probably) occupied by the old object, you get 1050 this
time, and now 1050 is stored in cmd. 1050 is then added to the list. Now
there is two such objects in memory, at location 1000 and 1050.

At this point, your cmd variable contains 1050 referencing that object,
and your list contains 1000 and 1050 referencing those two objects. The
old object can not be garbage collected as the list still has an existing
reference to it.

As long as you have a valid reference which the compiler can go through
in order to find your objects (in this case it has your list, which has
references to the objects) then the objects won't be garbage collected.

--
Lasse Vågsæther Karlsen
la***@vkarlsen.no
PGP KeyID: 0x0270466B
Jul 21 '05 #9
Thanks all

Got it now

Simon
Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: pmatos | last post by:
Hi all, I've been having questions about strings, references, initializations... I've created code (which will not compile due to a reference problem) and I'd like comments on why this won't...
10
by: Michael Sgier | last post by:
Hello In main.cpp: CWorld* gameWorld = new CWorld; // do i need this? CSimpEngine* MyCSimpEngine = new CSimpEngine; CWorld *world = MyCSimpEngine->OnGetWorld(); world->Prepare(); //here i...
3
by: John Nagle | last post by:
Are weak refs slower than strong refs? I've been considering making the "parent" links in BeautifulSoup into weak refs, so the trees will release immediately when they're no longer needed. In...
7
by: lukertin | last post by:
I have a 2-D array stored as an object, when i go to call up the array using my $testvar = @ {$self->Peptides}; it gives me the error Can't use string ("0") as an ARRAY ref while "strict...
5
by: _dee | last post by:
I'm working on a port of a legacy app originally written in C. Data was compacted into bit fields. Are there any sites or books that cover optimized handling of this type of data? I'd need to...
3
by: RgeeK | last post by:
A project I'm thinking about, could use some advice from those who understand the dimensional limits of an SQL-like DB. Imagine, for example, a database containing detailed data for many cities...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.