
CType(), Bug or Feature??
When casting an object using CType, if the original object Is Nothing, CType
executes the default constructor of the converted object, instead of
returning Nothing.
For instance:
<CODE>
Dim iFieldValue as Integer
iFieldValue = CType(oADOCmd.ExecuteScalar(), Integer)
If iFieldValue Is Nothing Then
'Unimportant, as this code will never execute
ElseIf iFieldValue = 0 Then
'This code runs if the scalar SQL command returns either 0 or NULL
Else
'This code runs if the scalar SQL command returns a value other than 0
or NULL
End If
</CODE>
Right now, I have to do this:
<CODE>
Dim iFieldValue as Integer
If oADOCmd.ExecuteScalar() Is Nothing Then
'NULL value
Else
iFieldValue = CType(oADOCmd.ExecuteScalar(), Integer)
If iFieldValue = 0 Then
'This code runs if the scalar SQL command returns either 0 or NULL
Else
'This code runs if the scalar SQL command returns a value other than
0 or NULL
End If
End If
</CODE>
I don't see how this can possibly be desirable. This requires two trips to
the database to retrieve the same information. Very bad if the scalar
function has a high overhead.
I know that I can declare a variable as type Object, and work with that, but
that seems like going to L.A. via Omaha. And I'm not fond of having Objects
in my code, any more than I liked having Variants in VB6.
--
Toby Herring
Software Architects, Inc.
MCDBA, MCSD