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

Passing an arraylist which contains a struct which contains an arraylist

I've been reading about how C# passes ArrayLists as reference and Structs as
value, but I still can't get my program to work like I want it to.

Simple example:
code:------------------------------------------------------------------------------
class Program
{
static public ArrayList MyArrayList = new ArrayList();
static void Main(string[] args)
{

MyArrayList.Add(0);
Console.WriteLine(MyArrayList[0]);
SomeFunction(MyArrayList);
Console.WriteLine(MyArrayList[0]);

Console.ReadLine();
}

static void SomeFunction(ArrayList MyArrayList) {
ArrayList NewArrayList = new ArrayList(MyArrayList);

NewArrayList[0] = 1;
}
}
------------------------------------------------------------------------------

In SomeFunction I create a new ArrayList and not a reference to the old one
(MyArrayList). So when I modify NewArrayList, the change is not reflected in
MyArrayList in Main(). Output:
code:------------------------------------------------------------------------------
0
0
------------------------------------------------------------------------------

Easy enough and works as expected. Here's where I've been going bananas for
the last couple of hours.
It involves an arraylist inside a struct which is itself inside an
arraylist.
code:------------------------------------------------------------------------------
class Program
{
static public ArrayList MyArrayList = new ArrayList();
public struct Struct_Row {
public ArrayList ArrayList_Struct;
};

static void Main(string[] args)
{

Struct_Row testRow = new Struct_Row();
testRow.ArrayList_Struct = new ArrayList();
testRow.ArrayList_Struct.Add(2);
MyArrayList.Add(testRow);

Console.WriteLine(((Struct_Row)MyArrayList[0]).ArrayList_Struct[0]);
SomeFunction(MyArrayList);
Console.WriteLine(((Struct_Row)MyArrayList[0]).ArrayList_Struct[0]);
Console.ReadLine();
}

static void SomeFunction(ArrayList MyArrayList) {
ArrayList NewArrayList = new ArrayList(MyArrayList);

((Struct_Row)NewArrayList[0]).ArrayList_Struct[0] = 3;
}

}
------------------------------------------------------------------------------

Even though I've created a new arraylist in SomeFunction(), when I change
the
value of ArrayList_Struct[0] in SomeFunction(), the change is reflected in
Main() which I don't want :( Output:
code:------------------------------------------------------------------------------
2
3
------------------------------------------------------------------------------

So basically my question is, what do I have to do to be able to change the
values of NewArrayList.ArrayList_Struct in SomeFunction() that will not
change MyArrayList.ArrayList_Struct in Main() ?

Apr 13 '07 #1
3 2603
Inline
"Christopher H" <to****@NOSPAMhippoman.netwrote in message
news:iP*******************@wagner.videotron.net...
I've been reading about how C# passes ArrayLists as reference and
Structs as
value, but I still can't get my program to work like I want it to.

Simple example:
code:------------------------------------------------------------------------------
class Program
{
static public ArrayList MyArrayList = new ArrayList();
static void Main(string[] args)
{

MyArrayList.Add(0);
Console.WriteLine(MyArrayList[0]);
SomeFunction(MyArrayList);
Console.WriteLine(MyArrayList[0]);

Console.ReadLine();
}

static void SomeFunction(ArrayList MyArrayList) {
ArrayList NewArrayList = new ArrayList(MyArrayList);

NewArrayList[0] = 1;
}
}
------------------------------------------------------------------------------

In SomeFunction I create a new ArrayList and not a reference to the
old one
(MyArrayList). So when I modify NewArrayList, the change is not
reflected in
MyArrayList in Main(). Output:
code:------------------------------------------------------------------------------
0
0
------------------------------------------------------------------------------

Easy enough and works as expected.
It worked because it was an arraylist of primitive types.
When NewArrayList was created from MyArrayList it did a simple copy of
the elements in the first arraylist to the second arraylist.
>Here's where I've been going bananas for the last couple of hours.
It involves an arraylist inside a struct which is itself inside an
arraylist.
code:------------------------------------------------------------------------------
class Program
{
static public ArrayList MyArrayList = new ArrayList();
public struct Struct_Row {
public ArrayList ArrayList_Struct;
};

static void Main(string[] args)
{

Struct_Row testRow = new Struct_Row();
testRow.ArrayList_Struct = new ArrayList();
testRow.ArrayList_Struct.Add(2);
MyArrayList.Add(testRow);
Console.WriteLine(((Struct_Row)MyArrayList[0]).ArrayList_Struct[0]);
SomeFunction(MyArrayList);

Console.WriteLine(((Struct_Row)MyArrayList[0]).ArrayList_Struct[0]);
Console.ReadLine();
}

static void SomeFunction(ArrayList MyArrayList) {
ArrayList NewArrayList = new ArrayList(MyArrayList);

((Struct_Row)NewArrayList[0]).ArrayList_Struct[0] = 3;
}

}
------------------------------------------------------------------------------

