1

私は NerdDinner チュートリアルを進めようとしています - そして練習として、私はそれを VB に変換しています。私はそれほど遠くにいるわけではなく、C# の Yield ステートメントを通過した後、Shared VB Array Initializers で立ち往生しています。

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}$"))}

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

ありがとう

4

3 に答える 3

3

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 への変換がより簡単になるでしょう :)

于 2009-06-29T12:33:09.073 に答える
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

また乾杯 ジョン

于 2009-06-29T12:41:52.393 に答える
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
于 2009-10-01T07:55:42.003 に答える