如何为NerdDinner创建共享VB数组初始化器

时间:2023-02-13 09:41:19

I am trying to work my way through the NerdDinner tutorial - and as an exercise I'm converting it to VB as I go. I'm not very far in and after having gotten past the C# Yield statement I'm stuck on Shared VB Array Initialisors.

我正在努力通过NerdDinner教程 - 并且作为练习我正在将它转换为VB。我已经离开了C#Yield语句,我不是很远,我被困在共享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}$)")},

Can anyone please help me write this in VB?

有人可以帮我写一下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}$"))}

This code has an error as it does not accept the String and the Regex as an item for the array.

此代码有错误,因为它不接受String和Regex作为数组的项。

Thanks

3 个解决方案

#1


I don't believe that VB9 supports collection initializers, although I think it will be in VB10.

我不相信VB9支持集合初始化器,虽然我认为它将在VB10中。

The simplest option is probably to write a shared method which creates and then returns the dictionary, and call that shared message from the variable initializer. So in C#, it would be:

最简单的选择可能是编写一个共享方法,该方法创建然后返回字典,并从变量初始化程序中调用该共享消息。所以在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;
}

Hopefully you'll find that easier to translate into VB :)

希望你会发现更容易翻译成VB :)

#2


My VB conversion for completeness:

我的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

Cheers again Jon

再次干杯乔恩

#3


In case its any use, here is my completed VB.Net NerdDinner PhoneValidator incl UK and Ireland Mobile Phones

如果有任何用途,这里是我完成的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

#1


I don't believe that VB9 supports collection initializers, although I think it will be in VB10.

我不相信VB9支持集合初始化器,虽然我认为它将在VB10中。

The simplest option is probably to write a shared method which creates and then returns the dictionary, and call that shared message from the variable initializer. So in C#, it would be:

最简单的选择可能是编写一个共享方法,该方法创建然后返回字典,并从变量初始化程序中调用该共享消息。所以在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;
}

Hopefully you'll find that easier to translate into VB :)

希望你会发现更容易翻译成VB :)

#2


My VB conversion for completeness:

我的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

Cheers again Jon

再次干杯乔恩

#3


In case its any use, here is my completed VB.Net NerdDinner PhoneValidator incl UK and Ireland Mobile Phones

如果有任何用途,这里是我完成的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