hai..
im havin 2 classes :classA ,classB
classA contains arraylist...
objects of classB are added into the arraylist in ClassA..
how can I access properties of classB from arraylist in classA
for ex. i tried this...
classA.classB[0].property ...its not working!!!
AlanPosted Sep 15, 2007, 6:44 AM
You need to cast the ArrayList element to its actual type before you can access a property of that type. Try this example:
using System;
using System.Collections;
class Program
{
static void Main()
{
A a = new A();
ArrayList al = new ArrayList();
B b = new B();
b.MyInt = 3;
al.Add(b);
a.ListOfB = al;
Console.WriteLine(((B)a.ListOfB[0]).MyInt.ToString());
Console.ReadLine();
}
}
class A
{
ArrayList listOfB;
public ArrayList ListOfB
{
get{ return listOfB; }
set{ listOfB = value; }
}
}
class B
{
int myInt;
public int MyInt
{
get{ return myInt; }
set{ myInt = value; }
}
}