Convert 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' to '1-dimensional array of St

R

roidy

Hi, I`m trying to get the files in a directory using:-

Dim files As String() = My.Computer.FileSystem.GetFiles("c:\")

But get the following error:-

Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)'
cannot be converted to '1-dimensional array of String'


I need the files as an array of strings to pass to another function. So my
question is how to convert a read only collection of strings to an array of
strings

Thanks
Rob
 
R

roidy

Dim files as string() = Directory.GetFiles("c:\") - this work but I need to
support multiple wildcards eg.

My.Computer.FileSystem.GetFiles("c:\",
FileIO.SearchOption.SearchTopLevelOnly, "*.txt", "*.doc")

It seem as if Directory.GetFiles only supports a single wildcard.

Rob
 
G

gillardg

hi here is a one minute solution


Dim files As String()
Dim x As Integer = 0
For Each str As String In My.Computer.FileSystem.GetFiles("c:\")
ReDim files(x + 1)
files(x) = str
x = x + 1
Next


but maybe there is a better way to do that :)
 
R

roidy

Thanks, that was the kind of code I`d come up with, I was hoping there was a
more elegant single line solution.

Rob

ps You need ReDim Preserve else the array is wiped every time :)
 
T

Tom Shelton

Hi, I`m trying to get the files in a directory using:-

Dim files As String() = My.Computer.FileSystem.GetFiles("c:\")

But get the following error:-

Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)'
cannot be converted to '1-dimensional array of String'


I need the files as an array of strings to pass to another function. So my
question is how to convert a read only collection of strings to an array of
strings

Thanks
Rob

Dim files As String() = System.IO.Directory.GetFiles ("c:\")

My suggestion is to learn the framework, and abandon most of the My.Crap
stuff.
 
R

roidy

If you read my 2nd post you`ll see I can`t use System.IO.Directory.Getfiles
because it doesn`t support multiple wildcards.

Rob
 
T

Tom Shelton

If you read my 2nd post you`ll see I can`t use System.IO.Directory.Getfiles
because it doesn`t support multiple wildcards.

Rob

I can see how my answer could have been read as insulting - but, it was not
meant to be so. I apologize for that.

Still, I would avoid My for this. A simple function along these lines should
suffice:

Function GetFiles(ByVal path As String, ByVal ParamArray wildCards() As String) As String()
If wildCards.Length = 0 Then
Return Directory.GetFiles(path)
End If

Dim files As New List(Of String)
For Each wildCard As String In wildCards
files.AddRange(Directory.GetFiles(path, wildCard))
Next

Return files.ToArray()
End Function
 
A

Andrew Morton

my question is how to convert a read only collection of strings to an
array of strings

Look in the help for ReadOnlyCollection in the "extension methods" section.

Andrew
 
A

Andrew Morton

Look in the help for ReadOnlyCollection in the "extension methods"

Well, apart from familiarizing yourself with more of the .NET framework, go
with Tom's suggestion, it is a much better way of going about the original
problem.

Andrew
 
R

roidy

Hey it takes more than that to insult me : )

Thanks for the code, can I just ask why should 'My' be avoided? I guess I
was just hoping for an easy way to convert a ReadOnlyCollection(Of String)
to an array of strings.

Rob
 
A

Armin Zingler

roidy said:
Thanks for the code, can I just ask why should 'My' be avoided?

It's a PITA and like cancer pullulating my own project.

In other words? Naturally I use libraries, but 'My' means something
different: It is _inserted_ into my own project. I don't want this because
the 'My' design does not meet the claims that I have in many ways. So, it's
sink or swim. The My infrastructure contravenes the conventions that must be
complyed with throughout the rest of the project.

It's starts with the name: 'My'. What's that? No, it's no mine! Sorry,
you're out! I have other naming conventions. How can I change the name?
Impossible. Where does the name come from? Maybe from "Myspace"?
"My.Application" - yeah, it's my application; that's a nice word game but
has nothing to do with professional programming. Namespace names are not
meant to create a kind of sentence. My.Dog.Ran.Away or what?

'My' is intransparent. I always want to know what I do. I can't with 'My'.
Whenever I "go to definition", it doesn't work in most cases. Everything is
hidden behind the curtain. I hate this. That's VB6 behavior. Please, not
again! Try it with "My.Application" using "go to definition" on the word
'Application'. Where are you taken to? No, not to 'Application' as expected.
You're taken to "WindowsApplication1.My". Ok, I've found the namesspace.
Great. But where is the member 'Application'? I don't find it. Further more,
if 'My' is a namespace, 'Application' is what? Mustn't it be a namespace
name or a class name? Well, Ctrl+I (member info) on the word 'Application'
says it's a property. How can that be? Namespaces don't have properties. And
so on... It's a catastrophe. With the means of the IDE I failed to find out
what this 'My' thing really is.

My is redundant. Many things are already there in the (rest of the)
Framework. That's where it belongs to. If the feature makes sense and is not
there outside 'My', why hasn't it been included there? Either it's useless
or, if it's not, then there is a good chance that it will be added later so
that other langauges will benefit from it also. But then it's redundant
again. Many methods inside 'My' just call what has already been there
before. Why do we need My.Computer.Clock.TickCount? It just calls
Environment.Tickcount.

Navigate to Microsoft.VisualBasic.Devices.ServerComputer.FileSystem in
object browser. It's type is displayed as
Microsoft.VisualBasic.MyServices.FileSystemProxy. In the lower pane you can
click on that type. Where are you taken to? Nowhere! But I want to see the
members. I can't! Where are they? I can use them in code but don't see them
in the object browser??

My uses Modules. Modules are prohibited by convention in my project (yes, in
*my* project. 'My' is not my project).

My uses instances where it doesn't make sense. Why are those members like
Microsoft.VisualBasic.Devices.Computer.Audio not shared members? Will there
be other Computer instances?

An example: My.Settigs my be nice. However, I want my settings to be a
shared member of my Class Main. So, how can I move the feature there?
Impossible.

So, 'My' is just for dummies that don't have a clear idea of good
application design and don't care about such a brick placed into each
project. 'My' is one reason why MSFT calls VB a language for beginners again
even if it is not. That's a big step back into the past. Instead of making
people aware of how great VB has become since .Net.

I've created a new project template using _mtype="empty".


Armin
 
R

roidy

Err..... OK...... So I guess that means you really don`t like 'My' : )

All joking aside I kind of see what you mean.

Rob
 
A

Armin Zingler

roidy said:
Err..... OK...... So I guess that means you really don`t like 'My'
:)

How can you know? :-o ;)
All joking aside I kind of see what you mean.

Rob

That's my personal opinion only, of course. Probably I don't understand the
concept.
So, 'My' is just for dummies [..]

Was not meant to be offending. Should read: 'My' was designed as if we were
dummies....

I'm guessing only. I don't know it.


Just before anybody complains...


Armin
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top