Object in arraylist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jkhome@f2s.com

    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.flowLog ic()

    For Each objItem In myArray
    Response.Write( objItem.ToStrin g & "<BR>")
    Next

  • Herfried K. Wagner [MVP]

    #2
    Re: Object in arraylist

    <jkhome@f2s.com > schrieb:[color=blue]
    > 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.flowLog ic()[/color]

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

    Comment

    • Phill.  W

      #3
      Re: Object in arraylist

      <jkhome@f2s.com > wrote in message
      news:1128078255 .748355.271250@ g49g2000cwa.goo glegroups.com.. .[color=blue]
      > I have an arraylist which contains objects
      > how do i go about reading the data out of the objects in the arraylist[/color]

      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.myP roperty = 1

      or, for the one-liner fraternity

      DirectCast( myArray.Item(0) , myType ).myProperty = 1

      [color=blue]
      > For Each objItem In myArray
      > Response.Write( objItem.ToStrin g & "<BR>")
      > Next[/color]

      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.


      Comment

      • jkhome@f2s.com

        #4
        Re: Object in arraylist

        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(ByVa l processData As SqlDataReader) As
        ArrayList

        Dim rowlist As New ArrayList
        'Dim returnArray As New ArrayList

        If TypeOf processData Is SqlDataReader Then


        While (processData.Re ad)
        Dim values(processD ata.FieldCount) As Object
        processData.Get Values(values)
        rowlist.Add(val ues)
        End While

        rowlist.TrimToS ize()
        ArrayList.ReadO nly(rowlist)

        'ElseIf TypeOf processData Is String Then

        Else

        End If

        Return rowlist

        End Function

        Comment

        • jkhome@f2s.com

          #5
          Re: Object in arraylist

          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(ByVa l processData As SqlDataReader) As
          ArrayList


          Dim rowlist As New ArrayList
          'Dim returnArray As New ArrayList


          If TypeOf processData Is SqlDataReader Then


          While (processData.Re ad)
          Dim values(processD ata.FieldCount) As Object
          processData.Get Values(values)
          rowlist.Add(val ues)
          End While


          rowlist.TrimToS ize()
          ArrayList.ReadO nly(rowlist)


          'ElseIf TypeOf processData Is String Then


          Else


          End If


          Return rowlist


          End Function

          Comment

          Working...