Reputation: 43813
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();
}
}
}
Upvotes: 1
Views: 8852
Reputation: 146499
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 range 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
Upvotes: 1
Reputation: 36806
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...
Upvotes: 7
Reputation: 443
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).
Upvotes: 0
Reputation: 16106
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;
Upvotes: 1
Reputation: 415725
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.
Upvotes: 2