Object in arraylist

  • Thread starter Thread starter jkhome
  • Start date Start date
J

jkhome

Please can someone help me. I have an arraylist which contains objects
how do i go about reading the data out of the objects in the arraylist,
code example below.. I really have no idea how to do this!!! The code
below just shows me it is a 'system.object[]' when displayed on the
screen but i want its contents. I know i can do objItem(0) but i don't
know how to get the upper bound or do i need to convert it to another
type!!

myArray = testing.flowLogic()

For Each objItem In myArray
Response.Write(objItem.ToString & "<BR>")
Next
 
Please can someone help me. I have an arraylist which contains objects
how do i go about reading the data out of the objects in the arraylist,
code example below.. I really have no idea how to do this!!! The code
below just shows me it is a 'system.object[]' when displayed on the
screen but i want its contents. I know i can do objItem(0) but i don't
know how to get the upper bound or do i need to convert it to another
type!!

myArray = testing.flowLogic()

What's the type of the objects stored in the arraylist?
 
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.
 
Below is the code i am using to create the array list. It is reading
data from a datareader into an object type

Private Function processSQL(ByVal processData As SqlDataReader) As
ArrayList

Dim rowlist As New ArrayList
'Dim returnArray As New ArrayList

If TypeOf processData Is SqlDataReader Then


While (processData.Read)
Dim values(processData.FieldCount) As Object
processData.GetValues(values)
rowlist.Add(values)
End While

rowlist.TrimToSize()
ArrayList.ReadOnly(rowlist)

'ElseIf TypeOf processData Is String Then

Else

End If

Return rowlist

End Function
 
Below is the code i am using to create the array list. It is reading
data from a datareader into an object type


Private Function processSQL(ByVal processData As SqlDataReader) As
ArrayList


Dim rowlist As New ArrayList
'Dim returnArray As New ArrayList


If TypeOf processData Is SqlDataReader Then


While (processData.Read)
Dim values(processData.FieldCount) As Object
processData.GetValues(values)
rowlist.Add(values)
End While


rowlist.TrimToSize()
ArrayList.ReadOnly(rowlist)


'ElseIf TypeOf processData Is String Then


Else


End If


Return rowlist


End Function
 

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

Back
Top