Reputation: 25
I have an object with string
properties, for example, a Patient object with weight and height.
But when the property is null, my code fails when I try to use it, as the property is set to null. I'm trying to create a function that checks if the string/property is null, and if so, set the property to ""
.
I know that I can do this:
if(string.isNullOrEmpty(patient.weight)) patient.weight = "";
But I need the code to be as clean as possible, and I have a lot of properties, so I don't want to manually check each one. Ideally, I'd like to have a function that can take the string, (without failing even if it is null), and simply return either the value if it's not null, or a ""
if it is null.
Can anyone give me a clue on this?
Upvotes: 1
Views: 5742
Reputation: 391456
Personally I would ensure those properties can never be null by writing them like this:
private string _Name = string.Empty;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value ?? string.Empty;
}
}
However, what you're looking for is probably the ??
operator, also known as the null-coalescing operator, as used above, basically, this expression:
x = y ?? z;
means the same as this:
if (y != null)
x = y;
else
x = z;
and that's not entirely true either. In the above example y
is evaluated twice, which does not happen with the ??
operator so a better approximation would be this:
var temp = y;
if (temp != null)
x = temp;
else
x = z;
Upvotes: 5
Reputation: 446
You can take the idea from @Jon one step further so you don't have to worry about using ?? everywhere:
Define your access methods like this:
private String m_name = null;
public String Name
{
get { return m_name ?? String.Empty; }
set { m_name = value; }
}
Anywhere you access the Name property it will perform the null coalescing operator for you.
Upvotes: 0
Reputation: 18743
I'm not sure what you are asking is the best solution for you. If every time you wan't to check a String
and return ""
if it is null
, I'd suggest instead that you initialize your String
fields to ""
.
private String height = "";
instead of
private String height;
By the way, you should store values like height
and weight
as Double
and not String
, unless you have a good reason not to.
Upvotes: 1
Reputation: 2548
If Weight and Height are integers they cannot be null. If your object is null and you are trying to get the Height of Weight of a null object then you code is simply wrong. If a string is null then it will print a null character.
The following is very elegant:
if ( String.isNullOrEmpty(patient.weight) )
{
//DO STUFF HERE
}
else
{
//DO OTHER STUFF HERE
}
Please understand if patient is null you shouldn't even be in this block of code.
If you are using isNullOrEmpty then doing the following is pointless.
patient.weight = "";
Author's Note
I guess i do not understand the reason a string being null is going to cause a problem. The points about setting the value to an empty string when intialized are of course valid.
Upvotes: 0
Reputation: 39970
You can use the ??
operator to return a default value of your choice when any object is null.
string a = null;
a = a ?? "womble";
// a now has the value "womble"
string b = "fluff";
b = b ?? "cabbage";
// b wasn't null, so it still has the value "fluff"
Upvotes: 1
Reputation: 14921
You can do myString ?? string.Empty
, which gives you the string, or string.Empty if it is null.
Upvotes: 2
Reputation: 134035
Use the null-coalescing operator:
string s = Patient.Name ?? string.Empty;
So if Patient.Name
is null, then s
will be set to the empty string.
Upvotes: 2
Reputation: 164331
Something like this ?
public string EmptyIfNull(string value)
{
return value ?? String.Empty;
}
Upvotes: 2
Reputation: 1502066
Well the null coalescing operator sounds like it's your friend here:
string text = patient.Name ?? "";
You could write an extension method to do the same thing, but I think the operator's likely to end up being more readable.
Note that this won't set the property - but that's not what you say you want anyway, in the sentence:
Ideally, I'd like to have a function that can take the string, (not fail even if it is null), and simply return either the value (if it's not null), or a "" if it is null.
That's exactly what the code above does.
Upvotes: 2