2009-04-23 8 views
2

初心者の質問。私はVB.NetでASP.Net MVCアプリケーションを作成しており、NerdDinnerをサンプルとして使用しています(これはC#です)。私は検証プロセス、特にModels \ Dinner.csにあるコードについています。私はhttp://www.developerfusion.com/tools/convert/csharp-to-vb/を使用してVB.Netに変換しようとしましたが、GetRuleViolationsメソッド(下記のコードを参照)にあるYieldステートメントで突き詰めます。だから私の質問は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(Tの実装)を実装する必要があります。それは刺激的で時間がかかるものの、可能です。 –

関連する問題