Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dears,
What is the syntax of assigning a value if the variable is coming null, without using the if statement,

The problem is that
RADentryDate.SelectedDate = DateTime.Parse(row[15].ToString())

sometimes the row[15] is DBNULL

The problem is that when it is null, an error occurs. so i want to assign a default date when the value comes null

Regards
Posted
Updated 27-Apr-11 8:49am
v3
Comments
Michael Waguih 27-Apr-11 6:58am    
Can you put the code where you want to assign this and what have you tried ?
Ankit Rajput 27-Apr-11 7:24am    
Every body is giving the same answer.
But still we all dont the exact problem.
Can you please describe your problem?

If you are talking about getting values from a database that might be null you could do something like this

C#
string name = datareader.IsDBNull(0) ? "Unknown Name" : datareader.GetString(0);


Otherwise you need to elaborate your question some more.
 
Share this answer
 
Comments
Olivier Levrey 27-Apr-11 7:36am    
Good answer. My 5.
I don't think you can escape from if, but you can use the shorthand syntax for it:
// maybeNull is the passed parameter - if it's not null it will be assigned to someVariable, else use valueToAssignIfNull
var someVariable = maybeNull ?? valueToAssignIfNull;
 
Share this answer
 
Comments
Olivier Levrey 27-Apr-11 7:36am    
Good answer. Have a 5.
Sergey Alexandrovich Kryukov 27-Apr-11 21:38pm    
My 5, proposed as answer.
--SA
I am no c# pro but I'd try:

C#
RADentryDate.SelectedDate = !DBNull.Value.Equals(row[15]) ? DateTime.Parse(row[15].ToString()) : DateTime.Now;
 
Share this answer
 
v3
The solution is pretty simple as solution 2 says,

you may use null coal-easing operator (??) here.

Write something like this to assign default value, if current value comes null.
(??) operator will allow you to assign default value if it comes null and takes it self if it is not null.

RADentryDate.SelectedDate = DateTime.Parse(row[15] ?? DateTime.Now);

Is this an answer to your problem?

Thanks :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Apr-11 21:39pm    
Correct, a 5.
--SA
Sanjay J Patolia 28-Apr-11 5:48am    
Thank you very much :)
Easiest way....
You have to declare the variable in the following manner:
string variableName = "";


And you're through!!.

;-)
 
Share this answer
 
Comments
Kim Togo 27-Apr-11 7:38am    
It is better to use:

string variableName = string.Empty;

It is much easier to read.
Sergey Alexandrovich Kryukov 27-Apr-11 21:41pm    
Correct, Kim. Not only readability; you may want to get rid of all immediate constants, extermination of all " is a way to go. Always should be string.Empty.
--SA
well you can use below line instead of if-else ...
(Control_id.value=null) ? return "Your value" : return Control_id.value


or you can use method given by Michael,

(Control_id.Equals(null))? return "the value you want" : return Control_id.Value


Above method is called Ternary operator {"Condition ? Result1 : Result2"}
 
Share this answer
 
Comments
Olivier Levrey 27-Apr-11 7:35am    
This is will not compile. You can't use the return keyword on the right side of ?. However, this works: return test ? value1 : value2;.

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