Solved

Convert YIELD in C# to vb.net

Posted on 2011-02-23
5
973 Views
Last Modified: 2012-05-11
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);
                }
0
Comment
Question by:HankinCooper
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
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
 

Author Comment

by:HankinCooper
Comment Utility
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
 

Author Comment

by:HankinCooper
Comment Utility
sorry, and this function..

        Function Lines(ByVal rdr As TextReader) As IEnumerable(Of String)
            Return New VByield(rdr)
        End Function
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
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
 

Author Comment

by:HankinCooper
Comment Utility
you are awesome!!!!!!!!!!!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now