Invalid Cast on field

919
9
10-10-2012 11:56 AM
KevinYanuk
Occasional Contributor
Hello;

I am trying to compare a double field:

if ((double)_feature.get_Value(_feature.Fields.FindField("MYFIELD")) > 8.33)


And I am getting an invalid cast error.

I used the same logic before in my code, and I can't quite figure out why this is not letting me do this.


Thanks
0 Kudos
9 Replies
GeorgeFaraj
Occasional Contributor III
What do you get when you do this?

_feature.get_Value(_feature.Fields.FindField("MYFIELD")).ToString()
0 Kudos
AlexanderGray
Occasional Contributor III
If the value in the field is null, you will not be able to cast it to double.  When there is a null value in a field, .net returns a dbnull.value which cannot be cast to a double even if the field type is double.  Perhaps this is the problem.
0 Kudos
KevinYanuk
Occasional Contributor
What do you get when you do this?

_feature.get_Value(_feature.Fields.FindField("MYFIELD")).ToString()


cannot perform the operator on "object" and "double"


If the value in the field is null, you will not be able to cast it to double.  When there is a null value in a field, .net returns a dbnull.value which cannot be cast to a double even if the field type is double.  Perhaps this is the problem.


Ah, I believe this is probably the case.  I will have to check tomorrow, but I'm pretty sure this is my problem.  Thanks!
0 Kudos
GeorgeFaraj
Occasional Contributor III
cannot perform the operator on "object" and "double"


Sure you can call ToString() on objects and doubles.
0 Kudos
AlexanderGray
Occasional Contributor III
ToString will fail if the object is dbnull value.  Happens all the time, you have to check for that every time, it is a real pain.  If the type returned was a AO type, I would write an extension to check for that but I hesitate on writing extensions for type object.
0 Kudos
GeorgeFaraj
Occasional Contributor III
ToString will fail if the object is dbnull value.  Happens all the time, you have to check for that every time, it is a real pain.  If the type returned was a AO type, I would write an extension to check for that but I hesitate on writing extensions for type object.


Indeed. You should already be checking for null values before calling any methods on it.
0 Kudos
NeilClemmons
Regular Contributor III
A simple solution to this problem is to create a class for reading and writing values to database fields.  Put the class into a common code library that you can reference in all of your projects.

This is how you might write a method on that class for reading doubles:

        Public Shared Function DatabaseToDouble(ByVal value As Object) As Double
            If value Is DBNull.Value Then
                Return 0
            Else
                Return Convert.ToDouble(value)
            End If
        End Function


Here's how you would call it:
Dim d As Double = whateverYouNamedThatClass.DatabaseToDouble(feature.Value(fieldIndex))

You would add a method to the class for each data type you need (string, int, boolean, etc).
0 Kudos
AlexanderGray
Occasional Contributor III
The code assumes that nulls are to be treated the same as zero in all cases.  That is a decision you have to make to start with.  In my work, there are cases where that assumption is incorrect so that is something to think about.  You may also return Double.NaN but then you still have to check it before using it.

Putting a not null constraint on the field with a default value of zero would avoid this problem to start with if you have that sort of control over the database.
0 Kudos
NeilClemmons
Regular Contributor III
All you need to do is overload the method to accept a default value that allows you to specify the value you want returned in cases where the database value is null.

        Public Shared Function DatabaseToDouble(ByVal value As Object, ByVal defaultValue As Double) As Double
            If value Is DBNull.Value Then
                Return defaultValue
            Else
                Return Convert.ToDouble(value)
            End If
        End Function
0 Kudos