some logic for constructor

  • Thread starter Thread starter André Freitas
  • Start date Start date
A

André Freitas

All fields in my project are validated with regular expressions. I have put
this regular expressions inside a certain function, like below:

Public Shared Function ValidateText(ByRef LabelName as String, ByVal Text As
String, ByVal Pattern As String) As Boolean

Dim vStringDictionary As StringDictionary
vStringDictionary = New StringDictionary

vStringDictionary.Add("UsernameP", "^[a-zA-Z0-9]{6,20}$")
vStringDictionary.Add("UsernameE", "Nome de usuário inválido.")

vStringDictionary.Add("PasswordP", "^[a-zA-Z0-9]{6,20}$")
vStringDictionary.Add("PasswordE", "Senha inválida.")

[...]

I do call the function passing the label where the message will apear, in
case of false, the text to validate, and the pattern name.

Using:
vStringDictionary(Pattern & "P") i do get the pattern, and
vStringDictionary(Pattern & "E") i do get the error message to return to the
label in case of false.

The problem is: always when i call this function im declaring the dictionary
again, and it sounds a waste. Anyone see a best logic for this?

Thx
 
André Freitas said:
All fields in my project are validated with regular expressions. I have
put this regular expressions inside a certain function, like below:

Public Shared Function ValidateText(ByRef LabelName as String, ByVal
Text As String, ByVal Pattern As String) As Boolean

Dim vStringDictionary As StringDictionary
vStringDictionary = New StringDictionary

vStringDictionary.Add("UsernameP", "^[a-zA-Z0-9]{6,20}$")
vStringDictionary.Add("UsernameE", "Nome de usuário inválido.")

vStringDictionary.Add("PasswordP", "^[a-zA-Z0-9]{6,20}$")
vStringDictionary.Add("PasswordE", "Senha inválida.")

[...]

I do call the function passing the label where the message will apear,
in case of false, the text to validate, and the pattern name.

Using:
vStringDictionary(Pattern & "P") i do get the pattern, and
vStringDictionary(Pattern & "E") i do get the error message to return to
the label in case of false.

The problem is: always when i call this function im declaring the
dictionary again, and it sounds a waste. Anyone see a best logic for this?

Thx

Use a static variable and populate it in the static constructor.

I would use a class for storing the pattern and message instead of using
suffix on the key:


Public Class ValidationInfo

Private _pattern As String, _message As String

Public Sub New(pattern As String, message As String)
_pattern = pattern;
_message = message;
End Sub

Public Property Pattern As String
Get
Return _pattern;
End Get
End Property

Public Property Message As String
Get
Return _message;
End Get
End Property

End Class


Create a static variable for the dictionary:

Private Shared _validationInfo As Dictionary(Of String, ValidationInfo)


Create the dictionary and populate it in the static constructor:

Shared Sub New()
_validationInfo = New Dictionary(Of String, ValidationInfo)()
_validationInfo.Add("", New ValidationInfo("", ""))
_validationInfo.Add("Username", _
New ValidationInfo( _
"^[a-zA-Z0-9]{6,20}$", _
"Nome de usuário inválido.")
)
_validationInfo.Add("Password", _
New ValidationInfo( _
"^[a-zA-Z0-9]{6,20}$", _
"Senha inválida.")
)
End Sub


Now you can use the dictionary in the method:

Dim info As ValidationInfo = _validationInfo(Pattern)
 
Very very nice, thxx a lot.
=D

Göran Andersson said:
André Freitas said:
All fields in my project are validated with regular expressions. I have
put this regular expressions inside a certain function, like below:

Public Shared Function ValidateText(ByRef LabelName as String, ByVal Text
As String, ByVal Pattern As String) As Boolean

Dim vStringDictionary As StringDictionary
vStringDictionary = New StringDictionary

vStringDictionary.Add("UsernameP", "^[a-zA-Z0-9]{6,20}$")
vStringDictionary.Add("UsernameE", "Nome de usuário inválido.")

vStringDictionary.Add("PasswordP", "^[a-zA-Z0-9]{6,20}$")
vStringDictionary.Add("PasswordE", "Senha inválida.")

[...]

I do call the function passing the label where the message will apear, in
case of false, the text to validate, and the pattern name.

Using:
vStringDictionary(Pattern & "P") i do get the pattern, and
vStringDictionary(Pattern & "E") i do get the error message to return to
the label in case of false.

The problem is: always when i call this function im declaring the
dictionary again, and it sounds a waste. Anyone see a best logic for
this?

Thx

Use a static variable and populate it in the static constructor.

I would use a class for storing the pattern and message instead of using
suffix on the key:


Public Class ValidationInfo

Private _pattern As String, _message As String

Public Sub New(pattern As String, message As String)
_pattern = pattern;
_message = message;
End Sub

Public Property Pattern As String
Get
Return _pattern;
End Get
End Property

Public Property Message As String
Get
Return _message;
End Get
End Property

End Class


Create a static variable for the dictionary:

Private Shared _validationInfo As Dictionary(Of String, ValidationInfo)


Create the dictionary and populate it in the static constructor:

Shared Sub New()
_validationInfo = New Dictionary(Of String, ValidationInfo)()
_validationInfo.Add("", New ValidationInfo("", ""))
_validationInfo.Add("Username", _
New ValidationInfo( _
"^[a-zA-Z0-9]{6,20}$", _
"Nome de usuário inválido.")
)
_validationInfo.Add("Password", _
New ValidationInfo( _
"^[a-zA-Z0-9]{6,20}$", _
"Senha inválida.")
)
End Sub


Now you can use the dictionary in the method:

Dim info As ValidationInfo = _validationInfo(Pattern)
 
Back
Top