Casting ArrayList entries as different structures

G

Guest

Hello All:

I am writing a function which accepts an ArrayList as its parameter and
converts the contents of the ArrayList into an XmlNodeList. The ArrayList
will contain one of several different structures. For example, the
structures could be either a Customer structure (which contains a customer id
and name) or an Address structure (which contains an address type and street
number, city, etc). The XmlNodeList is uded to populate a DropDownList where
the id or address type are the ListItem's value and other information is the
ListItem's text.

I want the function to determine which structure is in the ArrayList
(Customer or Address) and cast the ArrayList entry appropriately. I can code
everything except the how to determine which structure is in the ArrayList.

For example:

Public Function Foo(AL as ArrayList) as XmlNodeList
Dim e as System.Collections.iEnumerator = AL.GetEnumerator
Do While e.MoveNext
' Determine which structure is contained in e.Current
' Perform other tasks with the e.Current entry of the ArrayList
Loop
'return some XmlNodeList
End Function

Does anyone know how to do this?

TIA,
 
C

cbDevelopment

You could do something like:

For each O as object in AL
if typeof O is CUSTOMER then ' Process as customer
elseif typeof O is ADDRESS then ' process as address
elseif etc...
end if
Next

Or you could create an interface that all your items share, and you could
cast all the items in the arraylist to that interface and treat them
identically.

----
700cb Development, Inc.
http://www.700cb.net
..NET utilities, developer tools,
and enterprise solutions

Hello All:

I am writing a function which accepts an ArrayList as its parameter
and converts the contents of the ArrayList into an XmlNodeList. The
ArrayList will contain one of several different structures. For
example, the structures could be either a Customer structure (which
contains a customer id and name) or an Address structure (which
contains an address type and street number, city, etc). The
XmlNodeList is uded to populate a DropDownList where the id or address
type are the ListItem's value and other information is the ListItem's
text.

I want the function to determine which structure is in the ArrayList
(Customer or Address) and cast the ArrayList entry appropriately. I
can code everything except the how to determine which structure is in
the ArrayList.

For example:

Public Function Foo(AL as ArrayList) as XmlNodeList
Dim e as System.Collections.iEnumerator = AL.GetEnumerator
Do While e.MoveNext
' Determine which structure is contained in e.Current
' Perform other tasks with the e.Current entry of the
ArrayList
Loop
'return some XmlNodeList
End Function

Does anyone know how to do this?

TIA,



--
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top