Reputation: 5738
If objects contains null or empty then how to validate or check the condition for the same?
How to bool check whether object obj is null
or Empty
I've code as follows:
class Program
{
static void Main(string[] args)
{
object obj = null;
double d = Convert.ToDouble(string.IsNullOrEmpty(obj.ToString()) ? 0.0 : obj);
Console.WriteLine(d.ToString());
}
}
With this code i'm getting NullReference Exception
as Object reference not set to an instance of an object.
Pls help.
Here I'm not getting....
How to validate whether that object is null
or Empty
without converting into .ToString() ??
Is there an approach to check the same??
Upvotes: 9
Views: 80003
Reputation: 1157
Following code could be a safer way of achieving it.
if(obj != null && !string.IsNullOrEmpty(obj.ToString()))
{
}
This code saves us from object being a non-string type.
Upvotes: 0
Reputation: 195
class Program
{
static void Main(string[] args)
{
object obj = DBNull.Value;
if(obj != DBNull.Value) {
double d = Convert.ToDouble(obj);
Console.WriteLine(d.ToString());
}
}
}
Upvotes: 2
Reputation: 1904
You can use the ??
operator. It is known as the null-coalescing operator.
Upvotes: 1
Reputation: 5129
You are getting the null reference because you are executing obj.ToString()
which will return obj's ToString() method return value. Problem is that in the previous line you set obj to null so you will get an object reference not... error
To make your code work you need to do:
//This will check if it's a null and then it will return 0.0 otherwise it will return your obj.
double d = Convert.ToDouble(obj ?? 0.0);
Your code as it is now however will always be 0.0
Without null coalescing: (??)
double d = Convert.ToDouble(obj ? 0.0 : obj);
EDIT
If I understand correctly from the comments you want to know if the object is null or an empty string. You can do this by casting it to string first instead of calling the ToString method which does something entirely different:
string objString = (obj as string);
double d = Convert.ToDouble(string.IsNullOrEmpty(objString) ? "0.0" : objString);
Upvotes: 3
Reputation: 6683
The problem that you are running into is that your object is of type, well, object. In order to evaluate it with string.IsNullOrEmpty, you should pass your object in with the cast to (string)
like so:
static void Main(string[] args)
{
object obj = null;
double d = Convert.ToDouble(string.IsNullOrEmpty((string)obj) ? 0.0 : obj);
Console.WriteLine(d.ToString());
}
This will work fine since you are not explicitly calling .ToString on your (nonexistent) object.
Upvotes: 13
Reputation: 8513
It sounds like what you want to do is this:
object obj = null;
double d;
if (!double.TryParse(Convert.ToString(obj), out d))
{
d = 0.0;
}
But the question does not make a lot of sense.
Upvotes: 1
Reputation: 14478
You shouldn't be a bit surprised that you get a NullReferenceException
with this code. The offending part is
obj.ToString()
If you wrote
object obj = null;
string s = obj.ToString();
you would expect a NullReferenceException
. Since the call to ToString
occurs before the call to string.IsNullOrEmpty
, the exception is thrown before there is a check for a null or empty string.
Upvotes: 0