473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Object in arraylist

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
4 4978
<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
<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tamir Khason | last post by:
How can I set the type of the object added to ArrayList (type of Array List Members) Here is the code: protected ArrayList tabs = new ArrayList(); public ArrayList Tabs {
2
by: Ralf B. | last post by:
Hi everybody I feel kind of dumb for not finding anything useful to point me in the right direction.. basically, I want to create a class with an Arraylist. I would like to add strings and...
5
by: zfeld | last post by:
How do I cast an object to its proper class at runtime given its System.Type I have code that looks like this: MyObject class has subclasses of MySubObjectA & MySubObjectB: MyObject obja =...
4
by: Arjen | last post by:
Hi, I have a class with some attributes. For example class person with name as attribute. I have add multiple persons in an arraylist. Now I need a function to get/find a person by the name...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
1
by: Jeremy S. | last post by:
I suspect I may be fundamentally misunderstanding how object variable references behave - and I'd sure appreciate some clarification - given my particular scenario. Summary: In an ASP.NET (1.1)...
3
by: Patrick.O.Ige | last post by:
I'm loading an Array below but getting the error "Object reference not set to an instance saying 'ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text' is the error line. I DON'T...
0
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
2
by: dotnetnoob | last post by:
i got this program that will fetch the data in the excel spreadsheet, it was working before then i make some adjustment and it now give me an error of "Object reference not set to an instance of an...
11
by: KMinFL | last post by:
This is a C# VS 2008 question... Our system has 2 base classes, SingleEntity and NewPluralEntity. SingleEntity provides access to properties and methods related to manipulating data in a database...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.