Accessing Object Members In An Array List -- Syntax & Guidelines Help Please

J

José Joye

ArrayList holds "objects". Therefore, you will have to cast it back to
Simpleobject class to access your properties.
That being said, starting with .NET 2, I will prefer using generic
containers (e.g.: List<T>). This will be safer and prevent you casting back.


- José
 
T

Tom

When I create a simple object with get & set properties and place this
object in an ArrayList ... I am unable to access the individual
members. :(

This seems so basic, yet I am confused. :((

----------------------------------
public class SimpleObject
{
private long var_0;
public long Var_0
{
get { return var_0; }
set { var_0 = value; }
}
}

ArrayList myList = new ArrayList();
myList.Add(new SimpleObject());
-----------------------------------

Here's where I get lost >>

I want to set and get the object member within the ArrayList.

I've tried things such as:

myList[0].Var_0 = 2;

plus all kinds of syntax "guesses".

Please explain object access when they are members of an ArrayList.

Thanks!!

-- Tom
 
M

Marc Gravell

ArrayList is untyped; it doesn't know what is in there (and indeed,
you could add any combination of objects).

To use lists sensibly (in 2.0 and above) you should use List<T>
instead, i.e.

List<SimpleObject> myList = new List<SimpleObject>();
myList.Add(new SimpleObject());
myList[0].Var_0 = 2;

Marc
 
T

Tom

Thank you Jose and Marc !!

I am going to try casting because I *need* experience doing this and
then I am going to utilize List<T>.

The need to cast to a specific type because the list is generic and
can hold any type of object you put in there is just too logical!
Geez, if I had an ego I'd be embarrassed.

This group is absolutely wonderful. I very much appreciate the help.

:))

-- Tom
 
J

Jon Skeet [C# MVP]

Peter Bromberg said:
No, the Arraylist is *not* Generic, it is a collection whose elements can
only hold type Object. That's why casting is necessary. The List<T> *is*
generic, so no casting required. Just to clarify.

I've always felt that the naming generics is unfortunate - because in
common parlance, an ArrayList is generic; it's vanilla, it can hold
anything.

In many ways generics are *actually* specialized, rather than generic.
Too late to fix now, of course.
 
R

Rick Lones

Tom said:
When I create a simple object with get & set properties and place this
object in an ArrayList ... I am unable to access the individual
members. :(

This seems so basic, yet I am confused. :((

----------------------------------
public class SimpleObject
{
private long var_0;
public long Var_0
{
get { return var_0; }
set { var_0 = value; }
}
}

ArrayList myList = new ArrayList();
myList.Add(new SimpleObject());
-----------------------------------

Here's where I get lost >>

I want to set and get the object member within the ArrayList.

I've tried things such as:

myList[0].Var_0 = 2;

plus all kinds of syntax "guesses".

Please explain object access when they are members of an ArrayList.

The details of the one-step casting syntax you are looking for here is:

((SimpleObject)(myList[0])).Var_0 = 2;

Declaring a SimpleObject and casting myList[0] to it before accessing the
property would be more readable, IMO.

HTH,
-rick-
 
T

Tom

HTH,

Yes it did. *Thanks* Rick. I got the casting working and observed all
those parenthesis. Yuck. Then created the List<T> and was pleased with
its syntax. :) Finally, my old school structure type thinking is
migrating towards classes with get & set. Works great with List<T>.
:))

-- Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top