<jk****@f2s.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have an arraylist which contains objects
how do i go about reading the data out of the objects in the arraylist
Every class you write "Is A" object, that is classes like ArrayList
can do /everything/ with your class that it can do with an Object.
That's why you can just add your objects into the ArrayList without
having to create your own, Strongly-Typed collection class.
But, when the ArrayList hands you the object back, it comes back
as a System.Object, because that's as much as the ArrayList cares
about.
BUT - you /know/ it's one of your classes (or you can find out
whether or not it is), so you can "downcast" the Object into your Type,
as in
Dim oThing as Object _
= myArray.Item( 0 )
Dim oTypedThing as MyType _
= DirectCast( oThing, myType )
oTypedThing.myProperty = 1
or, for the one-liner fraternity
DirectCast( myArray.Item(0), myType ).myProperty = 1
For Each objItem In myArray
Response.Write(objItem.ToString & "<BR>")
Next
How is objItem defined? "Dim objItem as Object", perhaps?
Try this :
myArray.Clear()
myArray.Add( New myType() )
myArray.Add( New myType() )
myArray.Add( New myType() )
For Each objItem As myType In myArray
Response.Write(objItem.GetType().ToString() & "<BR>")
Next
HTH,
Phill W.