Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I implement yield return for IEnumerable functions in VB.NET? [duplicate]

Possible Duplicate:
Yield In VB.NET

In C#, when writing a function that returns an IEnumerble<>, you can use yield return to return a single item of the enumeration and yield break; to signify no remaining items. What is the VB.NET syntax for doing the same thing?

An example from the NerdDinner code:

public IEnumerable<RuleViolation> GetRuleViolations() {

   if (String.IsNullOrEmpty(Title))
       yield return new RuleViolation("Title required","Title");

   if (String.IsNullOrEmpty(Description))
       yield return new RuleViolation("Description required","Description");

   if (String.IsNullOrEmpty(HostedBy))
       yield return new RuleViolation("HostedBy required", "HostedBy");

   if (String.IsNullOrEmpty(Address))
       yield return new RuleViolation("Address required", "Address");

   if (String.IsNullOrEmpty(Country))
       yield return new RuleViolation("Country required", "Country");

   if (String.IsNullOrEmpty(ContactPhone))
       yield return new RuleViolation("Phone# required", "ContactPhone");

   if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
       yield return new RuleViolation("Phone# does not match country", "ContactPhone");

   yield break;
}

This convert C# to VB.NET tool gives a "YieldStatement is unsupported" error.

like image 655
CoderDennis Avatar asked May 17 '09 21:05

CoderDennis


People also ask

How do you use IEnumerable in VB NET?

VB.NET IEnumerable Examples Use the IEnumerable Interface with arrays, Lists, and For-Each loops. IEnumerable. An IEnumerable collection contains elements. We use the For-Each loop in VB.NET to loop over these elements. Lists and arrays implement this interface. Interface benefits. We can use Extensions, such as ToList, on an IEnumerable type.

How to yield a value in VB NET?

VB.NET does not have the "yield" keyword. So, there are a few solutions (none of which are really clean) to get around this. You could use a return statement to return the value if you are looping through and would like to break an enumerator and return a single value.

Is it possible to implement two functions in IEnumerable (of T)?

Since IEnumerable (Of T) is inherited from IEnumerable interface, you will have to implement two similar Function s that are almost identical in names, but they are differ in the return type. Which function shall be implemented?

Is there a quick fix for yield return in VB NET?

This newer answer shows use of VB's Yield value (inside an Iterator Function declaration), which was added years after this question was asked. There is none. Period. Unless you are going to write your own state machine, there is no quick fix for this. See the blog post VB.NET's "yield return" .


3 Answers

There is currently no equivalent to C#'s yield return in VB.Net from a language syntax level.

However there was a recent write up in MSDN magazine by Bill McCarthy on how to implement a similar pattern in VB.Net 9.0

  • http://visualstudiomagazine.com/articles/2009/02/01/use-iterators-in-vb-now.aspx
like image 95
JaredPar Avatar answered Oct 22 '22 01:10

JaredPar


The new Async CTP includes support for Yield in VB.NET.

See Iterators in Visual Basic for information on usage.

like image 37
CoderDennis Avatar answered Oct 22 '22 00:10

CoderDennis


See my answers here:

  • Yield in VB.NET
  • Iterator pattern in VB.NET (C# would use yield!)

To summarize:
VB.Net does not have yield, but C# implements yield by converting your code to a state machine behind that scenes. VB.Net's Static keyword also allows you to store state within a function, so in theory you should be able to implement a class that allows you to write similar code when used as a Static member of a method.

like image 2
Joel Coehoorn Avatar answered Oct 21 '22 23:10

Joel Coehoorn