473,327 Members | 1,930 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,327 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
Nov 16 '05 #1
9 919
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

Nov 16 '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
Nov 16 '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
Nov 16 '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

Nov 16 '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
Nov 16 '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
Nov 16 '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

Nov 16 '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
Nov 16 '05 #9
Thanks all

Got it now

Simon
Nov 16 '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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.