CType(), Bug or Feature?? 
Author Message
 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



Mon, 31 Jan 2005 03:57:34 GMT  
 CType(), Bug or Feature??
Integer variable cannot be nothing  Integer set to nothing is 0

When you take nothing and convert it to Integer it returns 0

Olafur


Quote:
> 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



Mon, 31 Jan 2005 20:36:27 GMT  
 CType(), Bug or Feature??
You can do it like this

Dim oRetValue as Object
Dim iFieldValue as Integer
oRetValue = oADOCmd.ExecuteScalar()

If oRetValue Is Nothing Then
...
Else
  iFieldValue = ctype(oRetValue, Integer)
....
End If


Quote:
> 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



Mon, 31 Jan 2005 20:39:10 GMT  
 CType(), Bug or Feature??

Quote:
> 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.

Casting Nothing to a value type ist the same as assigning Nothing to a value
type. As the help for the Nothing keyword says, assigning Nothing to a value
type means to set it's default value.

You use ExecuteScalar and it returns an Object, so you have to deal with it
and can't ignore this fact. Especially because you want to distinguish
between Nothing and 0. My code would look like this:

Dim ESResult As Object

ESResult = oADOCmd.ExecuteScalar()

If ESResult Is Nothing Then
    'NULL value
Else
    Dim iFieldValue as Integer
    iFieldValue = CType(ESResult, 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

Armin



Mon, 31 Jan 2005 18:23:49 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. DoCmd.Close (Bug or Feature?)

2. ACCESS 95/97 BUG/FEATURE, CAN YOU HELP ?

3. Word MailMerge - a bug or a feature

4. bug or feature with expression columns

5. Bug or Feature?

6. SQL Query Feature/Bug ?

7. Windows bug or feature?

8. Free online bug report / feature enhancement tracking

9. VB6 IDE Bug/Feature?

10. VB5 SP3 bug or feature?

11. RichTextBox BUG or feature?

12. VB5 Enterprise install Bug/Feature???

 

 
Powered by phpBB® Forum Software