473,322 Members | 1,431 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,322 software developers and data experts.

Array List

Dan
I have an object that I want to add to an ArrayList.
However, I don't know ahead of time how many objects I
will need to add to the list. Therefore, I had built a
function that created an instace of the object, set its
properties, and then added it to the ArrayList. The
function would then adjust the properties of the object
and again add that object to the ArrayList. The problem I
am having is that the changes I make to, what I thought
would be object 2, are also reflected in object 1 when I
retrieve it from the ArrayList. Does the add method of the
ArrayList pass the object by reference or by value? Does
anyone know how to get around this?

Thanks for the help!
Nov 15 '05 #1
2 3243
100
Hi Dan,
ArrayList saves what you pass to the Add method. If you pass the reference
it will save that reference at some index. If you pass a value then it will
box first the value (because the methods accept parameters of type
Object)and then save a reference to the boxed value. It does not make any
copies of the objects added except in the casses where boxing takes place .

Maybe your function should create a new object each time it has to add a new
object to the list instead just adjust the properties.
Take a look at IClonable interface. But bare in mind that even if you
implement IClonable ArrayList won't use it and will save the reference you
pass to the Add method.

HTH
B\rgds
100

"Dan" <dd*****@fnisolutions.com> wrote in message
news:36****************************@phx.gbl...
I have an object that I want to add to an ArrayList.
However, I don't know ahead of time how many objects I
will need to add to the list. Therefore, I had built a
function that created an instace of the object, set its
properties, and then added it to the ArrayList. The
function would then adjust the properties of the object
and again add that object to the ArrayList. The problem I
am having is that the changes I make to, what I thought
would be object 2, are also reflected in object 1 when I
retrieve it from the ArrayList. Does the add method of the
ArrayList pass the object by reference or by value? Does
anyone know how to get around this?

Thanks for the help!

Nov 15 '05 #2
Dan <dd*****@fnisolutions.com> wrote:
I have an object that I want to add to an ArrayList.
However, I don't know ahead of time how many objects I
will need to add to the list. Therefore, I had built a
function that created an instace of the object, set its
properties, and then added it to the ArrayList. The
function would then adjust the properties of the object
and again add that object to the ArrayList. The problem I
am having is that the changes I make to, what I thought
would be object 2, are also reflected in object 1 when I
retrieve it from the ArrayList. Does the add method of the
ArrayList pass the object by reference or by value? Does
anyone know how to get around this?


It doesn't pass the object at all. It passes a reference, and it passes
that by value.

It sounds like you're being confused by the same thing a lot of others
have been in the past, namely the difference between reference types
and value types, and what "pass-by-reference" *really* means.

See http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

for some information which will hopefully help you. It's worth making
sure you really understand this - it's absolutely key to working in
..NET.

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

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

Similar topics

6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
1
by: aemazing | last post by:
hello, im new to the forum and i wanted to help with my c++ program. the teacher wants us to design a progrm that would keep track of airplanes awaitin landing at an airport. the program will...
1
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
1
by: cyrvb | last post by:
Hello, I'm a very very newbie in C# I did start 2 days ago, I get Visual Stuido 2005 C# I try to understand how to manage the arrays I did write this
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
4
by: Bob Bedford | last post by:
Hello, I've an array of array(1,2,3,4). I'd like to retrieve the values in 3 and 4 giving the values 1 and 2. How can I do that, how to search in an array of array ? in short, how to write the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.