Results 1 to 5 of 5

Thread: C# yield return in VB

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    C# yield return in VB

    Hello, I'm converting some C# code and I'm stuck at this line:

    c# Code:
    1. while (iterator.MoveNext()) yield return iterator.Current;

    The function returns IEnumerable. Can anyone tell me how this fragment can be re-written in VB?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: C# yield return in VB

    I know that VB doesn't have an equivalent to the C# 'yield' keyword. I didn't know what the best way to approximate it was though. I Googled. I found the answer. Funny that.

    http://blog.matthewdoig.com/?p=81
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: C# yield return in VB

    Well then I read this article. But still I'm not quite sure I understand how this works.

    Here's the original function in c#:

    c# Code:
    1. public static IEnumerable ForwardNodeIterator(Node firstNode, bool mustBeVisible)
    2.         {
    3.                 ForwardNodeEnumerator iterator = new ForwardNodeEnumerator(firstNode, mustBeVisible);
    4.                 while (iterator.MoveNext())
    5.                     yield return iterator.Current;
    6.         }

    I have a class that implements IEnumerator(Of node) (as in the original and it does not implement IEnumerable. Does the article you provided suggest that I need also a helper class that implements IEnumerable(Of Node) in order to achieve the result?


    P.S.
    I have a class that implements IEnumerator
    If add this to the class:
    vb Code:
    1. Implements IEnumerator(Of Node)
    2.  ' IEnumerator implementation here
    3.  
    4. Implements IEnumerable(Of Node)
    5.  
    6. Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Node) Implements System.Collections.Generic.IEnumerable(Of Node).GetEnumerator
    7.     Return Me
    8. End Function
    9.  
    10. Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
    11.     Return Me
    12. End Function

    would this work?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: C# yield return in VB

    I haven't actually read the details but I do know that you need to do a reasonable amount of work to simulate 'yield' in VB.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: C# yield return in VB

    You "simply" have to implement IEnumerable and IEnumerator interfaces. It's not pretty though. The yield keyword in C# basically writes a state machine for you that does the implementation of those two interfaces. I'd suggest that it's probably not worth the effort. If you really need to yield (rather than creating the entire sequence and returning everything in a List or array or similar) then I'd suggest it would be easiest to not convert that code from C#.

    Supposedly, VB will be getting an equivalent to yield in a future version that will actually be more powerful than the simple yield keyword in C#.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width