By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

Object in arraylist

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<jk****@f2s.com> schrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #2

P: n/a
<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.
Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

P: n/a
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.