2009-06-29 12 views
1

私はNerdDinnerチュートリアルを通して自分のやり方を試しています。練習として、私はVBに変換しています。私はあまり遠くないし、C#の歩みステートメントを通過した後、私は共有VB配列の初期設定に固執しています。NerdDinnerの共有VB配列の初期値を作成する方法

static IDictionary<string, Regex> countryRegex = 
new Dictionary<string, Regex>() { 
{ "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")}, 
{ "UK", new 
Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0- 
9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")}, 
{ "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0- 
9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\- 
\\s]{10}$)")}, 

誰でもVBで書いてもらえますか?

Public Shared countryRegex As IDictionary(Of String, Regex) = New Dictionary(Of String, Regex)() {("USA", New Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$"))} 

このコードは、配列の項目として文字列と正規表現を受け入れないため、エラーが発生します。

おかげ

答えて

3

私はそれwill be in VB10だと思うが、私は、VB9は、コレクション初期化子をサポートしていることを信じていません。

最も単純なオプションは、辞書を作成してから返す共有メソッドを記述し、その変数メッセージを変数初期化子から呼び出すことです。だから、C#で、それは次のようになります。

static IDictionary<string, Regex> countryRegex = CreateCountryRegexDictionary(); 

static IDictionary<strnig, Regex CreateCountryRegexDictionary() 
{ 
    Dictionary<string, Regex>() ret = new Dictionary<string, Regex>(); 
    ret["USA"] = new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$"); 
    // etc 
    return ret; 
} 

うまくいけば、あなたは簡単にVBに変換することがわかります:)完全性について

+0

ありがとうございましたジョンさん、私は共有メソッドの中にすべてを置くことはできませんでしたが、私はそれらの行に沿って考えていた - 私はクラスにコンストラクタを追加することを検討していた!私の悪い!乾杯、 –

1

私のVB変換:

Public Shared Function GetIDictionary() As IDictionary(Of String, Regex) 
Dim countryRegex As IDictionary(Of String, Regex) = New Dictionary(Of String, Regex)() 
countryRegex("USA") = New Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$") 
countryRegex("UK") = New Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)") 
countryRegex("Netherlands") = New Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)") 
Return countryRegex 
End Function 

乾杯再びジョン

1

その場合は、完成したVB.Net NerdDinner PhoneValidator(英国およびアイルランド携帯電話含む)

Public Class PhoneValidator 

    Private Shared Function GetIDictionary() As IDictionary(Of String, Regex) 
     Dim countryRegex As IDictionary(Of String, Regex) = New Dictionary(Of String, Regex)() 
     countryRegex("USA") = New Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$") 
     countryRegex("UK") = New Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)") 
     countryRegex("Netherlands") = New Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)") 
     countryRegex("Ireland") = New Regex("^((07|00447|\+447)\d{9}|(08|003538|\+3538)\d{8,9})$") 
     ' 
     Return countryRegex 
    End Function 

    Public Shared Function IsValidNumber(ByVal phoneNumber As String, ByVal country As String) As Boolean 

     If country IsNot Nothing AndAlso GetIDictionary.ContainsKey(country) Then 
      Return GetIDictionary(country).IsMatch(phoneNumber) 
     Else 
      Return False 
     End If 
    End Function 

    Public ReadOnly Property Countries() As IEnumerable(Of String) 
     Get 
      Return GetIDictionary.Keys 
     End Get 
    End Property 

End Class 
+0

乾杯、レオ、それは役に立つと確信しています! –

関連する問題