Even though I've created a new arraylist in SomeFunction(), when I
change the
value of ArrayList_Struct[0] in SomeFunction(), the change is
reflected in
Main() which I don't want :( Output:
code:------------------------------------------------------------------------------
2
3
------------------------------------------------------------------------------
Yup, it still simply did the copy of all elements in the arraylist. In
this case that is a copy of your struct with a copy of the reference to
the other arraylist....It still points to the original arraylist...thus
the resulting behavior.
>
So basically my question is, what do I have to do to be able to change
the
values of NewArrayList.ArrayList_Struct in SomeFunction() that will
not
change MyArrayList.ArrayList_Struct in Main() ?
What you want is a Deep Clone. Dotnet supports shallow clones (make a
copy).
If you wish to perform a deep clone, you will have to write the logic
yourself.

Basically you will have to extract the internal Arraylists, clone them
and stick them back in.

As a side note, not really relevant to your issue (yet), why are you
using a struct instead of a class?
Perhaps you attempted the struct for the value semantics. In this case
it was a nice try, and if you only had value types inside the struct it
would have worked. Unfortunately the reference to the ArrayList bit you.

Good luck
Bill

Apr 13 '07 #2
If I understand the code (not so clear), the problem is simply that
you are (when copying the ArrayList into the new ArrayList) cloning
the structure instances. This essentially does a mem-copy, with the
effect that any reference-fields (pointers, in other words) will be
pointing to *the same* underlying object on the managed heap : the
reference-types (classes) do not get cloned with their wrapper. This
means tha((Struct_Row)MyArrayLst[0]).ArrayList_Struct
points to the same arraylist each time. You cannot change this.

Marc

Apr 13 '07 #3
An ArrayList can not contain value types, only reference types, so when
you add a value type to the ArrayList, it will be boxed inside an
object, then added to the list.

Furthermore, as your structure contains a reference type, it only
contains a reference to the data. Even if you copy the structure, you
will only make a copy of the reference, the actual data is unchanged.

So, you are two steps from actually copying anything. When you create a
new ArrayList that contains the data from the static ArrayList, you are
only copying the references that point to the objects that contains the
structures, you are not copying the structures.

Let's say that you do copy the actual structures, like this:

ArrayList NewArrayList = new ArrayList()
foreach (object o in MyArrayList) {
Struct_Row row;
row.ArrayList_Struct = (row)o.ArrayList_Struct;
NewArrayList.Add(row);
}

Dispite the fact that you have copied the structures, you have still
only copied the references that the structures contains, not the data
that the references point to. When you use these copied structures, you
will still working with the same ArrayLists that the originals contains.
The value type characteristics of structures will not be useful to you
as long as the structure contains references. Stick to using classes
until you have found something that you can actually use structures for.

--
Göran Andersson
_____
http://www.guffa.com
Apr 13 '07 #4

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
2
by: Rustam Asgarov | last post by:
Hi. I have ArrayList that contains structures... I need to iterate through all structures in this array and update one of members in this structures... The first thing that comes to mind is just...
6
by: Michael C | last post by:
Is it possible to use an ArrayList inside a struct? I keep running into a null reference exception when I try to Add to the ArrayList in the struct, and it won't let me initialize the ArrayList in...
1
by: Irfan Akram | last post by:
Dear Friends, I'll be glad if you guys could help out. I am currently doing a web project in asp.net using c#. An ArrayList object in my code contains element records containing 3 fields. In a...
6
by: Germic | last post by:
Hi, I've never got an answer to this question in any group till now.... How do we pass a array of structures between a C# and a C DLL, I am using RCW interop? The C# Library gets an array of...
8
by: Bryan G | last post by:
Hi, I'm working on a VB project which involves using C library functions which take struct pointers as args, and I keep running into this error when trying to pass either an IntPtr or a Structure...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
4
by: sofeng | last post by:
The following link shows a chart I created about passing structures among functions. Would you review it and tell me if it requires any corrections? ...
14
by: jehugaleahsa | last post by:
Hello: I am working with Oracle .NET Stored Procedures. I would like to know how to return the results of a SELECT statement. I have tried returning a OracleRefCursor and a DataTable, but...
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...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.