views:

3676

answers:

7

Here's the scenario...

if (entry.Properties["something"].Value != null)
  attribs.something = entry.Properties["something"].Value.ToString();

While effective and working correctly, this looks ugly to me. If I don't check for a null before performing the ToString() then it throws an exception if the property was null. Is there a better way to handle this scenario?

Much appreciated!

+18  A: 

(edited to actually work :) )

object defaultValue = "default";
attribs.something = (entry.Properties["something"].Value ?? defaultValue).ToString()

Edit: I've also taken to using this, which isn't terribly clever but convenient:

public static string ToSafeString(this object obj)
{
    return (obj ?? string.Empty).ToString();
}
Rex M
Do you know off hand if the "default" could be replaced with string.Empty?
Dscoduc
@Dscoduc - Sure.
Rex M
+1 and Answer: Just tested it and worked perfectly... Thank you!
Dscoduc
Note that if the method making the call runs more than once and attribs is reused then you may be overwriting valid data gotten on 1 call with 'default' on a subsequent call.
xcud
@xcud: True. In that case, he should use the following code: attribs.something = (entry.Properties["something"].Value ?? (objcet)attribs.something).ToString();
configurator
+1  A: 

As a variation to RexM's answer:

attribs.something = (entry.Properties["something"].Value ?? attribs.something).ToString()

The only downside would be that the attribs.something would be assigned a value (itself, in this example) even if entry.Properties["something"].Value was null - which could be expensive if the .something property did some other processing and/or this line executes a lot (like in a loop).

ZaijiaN
+1 Great addition! Thank you!
Dscoduc
A: 

How about using an auxiliary method like this:

attribs.something = getString(
    entry.Properties["something"].Value, 
    attribs.something);

static String getString(
    Object obj,
    String defaultString)
{
    if (obj == null) return defaultString;
    return obj.ToString();
}

Alternatively, you could use the ?? operator:

attribs.something = 
    (entry.Properties["something"].Value ?? attribs.something).ToString();

(note the redundant ToString() call when the value is null)

Zach Scrivena
+1  A: 

To do precisely what you're trying to do a helper method can always be used:

CopyIfNotNull(entry.Properties["something"].Value, out attribs.something);

void CopyIfNotNull(string src, out string dest)
{
  if(src != null)
    dest = src;
}
Mike Hall
Don't you have to specify "out" in the second argument of the CopyIfNotNull?
Dscoduc
yup. I realized that after I posted it.
Mike Hall
+11  A: 

If you are targeting the .NET Framework 3.5, the most elegant solution would be an extension method in my opinion.

public static class ObjectExtensions
{
    public static string NullSafeToString(this object obj)
    {
        return obj != null ? obj.ToString() : String.Empty;
    }
}

Then to use:

attribs.something = entry.Properties["something"].Value.NullSafeToString();
Dale Ragan
I wish this was built into the C# framework...
Dscoduc
A: 

Can you not do:

attribs.something = entry.Properties["something"].Value as string;
NotDan
No, you can't; if it's 'null' you'll get a null reference error.
George Stocker
A: 

attribs.something = string.Format("{0}",entry.Properties["something"].Value)

aljj