Custom Control With SelectedValue Property - How to determine correct "default" data type to return?

I have a custom control that has a SelectedValue property.  It is basically a ComboBox control, if that helps.  Anyway, I have a business object that I'm binding to this control.  In some cases, the business object property that I'm binding to the control is a System.GUID, in other cases, it is a System.Int32 or System.String. 

My delimea is that if the custom control's internal _selectedValue member is Nothing, because the user never selected a value or the selection was cleared, I want to return a valid data type to my custom business object.

So if I'm binding to a property on my business object that is a System.GUID and _selectedValue is Nothing, then I want to return System.GUID.Empty.

System.Int32 and _selectedValue Is Nothing, then 0.
System.String and _selectedValue Is Nothing, then System.String.Empty.

I'll be fine with hardcoding the "default null value" into my custom control based on a given data type, but don't know how to implement this.  It would look something like this..


Get
   Select Case "Custom Business Object Property Type"
      Case System.GUID
         Return System.GUID.Empty
         Case System.Int32
            Return 0
         Case System.String
            Return System.String.Empty
         Case System.DateTime
            Return System.DateTime.MinValue
   End Select
End Get

I'm not sure how to detect which default value to pass back when the _selectedValue is Nothing.  Any help is appreciated.  Thanks!

Greg


'================================================
'Code from the form where I'm using the custom control and business object
'================================================
'Private member variable for form
Private _myCustomBusinessObject as New MyCustomBusinessObject()

Public Sub Form_Load....
   'Load Data Into Custom Business Object
   _myCustomBusinessObject.Load(1)
   'Bind Custom Control's SelectedValue to _myCustomBusinessObject.PersionGUID
   myCustomControl.DataBindings.Add("SelectedValue", _myCustomBusinessObject,
"PersonGUID")

End Sub


'================================================
'Code from the custom control
'================================================
'Private member variable for custom control
Private _selectedValue As System.Object = Nothing

'Custom Control SelectedValue Property
Public Property SelectedValue() As System.Object
   Get
      Return _selectedValue
   End Get
   Set (ByVal Value as System.Object)
      'Is this value valid, if so set member variable
      If ValidValue(Value) Then
         _selectedValue = Value
      Else
         _selectedValue = Nothing
      End If
   End Get
End Property


'================================================
'Code from the custom business object
'================================================
'Private member variable for custom business object
Private _personGUID as System.GUID = System.GUID.Empty

'PersonGUID Public Property
Public Property PersonGUID() As System.GUID
   Get
      Return _personGUID
   End Get
   Set (ByVal Value as System.GUID)
      _personGUID = Value
   End Set
End Property










Answer this question

Custom Control With SelectedValue Property - How to determine correct "default" data type to return?

  • FloridaAviator

    If I've understand you, you need to know the type before the return

    in that case:



    if (o is int)
    {
       // maybe you should return int.MaxValue/MinValue
       return 0;
    }

    if (o is string)
    {
       return string.empty;
    }

    if (o is GUID)
    {
    return GUID.empty;
    }


     



    Hope this is what you have meant.

  • Bjornar

    Thanks for the post.  Yeah that is what I was wanting to do, but I needed to know the data type that was bound to the control.  I resolved it using Reflection to get information about the property the control was bound to.

    Dim x As Type = Me.DataBindings.Item("SelectedValue").DataSource.GetType

    Dim y As System.Reflection.PropertyInfo = x.GetProperty(Me.DataBindings.Item("SelectedValue").BindingMemberInfo.BindingField)

    Select Case y.PropertyType.FullName.ToLower
       
    Case "system.guid"
          Return System.Guid.Empty
       Case "system.datetime"
          Return System.DateTime.MinValue
       Case "system.boolean"
          Return False
       Case "system.string"
          Return System.String.Empty
       Case Else
          Return 0
    E
    nd Select


  • Custom Control With SelectedValue Property - How to determine correct "default" data type to return?