C
Christopher H
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
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