2009-04-23 4 views
2

새내기 문제입니다. VB.Net에서 ASP.Net MVC 응용 프로그램을 작성 중이며 NerdDinner를 샘플로 사용하고 있습니다 (C#에 있음). 나는 Models \ Dinner.cs에있는 코드의 검증 과정을 고수하고있다. 내가 VB에서 닷넷 http://www.developerfusion.com/tools/convert/csharp-to-vb/ 사용하여 변환 시도했다 그러나 그것은 GetRuleViolations 메서드 (아래 코드 참조) 찾을 항복 문을 질식. 그래서 내 질문에 VB.Net에서 어떻게 동등한 할 것입니다?VB.Net을 사용하는 NerdDinner 유효성 검사 질문

네임 스페이스 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"); 
    } 
} 

}

+0

C#에서이 기능을 사용하지 못하게하는 제한 사항은 무엇입니까? –

답변

3

VB에서 "정확히 일치하는"것을 얻으려면 상태 값과 switch 문을 사용하여 (RuleViolation의) IEnumerator를 사용자 정의 구현해야합니다. 이 간단한 것을 위해, 그것은 과잉 일 것입니다.

당신은 목록을 작성하고,이처럼 채워은 "대부분 해당"버전을 얻을 수 있습니다 : 그것은 목록을 생성하고 모든 항목을 반환하기 때문에

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 

이는 C# 버전보다 약간 적은 efficent입니다 C# 버전에서 "foreach"문이 실행되는 즉시 해당 항목을 반환합니다. 이 경우 목록은 작기 때문에 큰 문제는 아닙니다.

+0

감사합니다. scott, 나는 이것을 시험해 보겠습니다. –

0

불행하게도, VB.Net에서 yield 문에 상응 없다.

+1

A가 -1로 나타납니다. VB에서 "yield"와 동등하지만 IEnumerator (of T) 구현체를 직접 작성해야합니다. 짜증나고 시간이 많이 걸리지 만 가능합니다. –

관련 문제