Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
2.46/5 (4 votes)
See more:
Hi,

I am initializing double type as null in my program

C#
double utilization1=null;
            double utilization2=null;
            double utilization3=null;
            double utilization4 = null;



And I am using a condition as below in my program

C#
if ((utilization1 != null) && (utilization2!=null)&&(utilization3 != null) && (utilization4!=null))
           {

            //do something
           }


I am getting error like below

C#
Cannot convert null to 'double' because it is a non-nullable value type	



Can anyone help me in solving this problem
Posted
Comments
Richard MacCutchan 18-Feb-14 10:40am    
Initialise the values to zero.
Sergey Alexandrovich Kryukov 18-Feb-14 13:04pm    
Not a good idea. Something like NaN is usually more appropriate.
—SA
Krunal Rohit 18-Feb-14 10:44am    
Ignore the following advice, it's complete garbage:

Try this,
double x;
x = double.Nan;
or x = null;

You'll find that the value types are not nullable types by default (reference types are). To make it a nullable type, you change the way you declare double to be double?. Notice the question mark after the type declaration - this is the shorthand way of defining a type as nullable. What this is actually doing is the equivalent of using the System.Nullable<double> generic to create a nullable type of double.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 13:10pm    
Voted 4. You are missing the possibility specific to floating-point types: using NaN, which is more light weight and more adequate in most cases. Please see Solution 6.
—SA
'double' is value type and value type cannot contain the null value.
If you want initialize variable with null value then you should use 'double?' instead of double.

double? utilization1=null;

if (utilization1.hasvalue)
{
//do something
}
 
Share this answer
 
Comments
agent_kruger 18-Feb-14 11:02am    
[Not an answer] sir, i just want to ask from were have you learnt this? did you search on internet or learnt it from somewhere.
Sergey Alexandrovich Kryukov 18-Feb-14 13:12pm    
If you are so ignorant and helpless, don't think that others are like that. These are way too elementary facts.
And let me ask you, why do you think this is not an answer? And is it why you voted 1 everywhere? Why?
—SA
agent_kruger 18-Feb-14 14:03pm    
no sir, i said to my comment that it is off topic and not an answer.
Pete O'Hanlon 18-Feb-14 16:29pm    
Why do you think it's off topic and not an answer? This answer has explained that value types cannot contain null, and it has shown how to write the code so that a value type can be null. You do know that a double is a value type don't you? So, I'm sorry, but you are wrong and you need to learn more about .NET before you start downvoting people.
Sergey Alexandrovich Kryukov 18-Feb-14 17:58pm    
Agree. Why would OP say "no sir"? I say that OP indicated it's "[Not an answer]" and this is not true. The answer does provide the mechanism to indicate "no initialized" value...
—SA
In addition to using nullable double?, you can also use NaN (Not a Number):
http://msdn.microsoft.com/en-us/library/system.double.nan(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.double.isnan(v=vs.110).aspx[^],
see also http://msdn.microsoft.com/en-us/library/system.double%28v=vs.110%29.aspx[^].

In most cases, this is the most adequate approach, lightweight compared to using the nullable type.

—SA
 
Share this answer
 
v2
Use Nullable Types[^].
C#
int? i = 10;
double? d1 = 3.14;
bool? flag = null;
char? letter = 'a';

-KR
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 13:15pm    
Voted 4. You are missing the possibility specific to floating-point types: using NaN, which is more light weight and more adequate in most cases. Please see Solution 6.
(Two votes of 1 are just amazing though. Who is that "clever"? :-)
—SA
Because it's a Value Type[^]

If you want it to be null you have to define it this way:
C#
Nullable<double> utilization1 = null;

if ((utilization1.HasValue)
{
  //Do Something with utilization1.Value
}

</double>


Hope it helps
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 13:10pm    
Voted 4. You are missing the possibility specific to floating-point types: using NaN, which is more light weight and more adequate in most cases. Please see Solution 6.
—SA
_Zorro_ 19-Feb-14 8:04am    
Thanks for pointing this out. Never used that, I'll keep that in mind.
Sergey Alexandrovich Kryukov 19-Feb-14 10:40am    
Sure.
—SA
Because System.Double is a value-type, not a reference-type; so it cannot be null. Better initialize it to zero.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 13:14pm    
First part it true, the rest of it is wrong. Please see other answer. Zero is probably the worst idea. How can you tell the difference between uninitialized 0 and 0 which is normal data? It's much better to use NaN.
—SA

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