Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert YIELD in C# to vb.net

Posted on 2011-02-23
5
Medium Priority
?
997 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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