1

I have been tasked with creating a Directory.EnumerateFiles version for VB.NET in 2008.

I have managed to do this for VS2008 in c#:

public IEnumerable<string> GetFileList(string fileSearchPattern, string rootFolderPath)
{
    Queue<string> pending = new Queue<string>();
    pending.Enqueue(rootFolderPath);
    string[] tmp;
    while (pending.Count > 0)
    {
        rootFolderPath = pending.Dequeue();
        tmp = Directory.GetFiles(rootFolderPath, fileSearchPattern, SearchOption.AllDirectories);
        for (int i = 0; i < tmp.Length; i++)
        {
            yield return tmp[i];
        }
        tmp = Directory.GetDirectories(rootFolderPath);
        for (int i = 0; i < tmp.Length; i++)
        {
            pending.Enqueue(tmp[i]);
        }
    }
}

and now i am translating it to vb.net:

Private Function GetfileList(ByVal fileSearchPattern As String, ByVal rootFolderPath As String) As IEnumerable(Of String)
    Dim pending As New Queue(Of String)()

    pending.Enqueue(rootFolderPath)
    Dim tmp() As String


    While (pending.Count > 0)
        rootFolderPath = pending.Dequeue()
        tmp = Directory.GetFiles(rootFolderPath, fileSearchPattern, SearchOption.AllDirectories)
        For counter As Integer = 0 To tmp.Length - 1

            'Yield gives an error as it does not appear to be a keyword!!

            tmp = Directory.GetDirectories(rootFolderPath)
            For i As Integer = 0 To tmp.Length - 1
                pending.Enqueue(tmp(i))
            Next
        Next
    End While
End Function

but Yield keyword gives me an error as 'not declared'?

Is there an alternative to the Yield that vb.net 2008 supports?

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 1
    It wasn't added until VS2012 I believe. – Ric Jan 13 '16 at 13:17
  • @Ric thanks for your comment. I have adjusted my question to a wider scope :) – Andrew Simpson Jan 13 '16 at 13:18
  • 1
    some useful reading: http://stackoverflow.com/a/2912862/885626 - also a a side-note: your for loops in the vb.net example are not the same as the c# version as you have an inner `for` which does the `Enqueue` in the vb version. – Ric Jan 13 '16 at 13:20
  • it appears it cannot be then. thanks – Andrew Simpson Jan 13 '16 at 13:24
  • but, my question is slightly different to those links in such that i do not HAVE to use Yield – Andrew Simpson Jan 13 '16 at 13:25
  • I suppose you could just `Enqueue` every item then return the `pending` variable as implements `IEnumerable` interface, or create a `List` and return that... – Ric Jan 13 '16 at 13:26
  • @ric thanks tryimg to work out how that would work.. – Andrew Simpson Jan 13 '16 at 13:34
  • What is the point of using yield anyway? The `GetFiles` call will not return until it has gotten all of the filenames. Can't you just return that array? The advantage of `EnumerateFiles` is that it returns before all of the files are found so you can begin iterating over the list without having to wait. I don't see that your methods provide that advantage. I think in order to implement `EnumerateFiles` yourself you will have to use the `FindFirstFileEx` and `FindNextFile` pInvoke functions. – Chris Dunaway Jan 13 '16 at 18:04
  • Decompiling mscorlib and looking at their implementation confirms that they use `FindFirstFile` and `FindNextFile`. As to the `Yield` keyword in VB.Net, for earlier versions you would have to implement your own iterator and use the `MoveNext`, etc. methods. – Chris Dunaway Jan 13 '16 at 18:14

0 Answers0