yield question

C

CSharper

I have the following code

ArrayList list = new ArrayList();
foreach (string name in FilesNames())
{
list.Add(name);
}
return list.ToArray();

which basically calls FileNames() method which yeild a string for each
call. It works file. The objective of this method to return an
object[] back so that it can be used to add it into the listview.
Adding the list view also works correct.

My question, is there a better way of doing this?

return FileNames().ToArray<object>(); ???

I thought it might work but the convertion from IEnumeration to
ICollection doesn't work, what is the best way of doing it?

Thanks.
 
J

Jon Skeet [C# MVP]

CSharper said:
I have the following code

ArrayList list = new ArrayList();
foreach (string name in FilesNames())
{
list.Add(name);
}
return list.ToArray();

which basically calls FileNames() method which yeild a string for each
call. It works file. The objective of this method to return an
object[] back so that it can be used to add it into the listview.
Adding the list view also works correct.

My question, is there a better way of doing this?

return FileNames().ToArray<object>(); ???

I thought it might work but the convertion from IEnumeration to
ICollection doesn't work, what is the best way of doing it?

It would help to know the actual signature of FileNames().

If it's just IEnumerator instead of IEnumerator<object> or
IEnumerator<string> then you could use:

FileNames().Cast<object>().ToArray()
 
C

CSharper

CSharper said:
I have the following code
ArrayList list = new ArrayList();
foreach (string name in FilesNames())
{
list.Add(name);
}
return list.ToArray();
which basically calls FileNames() method which yeild a string for each
call. It works file. The objective of this method to return an
object[] back so that it can be used to add it into the listview.
Adding the list view also works correct.
My question, is there a better way of doing this?
return FileNames().ToArray<object>(); ???
I thought it might work but the convertion from IEnumeration to
ICollection doesn't work, what is the best way of doing it?

It would help to know the actual signature of FileNames().

If it's just IEnumerator instead of IEnumerator<object> or
IEnumerator<string> then you could use:

FileNames().Cast<object>().ToArray()

Thanks, Cast worked.
Thanks again, Learn something everyday..
 
I

Ignacio Machin ( .NET/ C# MVP )

I have the following code

ArrayList list = new ArrayList();
foreach (string name in FilesNames())
{
     list.Add(name);}

return list.ToArray();

which basically calls FileNames() method which yeild a string for each
call. It works file. The objective of this method to return an
object[] back so that it can be used to add it into the listview.
Adding the list view also works correct.

My question, is there a better way of doing this?

return FileNames().ToArray<object>(); ???

I thought it might work but the convertion from IEnumeration to
ICollection doesn't work, what is the best way of doing it?

Thanks.

Hi,

Did you try binding FilesNames() directly? You are not forced to use
ArrayList
 
C

CSharper

I have the following code
ArrayList list = new ArrayList();
foreach (string name in FilesNames())
{
     list.Add(name);}
return list.ToArray();
which basically calls FileNames() method which yeild a string for each
call. It works file. The objective of this method to return an
object[] back so that it can be used to add it into the listview.
Adding the list view also works correct.
My question, is there a better way of doing this?
return FileNames().ToArray<object>(); ???
I thought it might work but the convertion from IEnumeration to
ICollection doesn't work, what is the best way of doing it?

Hi,

Did you try binding FilesNames() directly? You are not forced to use
ArrayList- Hide quoted text -

- Show quoted text -

Yes I did, but I got it resolved with Jon's approch.
 

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