tags:

views:

515

answers:

2

Newbie question. I’m writing an ASP.Net MVC app in VB.Net and have been using NerdDinner as a sample (which is in C#). I’m stuck on the validation process specifically the code found in Models\Dinner.cs . I have tried converting it to VB.Net using http://www.developerfusion.com/tools/convert/csharp-to-vb/ but it chokes on the Yield statement which found in the GetRuleViolations method (see code below). So my question is how would you do the equivalent in VB.Net?

namespace NerdDinner.Models {

[Bind(Include="Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")]
public partial class Dinner {

    public bool IsHostedBy(string userName) {
        return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
    }

    public bool IsUserRegistered(string userName) {
        return RSVPs.Any(r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase));
    }

    public bool IsValid {
        get { return (GetRuleViolations().Count() == 0); }
    }

    public IEnumerable<RuleViolation> GetRuleViolations() {

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

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

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

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

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

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

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

        yield break;
    }

    partial void OnValidate(ChangeAction action) {
        if (!IsValid)
            throw new ApplicationException("Rule violations prevent saving");
    }
}

}

A: 

Unfortunately, there is no equivalent to the yield statement in VB.Net.

TGnat
A gave this a -1. There is an equivalent to "yield" in VB, but it requires hand written IEnumerator(of T) implementations. It's irritating, and time consuming, but it's possible.
Scott Wisniewski
+2  A: 

Getting the "exact equivalent" in VB would require you to do a custom implementation of IEnumerator(of RuleViolation) using a state value and a switch statement. For something this simple, that would be overkill, however.

You can get a "mostly equivalent" version by creating a list, and populating it like this:

public function GetRuleViolations() as IEnumerable(of RuleViolation)
    dim ret = new List(of RuleViolation)();

    'replace the ... with the appopriate logic from above.
    if ... then
        ret.Add(...)
    end if

    return ret
end function

This is slightly less efficent than the C# version because it creates a list and returns all the items at once, where as the C# version returns each items on the fly as the "foreach" statement is executing. In this case the list is small, so it's not a big deal.

Scott Wisniewski
Thanks scott, I will try this out
John Marsing