Question Assigning the collection returned by My.Computer.FileSystem.GetFiles to a variable

Keith Howard

Active member
Joined
Jan 28, 2010
Messages
28
Programming Experience
5-10
Hello.

Do you know how to declare a variable that can hold the result of a call to GetFiles?

I don't know how to do this, so I am using the following verbose/duplicative version:

Dim l_v_String_FoundFile As String
If My.Computer.FileSystem.GetFiles("A:\u_A\u_W", FileIO.SearchOption.SearchAllSubDirectories, l_v_String_FileMask).Count = 1 Then
l_v_String_FoundFile = My.Computer.FileSystem.GetFiles("A:\u_A\u_W", FileIO.SearchOption.SearchAllSubDirectories, l_v_String_FileMask).Item(0)
End If

Thanks.

Keith
 
use type inference
Dim result = My.Computer.FileSystem.GetFiles(...)

result variable will have the type that is returned by GetFiles method. Check with documentation and you can see they are the same type.
 
First result from a google search turns up MSDN, with an example:

' Usage
Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(directory)


People need to practice their google-fu.
 
Thanks Herman,

That answers my question.

I did try to look it up but was confused by the ReadOnlyCollection(Of String) syntax.

Cheers,

Keith
 
' Usage
Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(directory)
While correct, it is lots of keyboard typing for no reason.
 
One good reason would be code clarity, in a business environment leaving it to the next guy to decipher what you wrote isn't really a good practice.

In fact where I work we insist on fully qualified names, not only for types, but for objects, functions, and variables as well. It doesn't take much longer to hit one letter + tab a couple more times when you write the line, and it ensure whoever comes after has no ambiguity to deal with in the existing code base.
 
Last edited:
One good reason would be code clarity, in a business environment leaving it to the next guy to decipher what you wrote isn't really a good practice.

In fact where I work we insist on fully qualified names, not only for types, but for objects, functions, and variables as well. It doesn't take much longer to hit one letter + tab a couple more times when you write the line, and it ensure whoever comes after has no ambiguity to deal with in the existing code base.

The only reason that there would be ambiguity is that you're looking at the code outside of the IDE. If it's inside the IDE then you simply mouse over a variable or whatever and Intellisense gives you the rest of the information. Personally, I would never fully qualify something unless it was used only in one place my code or it was required to disambiguate something for the compiler. I would always use type inference too. Not to say that other options are wrong but they make code more verbose and therefore, in my opinion, harder to read.
 
Hello Keith

I dont know if you are ok with what has already been said as your last post showed some confusion but when you have something of type ReadOnlyCollection(Of String) then just think of this as an array of string, you have to have the variable of the type of ReadOnlyCollection(Of String) or you will get an error but when it comes down to it, it is just another array.

Regards
 
Hello Hip Turtle!

I do understand now, but I need to use that syntax a bit to get used to it. I would have expected something like:

Dim value As System.Collections.ObjectModel.ReadOnlyCollection() As String

instead of:

Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

But I guess the declaration syntax is different for arrays and collections.

Thanks.

Keith
 
The (Of String) syntax means generics, there is a ReadOnlyCollection(Of T) generic class where T is a type placeholder that can be any given type, in this case it is String.
Here for example you can read about generics: Generic Types in Visual Basic (Visual Basic)
 
It's not that the syntax is different for arrays and collections. That is the syntax for generic types. Using the most common generic type and it's non-generic analogue as an example, .NET 1.x provided the ArrayList class to provide dynamic array functionality. Unlike an array, an ArrayList could grow and shrink as required. It had various issues though, the main one being the fact that each item was an Object reference. This meant that absolutely anything could be added to an ArrayList and each item had to be cast when retrieved. Unlike a String array, which can only accept Strings, there was no way to restrict an ArrayList to accept only Strings.

Then along came .NET 2.0 and introduced generics. It basically replaced the ArrayList with the List(Of T) class, where T is any type you chose to specify. By creating a List(Of String) you create a collection that can only accept Strings. By creating a List(Of Integer) you create a collection that can only accept Integers. A ReadOnlyCollection(Of T) is like a List(Of T) except that, once created, it cannot be changed.
 
It's not that the syntax is different for arrays and collections. That is the syntax for generic types. Using the most common generic type and it's non-generic analogue as an example, .NET 1.x provided the ArrayList class to provide dynamic array functionality. Unlike an array, an ArrayList could grow and shrink as required. It had various issues though, the main one being the fact that each item was an Object reference. This meant that absolutely anything could be added to an ArrayList and each item had to be cast when retrieved. Unlike a String array, which can only accept Strings, there was no way to restrict an ArrayList to accept only Strings.

Then along came .NET 2.0 and introduced generics. It basically replaced the ArrayList with the List(Of T) class, where T is any type you chose to specify. By creating a List(Of String) you create a collection that can only accept Strings. By creating a List(Of Integer) you create a collection that can only accept Integers. A ReadOnlyCollection(Of T) is like a List(Of T) except that, once created, it cannot be changed.


I understand that and agree with everything you are saying about the List(Of T) class as it is today, and I suppose I was trying to simply things too much to explain what this class is. Yes the List Collection is not an array of Type as such but it has similarities IE if you look at a List(Of String) populated with 10 items and an Array of string populated with those same 10 items they will look the same (item for item). If I have caused any confusion then I apologise.

Regards
 
@hipturtle, I wasn't having a go at you or what you posted. I was simply responding to post #9.

@Keith Howard, please update your profile. You are obviously not using .NET 1.1.
 
@hipturtle, I wasn't having a go at you or what you posted. I was simply responding to post #9.

Thanks for that I am new to forums and sometimes get a bit lost with all the posts, particularly when a few people are posting about the same thing.

Regards
 
Thanks for that I am new to forums and sometimes get a bit lost with all the posts, particularly when a few people are posting about the same thing.

Regards

Indeed. I should have quoted the post I was responding to. My mistake.
 
Back
Top