Convert YIELD in C# to vb.net

How can I quickly convert this to VB please

            foreach (string dir in subDirs.Reverse())
                {
                    DirectoryInfo directory = new DirectoryInfo(dir);
                    yield return new File(directory.Name, parentDirectory);
                    dirs.Push(dir);
                }
HankinCooperAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
wdosanjosConnect With a Mentor Commented:
The example code you provided is not complete, so I cannot provide a matching sample.  The code below implements the basic pattern by providing an iterator that returns MyFile objects from the directory provided. I think you should be able to adapt it to your specific application.

Option Strict On
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Runtime.CompilerServices


Module Module1

    Sub Main()
        Dim root As DirectoryInfo

        root = New DirectoryInfo("C:\Windows\Temp")

        Dim query = From file In root.Files _
                    Where file.Name.EndsWith(".dat", StringComparison.CurrentCultureIgnoreCase)

        For Each file In query
            Console.WriteLine("{0}{1}", file.ParentDirectory.FullName, file.Name)
        Next

        Console.ReadLine()

    End Sub

End Module


Public Class MyFile
    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _parentDir As DirectoryInfo
    Public Property ParentDirectory() As DirectoryInfo
        Get
            Return _parentDir
        End Get
        Set(ByVal value As DirectoryInfo)
            _parentDir = value
        End Set
    End Property

    Sub New(ByVal name As String, ByVal parentDir As DirectoryInfo)
        _name = name
        _parentDir = parentDir
    End Sub
End Class


Module DirectoryInfoExtensions

    <Extension()> _
    Function Files(ByVal dir As DirectoryInfo) As IEnumerable(Of MyFile)
        Return New DirectoryFileEnumerator(dir)
    End Function

    Public Class DirectoryFileEnumerator
        Implements IEnumerable(Of MyFile)
        Implements IEnumerator(Of MyFile)

        Private _dir As DirectoryInfo
        Private _current As MyFile
        Private _files() As FileInfo
        Private _index As Integer

        Sub New(ByVal dir As DirectoryInfo)
            _dir = dir
            _files = _dir.GetFiles()
            _index = 0
        End Sub


        Public Function GetEnumerator() As IEnumerator(Of MyFile) Implements IEnumerable(Of MyFile).GetEnumerator
            Static iFirstTime As Int32
            If iFirstTime = 0 AndAlso Threading.Interlocked.Increment(iFirstTime) = 1 Then
                Return Me
            Else
                Throw New InvalidOperationException("GetEnumerator can only be called once on a stream")
            End If
        End Function


        Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
            Dim hasNext As Boolean = False

            If _index < _files.Length Then
                _current = New MyFile(_files(_index).Name, _dir)
                _index = _index + 1
                hasNext = True
            End If

            Return hasNext
        End Function


        Private Function IEnumerator_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
            Return GetEnumerator()
        End Function


        Public ReadOnly Property Current() As MyFile Implements IEnumerator(Of MyFile).Current
            Get
                Return _current
            End Get
        End Property

        Private ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current
            Get
                Return Current
            End Get
        End Property


        Private Sub Reset() Implements IEnumerator.Reset
            Throw New NotImplementedException()
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            'not implemented
        End Sub

    End Class



End Module

Open in new window

0
 
wdosanjosCommented:
Unfortunately, VB does not have a 'yield return' equivalent.  There are some workarounds though.  Please check the following Visual Studio Magazine article for more details:

http://visualstudiomagazine.com/Articles/2009/02/01/Use-Iterators-in-VB-Now.aspx?Page=2
0
 
HankinCooperAuthor Commented:
ok, so created this class, where/how to I apply it to my example please? thanks...



Imports System.IO

Namespace ains
    Public Class VByield
        Implements IEnumerable(Of String)
        Implements IEnumerator(Of String)

        Private _rdr As TextReader
        Private _current As String

        Sub New(ByVal rdr As TextReader)
            _rdr = rdr
        End Sub


        ''' <summary>
        '''    Get's an enumerator to read lines from a stream
        ''' </summary>
        ''' <returns>IEnumerator(Of String)</returns>
        ''' <remarks>Can only be called once due to the underlying stream. Not thread safe.</remarks>
        ''' <exception cref="InvalidOperationException">thrown if GetEnumerator is called more than once</exception>
        '''
        Public Function GetEnumerator() As IEnumerator(Of String) Implements IEnumerable(Of String).GetEnumerator
            Static iFirstTime As Int32
            If iFirstTime = 0 AndAlso Threading.Interlocked.Increment(iFirstTime) = 1 Then
                Return Me
            Else
                Throw New InvalidOperationException("GetEnumerator can only be called once on a stream")
            End If
        End Function


        Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
            _current = _rdr.ReadLine()
            Return _current IsNot Nothing
        End Function


        Private Function IEnumerator_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
            Return GetEnumerator()
        End Function


        Public ReadOnly Property Current() As String Implements IEnumerator(Of String).Current
            Get
                Return _current
            End Get
        End Property

        Private ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current
            Get
                Return Current
            End Get
        End Property


        Private Sub Reset() Implements IEnumerator.Reset
            Throw New NotImplementedException()
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            'not implemented
        End Sub

    End Class
End Namespace
0
 
HankinCooperAuthor Commented:
sorry, and this function..

        Function Lines(ByVal rdr As TextReader) As IEnumerable(Of String)
            Return New VByield(rdr)
        End Function
0
 
HankinCooperAuthor Commented:
you are awesome!!!!!!!!!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.