GetType of custom Property set to nothing

  • Thread starter Thread starter Nicolas
  • Start date Start date
N

Nicolas

I want to return the proper Null to the SQL Database if my property is
nothing
Also I thought about creating a function so I don't have to put so many if
statement for each property for each classes etc.
However if I call ReturnValue(myClass.CompanyName) and CompanyName is
Nothing then it crash where it shoud return SqlTypes.SqlString.Null


Private _CompanyName As String


<ComponentModel.Browsable(True), ComponentModel.Description("CompanyName"),
ComponentModel.ReadOnly(False)> _
Public Property CompanyName() As String Implements iCompany.CompanyName
Get
Return _CompanyName
End Get

Set(ByVal Value As String)
Me._CompanyName = Value
End Set
End Property

Function ReturnValue(ByVal thisObject As Object) As Object
Try
If IsNothing(thisObject) Then

""""" CRASH HERE """ Because thisObject is nothing so I cannot get the type
Select Case thisObject.GetType.ToString
Case GetType(System.String).ToString
Return SqlTypes.SqlString.Null
Case GetType(System.Boolean).ToString
Return SqlTypes.SqlBoolean.Null
Case Else
Return SqlTypes.SqlString.Null
End Select
Else
Return thisObject
End If

Catch ex As Exception
Return SqlTypes.SqlString.Null
End Try
End Function



Thanks for the help

Nicolas
 
Function ReturnValue(ByVal thisObject As Object) As Object
Try
If IsNothing(thisObject) Then

""""" CRASH HERE """ Because thisObject is nothing so I cannot get the
type
Select Case thisObject.GetType.ToString
Case GetType(System.String).ToString
Return SqlTypes.SqlString.Null
Case GetType(System.Boolean).ToString
Return SqlTypes.SqlBoolean.Null
Case Else
Return SqlTypes.SqlString.Null
End Select
Else
Return thisObject
End If

Catch ex As Exception
Return SqlTypes.SqlString.Null
End Try
End Function
Nick,

If you want to return null if the object is nothing, try:

Function ReturnValue(ByVal Value As Object) As Object
If Value Is Nothing
Return DBNull.Value
Else
Return Value
End If
End Function


HTH,
Mythran
 
Back
Top