yield question

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

    yield question

    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().ToA rray<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.
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: yield question

    On Apr 4, 11:14 am, CSharper <cshar...@gmx.c omwrote:
    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().ToA rray<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

    Comment

    • CSharper

      #3
      Re: yield question

      On Apr 4, 3:56 pm, "Ignacio Machin ( .NET/ C# MVP )"
      <ignacio.mac... @gmail.comwrote :
      On Apr 4, 11:14 am, CSharper <cshar...@gmx.c omwrote:
      >
      >
      >
      >
      >
      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().ToA rray<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- Hide quoted text -
      >
      - Show quoted text -
      Yes I did, but I got it resolved with Jon's approch.

      Comment

      Working...