Click here to Skip to main content
15,885,078 members
Articles / Programming Languages / Visual Basic

Get any control's value in any type

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
6 Jul 2009CPOL1 min read 13K   5  
Gey any control's value in the type specified.

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:

VB
''' <summary>
''' Returns a Control's value in the specified Type.
''' </summary>
''' <typeparam name="T">Return value type.</typeparam>
''' <param name="cntrl"><see 
'''      cref="Control">Control</see>
'''     to get the value from</param>
''' <returns>Value as T</returns>
''' <remarks>
''' <para>Returns Text property value for <see 
'''    cref="TextBox">Textbox</see> and 
'''    <see cref="Label">Label</see></para>
''' <para>Returns Value property value for
'''    <see cref="HiddenField">HiddenField</see></para>
''' <para>Returns SelectedValue property value for
'''    <see cref="DropdownList">DropdownLis</see>
'''    and <see cref="RadiobuttonList">RadiobuttonList</see>.
'''    If default Item is selected returns NULL</para>
''' <para>Returns Checked property value for
'''   <see cref="CheckBox">CheckBox</see></para>
''' </remarks>

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

''' <summary>
''' Converts a string value to the specified type
''' </summary>
''' <typeparam name="T">Type to convert to</typeparam>
''' <param name="value">String value to convert</param>
''' <returns>Value as T</returns>
''' <remarks>
''' <para>If control value is not an empty string and
''' the if the value can be converted to the specified return type, 
''' Converts and returns the value as the specified type.</para>
''' <para>If control value is an empty string, returns Nothing</para>
''' </remarks>
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

''' <summary>
''' Converts and Returns <see cref="String">String</see>
''' representation of a value of any Type.
''' </summary>
''' <typeparam name="T">Value Type</typeparam>
''' <param name="value">Value to convert</param>
''' <returns>Value as <see cref="String">String</see></returns>
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:

VB
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:

VB
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --