如何为 NerdDinner 创建共享 VB 数组初始值设定项

发布于 2024-07-26 03:15:16 字数 784 浏览 4 评论 0原文

我正在努力完成 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}$)")},

谁能帮我用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}$"))}

此代码有一个错误,因为它不接受字符串和正则表达式作为数组的项目。

谢谢

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.

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?

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年华零落成诗 2024-08-02 03:15:16

我不相信 VB9 支持集合初始值设定项,尽管我认为它 将在 VB10 中

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

I don't believe that VB9 supports collection initializers, although I think it will be in 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:

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 :)

夜吻♂芭芘 2024-08-02 03:15:16

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

再次欢呼乔恩

My VB conversion for completeness:

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

还给你自由 2024-08-02 03:15:16

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文