New to .NET - ArrayList Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry West

    #1

    New to .NET - ArrayList Question

    I have an ArrayList of DirectoryInfo structures. As I'm certain you know,
    each DirectoryInfo structure has a property called FullName. I'm trying to
    get at the fullname property for the first element in the array so I can
    assign it to a var but I can't seem to figure out how to do it.

    Image.FromFile( alDirInfo(0).It em)

    Nothing I do reveals this property in the IDE to select. So do I have to use
    the CopyTo method of the ArrayList to get at the FullName property? It seems
    like I should be able to access each DirectoryInfo structure from right
    inside the array. Is this not possible?

    Thanks!

    JW


  • Fabio

    #2
    Re: New to .NET - ArrayList Question


    "Jerry West" <jw@comcast.net ha scritto nel messaggio
    news:13a4mcmmrc rs572@news.supe rnews.com...
    >I have an ArrayList of DirectoryInfo structures. As I'm certain you know,
    >each DirectoryInfo structure has a property called FullName. I'm trying to
    >get at the fullname property for the first element in the array so I can
    >assign it to a var but I can't seem to figure out how to do it.
    >
    Image.FromFile( alDirInfo(0).It em)
    >
    Nothing I do reveals this property in the IDE to select. So do I have to
    use the CopyTo method of the ArrayList to get at the FullName property? It
    seems like I should be able to access each DirectoryInfo structure from
    right inside the array. Is this not possible?
    >
    Replace ArrayList with List(Of DirectoryInfo)

    and obviously

    Image.FromFile( alDirInfo.Item( 0).FullName)

    --
    Help The New .Net Site! http://www.devbox4.net


    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: New to .NET - ArrayList Question

      Jerry West wrote:
      I have an ArrayList of DirectoryInfo structures. As I'm certain you know,
      each DirectoryInfo structure has a property called FullName. I'm trying to
      get at the fullname property for the first element in the array so I can
      assign it to a var but I can't seem to figure out how to do it.
      >
      Image.FromFile( alDirInfo(0).It em)
      >
      Nothing I do reveals this property in the IDE to select. So do I have to use
      the CopyTo method of the ArrayList to get at the FullName property? It seems
      like I should be able to access each DirectoryInfo structure from right
      inside the array. Is this not possible?
      >
      Thanks!
      >
      JW
      If you use framework 2.0, use the generic List instead of ArrayList (as
      Fabio suggested).

      If you use framework 1.x, there is no generics, so you are stuck with
      the ArrayList.

      The ArrayList is a list of Object, so when you access an item in the
      list, you get a reference to Object. You have to cast this reference to
      the actual type of the object that you stored in the list to be able to
      get to it's properties.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Phill W.

        #4
        Re: New to .NET - ArrayList Question

        Jerry West wrote:
        I have an ArrayList of DirectoryInfo structures.
        I'm trying to get at the fullname property for the first element
        in the array so I can assign it to a var but I can't seem to figure
        out how to do it.
        The Items of an ArrayList are typed "As Object", because they can be
        absolutely any type you can imagine. To get the Object "back" to the
        DirectoryInfo object you put in there, you have to "down-cast" each item
        to the correct Type (checking first, if there's any doubt), as in

        Dim o as Object = alDirInfo(0).It em
        Dim s As String = String.Empty

        If Typeof( o ) Is DirectoryInfo Then
        s = DirectCast( o, DirectoryInfo ).FullName
        End If

        HTH,
        Phill W.

        Comment

        Working...