Re: ArrayList consisting of objects



In addition to Steven,

Have a look at the keyword "new"
"new" tells that it is a new array (you are instancing that from a Class
running its instancing method)

Without it it is telling that it is referencing (setting the reference) to
an existing one.

Cor

"ZS" <ZS@xxxxxxxxxxxxxxxxxxxxxxxxx> schreef in bericht
news:035E8687-4045-4287-A56C-6E43810D9F16@xxxxxxxxxxxxxxxx
Hi,
Here is a piece of code where I have a class by name SomeClass
array1 consists of objects of the type SomeClass
array2 is intialized with contents of array1.
Why does changes made to the object of array2 affect the same object in
array1.

The o/p for the following should be just '1' but why is it '11'?

ArrayList array1 = new ArrayList();
array1.Add(new SomeClass("1"));
array1.Add(new SomeClass("2"));

ArrayList array2 = new ArrayList();
array2 = array1;
array1.Add(new SomeClass("3"));


SomeClass al = (SomeClass) array2[0];
al.AlName = "11";

SomeClass newObj = (SomeClass) array1[0];
MessageBox.Show (newObj.AlName);


.



Relevant Pages

  • Re: ArrayList consisting of objects
    ... array1 consists of objects of the type SomeClass ... array2 is intialized with contents of array1. ... ArrayList array1 = new ArrayList; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Creating a Table of a Type which contains a nested table
    ... array1 t_array_va, ... array2 t_array_va ... Syntax and documentation are available on ... SQL> create or replace type t_complex_type as object ...
    (comp.databases.oracle.server)
  • Re: exception question,
    ... Suppose the constructor for Effect throws an exception. ... will Array1 and Array2 be deleted? ... What's the type of Array1? ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Creating a Table of a Type which contains a nested table
    ... Example 1 with VARRAY: ... array1 t_array_va, ... array2 t_array_va ... I've seen example 1 syntax in Oracle documentation but not for example 2. ...
    (comp.databases.oracle.server)
  • Re: Function CrossOver in VB.net
    ... 'Function will fail if Array2 is shorter than Array1 ... Dim ResultAs Boolean ...
    (microsoft.public.dotnet.languages.vb)