My problem is this (apologies if this is a little long ... hang in there):
I can define a function in VB.NET with optional parameters that wraps a SQL procedure:
And I can test for a missing argument by testing for the default I set:
Great, so in principle I can leave off unspecified optional parameters when calling the stored procedure and let the stored procedure default those parameters based on whatever defaults are specified in the stored procedure.
But hang on, what if my choice of default for the optional parameter is a valid value to send to the stored procedure? What if Arg1 can be 0 and that is different to the default set on the stored procedure?
Maybe I could use "Nothing" as my default for the optional parameter, and test using IsNothing. This works for complex types like String and any Objects. But fails for simple types like Integer, as setting an Integer to Nothing actually sets it to the value 0 and a subsequent call to IsNothing returns False:
Okay, so it looks like I need to replicate all the default values I have on the stored procedure into the wrapper VB.NET function. This is unfortunate as it duplicates code and leaves space for the two to become out of sync.
I want to be able to write something like:
And test for this special "Default" value on any type and not pass the stored proc parameter if this is set.
I have tried defining Structures that attempt to extend the base types (a bit like
those is System.Data.Sql Types) but then I hit the problem that Optional parameters cannot be Structures (only classes or simple types).
I have also considered using Overloads rather than Optional parameters, but this doesn't work either as it seems you cannot replicate the same functionality you have with Optional parameters with Overloads. Considering my original function:
I could re-write this using overloads as:
But I cannot do:
As this has the same signature as one of the previous versions. So although I can happily do this using optional parameters:
I cannot seem to achieve the same with an overload.
Can anyone think up a way of setting an optional parameter to some value I can detect as missing that is distinct from any default I may have set in the stored procedure that function wraps?
Thanks
I can define a function in VB.NET with optional parameters that wraps a SQL procedure:
Code:
Sub Test(Optional ByVal Arg1 As Integer = 0, _
Optional ByVal Arg2 As Integer = 0, _
Optional ByVal Arg3 As Integer = 0)
' Call my SQL proc with the same signature
End Sub
Code:
If (Arg1 = 0) Then
' Arg1 is missing so leave it off the call to the stored proc
Else
' Arg1 is provided so add it to the call to the stored proc
End If
But hang on, what if my choice of default for the optional parameter is a valid value to send to the stored procedure? What if Arg1 can be 0 and that is different to the default set on the stored procedure?
Maybe I could use "Nothing" as my default for the optional parameter, and test using IsNothing. This works for complex types like String and any Objects. But fails for simple types like Integer, as setting an Integer to Nothing actually sets it to the value 0 and a subsequent call to IsNothing returns False:
Code:
Dim i As Integer
i = Nothing
If (IsNothing(i)) Then
' this arm is never executed
Else
' this will print 0
Debug.Print(i)
End If
I want to be able to write something like:
Code:
Sub Test(Optional ByVal Arg1 As Integer = Default, _
Optional ByVal Arg2 As Integer = Default, _
Optional ByVal Arg3 As Integer = Default)
' Call my SQL proc with the same signature
End Sub
I have tried defining Structures that attempt to extend the base types (a bit like
those is System.Data.Sql Types) but then I hit the problem that Optional parameters cannot be Structures (only classes or simple types).
I have also considered using Overloads rather than Optional parameters, but this doesn't work either as it seems you cannot replicate the same functionality you have with Optional parameters with Overloads. Considering my original function:
Code:
Sub Test(Optional ByVal Arg1 As Integer = 0, _
Optional ByVal Arg2 As Integer = 0, _
Optional ByVal Arg3 As Integer = 0)
Code:
Sub Test(ByVal Arg1 As Integer, _
ByVal Arg2 As Integer, _
ByVal Arg3 As Integer)
End Sub
Sub Test(ByVal Arg1 As Integer, _
ByVal Arg2 As Integer)
End Sub
Sub Test(ByVal Arg1 As Integer)
End Sub
Code:
Sub Test(ByVal Arg2 As Integer, _
ByVal Arg3 As Integer)
End Sub
Code:
Test(Arg2:=3,Arg3:=1)
Can anyone think up a way of setting an optional parameter to some value I can detect as missing that is distinct from any default I may have set in the stored procedure that function wraps?
Thanks
Comment