User1122355199 posted
Hello everyone and thanks for your help in advance. I have inherited some code that I now need to debug. At the heart of the problem is the handling of null values within a class. Here is a code sample:
Private mstrModifier1 As String
Public Property Modifier1() As String
Get
Return mstrModifier1
End Get
Set(ByVal value As String)
mstrModifier1 = value
End Set
End Property
The value is set by parsing a text string:
If Len(CurrentLineItem.CPTCode) > 10 Then
'Get the Modifier 1
CurrentLineItem.Modifier1 = Mid(CurrentLineItem.CPTCode, 10, 2)
End If
In other words, the current line may not always set to a value. Since the variable is not initialized to a value when declared, I think the variable may be set to null. In these cases, when I try to write to a database using:
sqlComm.Parameters.AddWithValue("mod1", CurrentLineItem.Modifier1)
I receive an error Procedure or function 'sproc' expects parameter
'@mod1', which was not supplied. The column is set up accept null values. So I'm not exactly sure of the best way to fix this problem. I know I can set the value of the variable of the string to "" at the declaration. Or I could
check to see if the value is set and if not, set it to null or set up the parameter as nullable. I guess I'm looking at guidance on what the best practice would be. Any help would be appreciated.