tags:

views:

164

answers:

5

Visual studio is yelling at me, in the database it is a float, and in my class it is a double. Why can't I assign it and why does it think it's a 'double?' ?

LINE THREE BELOW

    Confirmation confirm = new Confirmation();

    confirm.order = theOrder;

    confirm.totalPrice = theOrder.BillingAmount;

HERE IS Confirmation DEF

    public class Confirmation
    {
        public Order order;

        public List<OrderItem> allProducts;

        public double totalPrice;
    }

HERE IS BillingAmount DEF from code I think generated from .dbml draggy-droppy...

[Column(Storage="_BillingAmount", DbType="Float")]
     public System.Nullable<double> BillingAmount
     {
      get
      {
       return this._BillingAmount;
      }
      set
      {
       if ((this._BillingAmount != value))
       {
        this.OnBillingAmountChanging(value);
        this.SendPropertyChanging();
        this._BillingAmount = value;
        this.SendPropertyChanged("BillingAmount");
        this.OnBillingAmountChanged();
       }
      }
     }
A: 

A double? is a nullable version of a double that can represent an "unknown" value, as well as a definitive double. The difference between a double and a float is that a double has twice the precision and rtange that a float has.

So you should be able to implicitly convert any float into a double, but not the other way around, Just like you can convert any short integer ( -32,768 to +32769) into a long ( - 2 Billion to + 2 billion), bit not the other way around.

EDIT: So you can also implicitly convert a double into a nullable double (double?) but not the other way around (it might be null).

But you can still convert a nullable double Explicitly. if it has a definite value, then you can convert it by casting it, or by accessing the "Value" property of the double?

   double? nulbleDbl = 123.456;
   double?   nullDbl = null;
   double x = nulbleDbl.Value;  //works just fine
   double y = nullDbl.Value;  // fails with convert error or cast error
   bool isOk = nullDbl.HasValue;  // returns false
Charles Bretana
so should I make it a nullable one in my other object I want to use it in or is there a good way to convert?
shogun
@Ryan, if it's a nullable in one, and ity has a definite value, then you can EXPLICITLY conver it by casting it, or by accessing the "Value" property of the double?
Charles Bretana
+2  A: 

double? is shorthand for Nullable<double>. It's a completely different type. My recollection is that you can implicitly convert between them, but only in one direction (from double to double?). The other direction doesn't make sense, because in the case where the double? is null, no conversion would exist. The reason the database code uses a nullable double is because, in the database, the value could possibly be NULL.

Joel Coehoorn
+5  A: 

In the db, if your column is nullable, this is represented in C# as Nullable<double>. double, being a value type, can not be null...but in db terms this is perfectly legal. That's why .Net has the Nullable<T> type. You can assign its value to a double by doing any of the following

double d = nullableDouble ?? 0;
double d = (nullableDouble.HasValue) ? nullableDouble.Value : 0;

etc...

Rich
+1  A: 

The way to convert a double? to a double is:

double? nullDouble = null;
double normaDouble = nullDouble ?? 0; 
  // where 0 is the default value used if nullDouble is null

this is the same as:

normalDouble = (nullDouble.HasValue) ? nullDouble.Value : 0;

or normalDouble = (nullDouble != null) ? nullDouble.Value : 0;

Phillip Ngan
omg... that '??' what does that do, if it is what I think it is you have made me very happy (and feel powerful)
shogun
It's the null coalescing operator. It means "use the left hand side, unless it is null, in which case, use the right hand side". The type coercion rules are a bit complicated; see the specification if you need details.
Eric Lippert
A: 

Your column in the DB must be Allowing Nulls, because a double? was generated. So if it can be null, it means it is possible that it does not get a value from your program. So maybe you should choose between double? or double, but to work with that everywhere (and maybe not allowing nulls in the DB).

devMomentum