Recently im having an issue regarding checking value of column list in workflow. What im planning to do is to check if a certain column has value or null. Certain state will be assigned depending on the value (null or not null). This is the thread that i’ve created to seek for help from professional
Luckily 2 kind gentlemen came to the rescue. Suggesting solution for this issue. At first i don’t see much difference their solutions with mine but after detail explanation and experiment this is what i came up with. This is the original code.
private void checker1(object sender, ConditionalEventArgs e)
string checker2 = workflowProperties.Item["Checker 2"].ToString();
if (checker2 != null)
e.Result = true;
else if (checker2 == null)
e.Result = false;
but when i debug, it says ‘object reference not set to an instance of an object’ although the value is null. After playing for a while that’s when i decided i need to seek for professionals help. Hemendra suggested that i should be using string checker2 = Convert.ToString(workflowProperties.Item[“Checker 2”]) to check if the column value is null or otherwise. When i debug there’s no more error stating that ‘object reference not set to an instance of an object’ but the program still didn’t go as i planned it although the value is null. Then ParthaStarSp suggested me should be using If (workflowProperties.Item[“Checker 2”] == Null). Just remove the string and it does the job. It really bugs me at first. I will quote their comments “There is a difference. When you get string data using ToString() and value is null then you will get the casting error but when you use my code then it will cast the string and avoid the error. If you have any doubt then check and see the result.”-Hemendra, “My suggestion is always to check the object whether its NULL and if it is not NULL then use the object’s value. I recommend this as the best practice… this is what we have been doing for several years...”-PrathaStarSp
I make an initiative to do some research to know the difference between .ToString() and Conver.ToString(). I’ll just copy from this forum.
Lets understand the difference via this example:
123 int i= 0;MessageBox.Show(i.ToString());MessageBox.Show(Convert.ToString(i));
We can convert the integer
Convert.ToString. So what’s the difference?
The basic difference between them is the
Convertfunction handles NULLS while
i.ToString ()does not; it will throw a NULL reference exception error. So as good coding practice using
convertis always safe.
This is just like what Hemendra said about casting error when i use .ToString() and not Convert.ToString(). But why i cannot get the value null and use it as comparison when i’m using Convert.ToString()? This is the answer.
ToString()on an object presumes that the object is not null (since an object needs to exist to call an instance method on it).
Convert.ToString(obj)doesn’t need to presume the object is not null (as it is a static method on the Convert class), but instead will return
String.Emptyif it is null.
There you have it. Convert.ToString() does not return null when it’s empty but instead will return String.Empty if it is null.