Guys, I’m trying to convert something from C# to VB.NET and I’m having trouble finding an equivlent in VB.NET to C#’s yield keyword. I realize ‘yield’ is not a convertable keyword to VB.NET, so can someone please show me how I would implement this code in VB.NET. I got all of it converted over except for the implemented GetEnumerator() function. It is simply a class that implements CollectionBase and IEnumerable (to make it LINQ worthy):
[Serializable()]
public partial class Customers : CollectionBase, System.Collections.Generic.IEnumerable<BusinessLayer.Customer>
{
public new System.Collections.Generic.IEnumerator<BusinessLayer.Customer> GetEnumerator()
{
foreach (BusinessLayer.Customer Cust in this.List)
{
yield return Cust;
}
}
public Customers()
{
}
public Customers(DataRowCollection datarows) : this()
{
this.Load(datarows);
}
protected void Load(DataRowCollection dataRows)
{
foreach (DataRow dr in dataRows) {
this.Add(new Customer(dr));
}
}
public Customer this[int index] {
get { return (Customer)base.InnerList[index]; }
set { base.InnerList[index] = value; }
}
public int Add(Customer val)
{
return base.InnerList.Add(val);
}
}
Thanks in advance for any help!
Because you cannot use the
yieldkeyword, you will have to implementGetEnumerator()another way. What you can do is return the enumerator of theListfromCollectionBase. However, because this is anIListand not anIList<T>, you will have to cast it (you can use theCast<T>()extension method from Linq for this). Your C# code then becomes:This gives the same result, but behaves in a slightly different way (with regards to no longer using the delayed execution of
yield).In VB.Net,
GetEnumerator()would be:The rest of your code should translate directly to VB.Net.