Introduction
When getting or setting the value property of any control, the value is of type String
. When we need to convert the value to any other type, we have to perform a check to see if the value is empty and do code logic based on that. Also, when setting a value to a control, the value needs to be converted to String
. This is fine for any value type except nullable types. For everything else, we can do ToString()
and it will be OK, but for nullable types, we have to invoke the HasValue
property of the type.
To overcome this dilemma in a sophisticated way, we will use some generic methods that can convert a String
value returned by the control to any type, and can convert a value of any type to String
to set to controls.
Using the code
To demonstrate, we will be using a generic object called Address
. The detailed description of the object is not important here, but we must know that the object has the following properties: AddressID
(Integer
), Address1
(String
), StateID
(Integer
), and CountryID
(Nullable(of Integer)
), and the object can load an Address
record from a database with the AddressID
.
I created three methods:
GetControlValue
(returns a control's value as the specified type).
ConvertFromString
(converts a String
value to any type, if possible).
ConvertToString
(converts a value of any type to String
type, if possible).
The method definitions are given below:
Public Shared Function GetControlValue(Of T)(ByVal cntrl As Control) As T
Dim value As Object = Nothing
Select Case True
Case TypeOf cntrl Is TextBox
value = CType(cntrl, TextBox).Text.Trim
Case TypeOf cntrl Is HiddenField
value = CType(cntrl, HiddenField).Value
Case TypeOf cntrl Is DropDownList
value = CType(cntrl, DropDownList).SelectedValue
Case TypeOf cntrl Is RadioButtonList
value = CType(cntrl, RadioButtonList).SelectedValue
Case TypeOf cntrl Is CheckBox
value = CType(cntrl, CheckBox).Checked
Case TypeOf cntrl Is Label
value = CType(cntrl, Label).Text
End Select
Return ConvertFromString(Of T)(value.ToString())
End Function
Public Shared Function ConvertFromString(Of T)(ByVal value As String) As T
Try
If Not String.IsNullOrEmpty(value) And _
TypeDescriptor.GetConverter(GetType(T)).IsValid(value) Then
Return CType(TypeDescriptor.GetConverter(_
GetType(T)).ConvertFromString(value), T)
End If
Catch ex As Exception
End Try
Return Nothing
End Function
Public Shared Function ConvertToString(Of T)(ByVal value As T) As String
Try
If TypeDescriptor.GetConverter(GetType(T)).CanConvertTo(GetType(String)) Then
Return TypeDescriptor.GetConverter(GetType(T)).ConvertToString(value).Trim
End If
Catch ex As Exception
End Try
Return Nothing
End Function
Getting the control value:
dim address as New Address
address.Address1 = GetControlValue(of String)(me.txtAddress1)
address.StateID = GetControlValue(of Integer)(me.ddlStates)
address.CountryID = GetControlValue(of _
Nullable(of Integer))(me.ddlCountries)
Setting the control value:
dim address as new Address
address.AddressID = 10
address.Load()
me.txtAddress1.Text = ConvertToString(of String)(address.Address1)
me.ddlStates.SelectedValue = ConvertToString(of Integer)(address.StateID)
me.ddlCountries.SelectedValue = _
ConvertToString(of Nullable(of Integer))(address.CountryID)
History
No updates.