Link to home
Start Free TrialLog in
Avatar of mainrotor
mainrotor

asked on

How to check for a null string in C#.Net

Hi Experts,
I need help checking for null values in my C#.Net program.

Here is a sample of my code, how would I check for null values in order to prevent my program from crashing?

SAMPLE CODE:
PatientLName = reject.PatientLName.ToString();

Open in new window


Thank you very much in advance,
mrotor
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

You can use

Convert.ToString(reject.PatientLName)

or

string.ISNULLorEmpty(reject.PatientLName)
Avatar of John Tsioumpris
I use the
string.Length>0

Open in new window

to determine if the string is null or empty
John, if the string is null, querying the Length property will trigger an error!
ASKER CERTIFIED SOLUTION
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't like the idea of defaulting a string to empty instead of null.  Null is easy to test for, and an empty string obfuscates whether the property has been set or not.  How can you tell if the value is "" or whether it has not been set if the default is empty string?

It's better to not set a default value.
I don't like the idea of defaulting a string to empty instead of null.
in my opinion it makes absolutely no sense to test each string member for being null. this check would be necessary for any access to that member and would blow up your code for no advantage. same as in a real print form string members should be empty if they don't have a value what easily could be checked if necessary (and only there).

How can you tell if the value is "" or whether it has not been set if the default is empty string?
why did you want to know that difference?  if you initialize all variables with a valid value, an empty string variable means that it has no value. that is much better than for other types where you either have to define your own 'no value' or have to test for null, even if you simply want to call ToString() function.

Sara
to determine if the string is null or empty
How does that tell you the string is null? Do you explicitly catch an exception? If so, that seems awfully expensive.

How can you tell if the value is "" or whether it has not been set if the default is empty string?
I could argue the same about using null, since you can assign a variable null after declaration.

Lastly, if you are using C#6 or higher, you can do this on your reject class property:
If you're using C# 6, then why not just use the null conditional operator?

in my opinion it makes absolutely no sense to test each string member for being null.
I'd say that depends on your team's convention.
I could argue the same about using null, since you can assign a variable null after declaration.

I think you're missing the point (or maybe I wasn't clear).  If you set a string to null, you are intentionally stating that it has no value.  You then know it has no value.  If the default is empty string, you have no idea if the string has a value that's empty, or whether it has no value at all.
Perhaps you are missing mine. The fact that null means something has no value is effectively a convention. The creators of the language could have easily said that empty string means no value. But the compiler defaults to null. That said, I can still just as easily treat empty string as meaning the value wasn't set. Or I can do the same with null. I'm not saying it's right or that it makes complete sense, but I could do it.
Just 2 add my 2 cents I would argue that String.Empty is a valid value versus null which is to say there is a lack of a defined value.  Of course it's all up to individual conventions such as using -1/1/0 in an int field to represent no value / true / false  versus null / 1 / 0 in a bit field.
@Author -

Have you tried my first comment - Both the options would have worked for you.

Thanks
@Pawan, not if reject itself is null.
@kyle - meaning?
Kyle means that your statement

String.IsNullorEmpty(reject.PatientLName) 

Open in new window


would throw an exception if reject is null,

it would not throw an exception if reject.PatientName was null.

if you were using IsNullOrEmpty your code makes no difference between null or empty what is a pro for the argument that making a difference is a useless and inefficient complication.

i am a c/c++ programmer where null objects can be defined by using pointers. however, the usage of pointers at places where it is not necessary is looked on as bad programming and this applies especially for pointer members.

Sara
@Kyle - Question is to check for reject.PatientLName not for reject.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My comment and Dirk's comment has completely answers the question.
It is better to delete since the author has not responded to any of the comment.
All comments have been valid regardless whether you share the opinion or not. Why should a valuable discussion regarding the difference between null and empty deleted. The Author has got enough good and working code for to resolve their problem.

An assist doesn't need to contain a complete solution. Kyle's answer is the only one which provides a full answer to the original post.

Sara