Nerddinner를위한 공유 VB 배열 초기 라이서를 만드는 방법
-
21-08-2019 - |
문제
나는 Nerddinner 튜토리얼을 통해 내 길을 다루려고 노력하고 있습니다. 운동으로 VB로 변환합니다. 나는 C# 수율 명세서를 지나서 그리 멀지 않았다.
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}$"))}
이 코드는 문자열을 허용하지 않기 때문에 오류가 있고 배열의 항목으로 REGEX가 허용됩니다.
감사
해결책
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로 번역하기가 더 쉽다는 것을 알게되기를 바랍니다. :)
다른 팁
완전성에 대한 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
제휴하지 않습니다 StackOverflow