【问题标题】:How to create Shared VB Array Initialisors for NerdDinner如何为 NerdDinner 创建共享 VB 数组初始化器
【发布时间】:2009-06-29 12:28:18
【问题描述】:

我正在尝试完成 NerdDinner 教程 - 作为练习,我正在将其转换为 VB。我不是很远,在通过 C# Yield 语句之后,我被困在 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}$)")},

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

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

谢谢

【问题讨论】:

    标签: vb.net arrays shared nerddinner


    【解决方案1】:

    我不相信 VB9 支持集合初始化器,虽然我认为它will be in 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 :)

    【讨论】:

    • 感谢 Jon,我一直在思考这些问题,尽管我还没有把它全部放在 Shared 方法中——我只是在考虑为类添加一个构造函数!我的错!干杯
    【解决方案2】:

    为了完整性我的 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
    

    再次为乔恩干杯

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 干杯利奥,我相信它会很有用!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2015-10-09
      相关资源
      最近更新 更多