Solved

Convert YIELD in C# to vb.net

Posted on 2011-02-23
5
982 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
ID: 34963099
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
ID: 34963368
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
ID: 34963374
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
ID: 34965129
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
ID: 34966043
you are awesome!!!!!!!!!!!!!
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

822 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