Casting ArrayList entries as different structures

Discussion in 'ASP .Net' started by =?Utf-8?B?Sm9l?=, Nov 28, 2005.

  1. 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,
    --
    Joe

    VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
    =?Utf-8?B?Sm9l?=, Nov 28, 2005
    #1
    1. Advertising

  2. 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

    =?Utf-8?B?Sm9l?= <> wrote in
    news::

    > 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,




    --
    cbDevelopment, Dec 4, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,730
    Natty Gur
    Aug 19, 2003
  2. tweak
    Replies:
    14
    Views:
    2,767
    Eric Sosman
    Jun 11, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    703
    Emmanuel Delahaye
    Sep 24, 2005
  4. Tom McGlynn
    Replies:
    19
    Views:
    1,274
    Daniel Pitts
    Jun 24, 2009
  5. Don Bruder
    Replies:
    3
    Views:
    951
    spikeysnack
    Aug 3, 2010
Loading...

Share This Page