Reputation: 728
I see such C# code which allow me go with foreach cycle over class properties.
public class EstimateDetailsModel : IEnumerable<string>
{
public string Dma { get; set; }
public string Callsign { get; set; }
public string Description { get; set; }
public IEnumerator<string> GetEnumerator()
{
yield return Dma;
yield return Callsign;
yield return Description;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
Help please. How can I make something like this using vb.net?
Upvotes: 1
Views: 509
Reputation: 755121
What you have provided here is a C# iterator method (the methods containing yield return
). Unfortunately there is no analogous feature for VB.Net at this time. The easiest way to simulate this is to instead build up a List(Of String)
and return thate
Public Function GetEnumerator() As IEnumerable(Of String) _
Implements IEnumerable(Of String).GetEnumerator
Dim list As New List(Of String)()
list.Add(Dma)
list.Add(Callsign)
list.Add(Description)
return list
End Function
Public Function GetEnumerator2() As IEnumerable _
Implements IEnumerable.GetEnumerator
Return GetEnumerator()
End Function
This isn't exactly equivalent to the C# version. Mainly because it will be promptly executed instead of delayed executed. But the end effect will be roughly the same
EDIT
Ah I forgot that Vb.Net added iterator support in the latest version (Visual Studio 2012). If you are using that version of Visual Studio you can do the following
Public Iterator Function GetEnumerator() As IEnumerable(Of String) _
Implements IEnumerable(Of String).GetEnumerator
Yield Dma
Yield Callsign
Yield Description
End Function
Upvotes: 1