如何为NerdDinner创建共享的VB数组初始值

问题描述:

我正在努力通过NerdDinner教程 - 而且作为一个练习,我将它转换为VB。我并不是很熟悉C#Yield语句,而是被困在Shared VB Array Initialisors中。如何为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}$"))} 

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

感谢

我不相信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 :)

+0

感谢Jon,我一直在思考这些问题,尽管我没有把它全部放在Shared方法中 - 我只是在考虑给这个类添加一个构造函数!我的错!干杯 – 2009-06-29 12:37:54

我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 

干杯乔恩

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

干杯利奥,我相信它会很有用! – 2009-10-02 07:54:55