Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

I tried the following to null the default 0, but no result. What is wrong in it.

datatype double.

C#
if (exg.CertficateNo == 0)
{
(exg.CertficateNo) = null;
}


Thanks in advance.
Posted
Updated 3-Jul-14 20:59pm
v2
Comments
Sergey Alexandrovich Kryukov 4-Jul-14 3:33am    
Why? I understand why nullify anything on, for example, initialization, but why doing it on condition? Totally wrong idea.
—SA

you can't assign null to double, use nullable double
C#
double? CertficateNo;

then you can set
C#
CertficateNo =null;

read
Nullable Types (C# Programming Guide)[^]
Using Nullable Types (C# Programming Guide)[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Jul-14 3:21am    
This is correct, a 5, but for floating-point types, there is a special, generally better, option.
Please see my answer.
—SA
You may assign null only to nullable data types[^] (as well to reference data types, of course).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Jul-14 3:22am    
This is correct, a 5, but for floating-point types, there is a special, generally better, option.
Please see my answer.
—SA
Yes for this you have to use nullable double type in c#
double? 

and in Entity database model
public System.Nullable<double> CertficateNo{ get; set; } 
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Jul-14 3:22am    
This is correct, a 5, but for floating-point types, there is a special, generally better, option.
Please see my answer.
—SA
use null able datatype while defining the "CertificateNo" property .
i.e. public double? CertificateNo { get; set; }
 
Share this answer
 
Comments
Murugan.NM 4-Jul-14 3:21am    
I need to use formula with this field. if i declare nullable CertificateNo, then how to make formulas.
Sergey Alexandrovich Kryukov 4-Jul-14 3:22am    
This is correct, a 5, but for floating-point types, there is a special, generally better, option.
Please see my answer.
—SA
All the solutions 1 to 4 are correct, but for floating-point types their is one more option which is important to know: NaN (not a number). You can use double.NaN and check as double.IsNaN:
http://msdn.microsoft.com/en-us/library/system.double.aspx[^].

This is the most economic and simple variant for "no data" value; it has many other uses. For further detail, please see:
http://en.wikipedia.org/wiki/Not_a_number[^],
http://en.wikipedia.org/wiki/IEEE_floating_point[^],
http://en.wikipedia.org/wiki/Floating_point[^].

—SA
 
Share this answer
 
Comments
Rahul VB 4-Jul-14 5:17am    
Yes sir. My 5
Andreas Gieriet 4-Jul-14 6:12am    
Hello Sergey,
I agree that the question looks a bit odd.
When it comes to a meta state to tell if a value is given or not, then you have the choice to make the value nullable or to take an arbitrary value from the value range that is never used for that particular field.
For numeric values, you normally go for positive/negative, not zero/zero, min- or max-values, etc. or NaN, +Inf, -Inf.
The important task: whenever you select some value as a sentry, make sure your application can never produce it in any way in the normal mode of operation. For NaN, this can be the result of having NaN as input "value" in a calculation and if dynamically you calculate 0.0/0.0, etc.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900