New to .NET - ArrayList Question

  • Thread starter Thread starter Jerry West
  • Start date Start date
J

Jerry West

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).Item)

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
 
Jerry West said:
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).Item)

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)
 
Jerry said:
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).Item)

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.
 
Jerry said:
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).Item
Dim s As String = String.Empty

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

HTH,
Phill W.
 
Back
Top