开发者

?? operator in system.DBNull

开发者 https://www.devze.com 2023-03-15 01:55 出处:网络
Is there an operator or built in function to simplyfy this: myVal = object1.object2.something(a,b).dataColumn.toString()==\"\"?object1.object2.something(a,b).dataColumn.toString():\"-\";

Is there an operator or built in function to simplyfy this:

myVal = object1.object2.something(a,b).dataColumn.toString()==""?object1.object2.something(a,b).dataColumn.toString():"-";

I know i can do something like:

string str = object1.object2.something(a,b).dataColumn.toString();
myVal =str==""?"-":str;

but I have many objects and I want to avoid it:

string str1 = object1.object2.something(a,b).dataColumn1.toString();
myVal1==""?str1:"-"
string st开发者_StackOverflowr2 = object1.object2.something(a,b).dataColumn2.toString();
myVal2==""?str2:"-"
:
string strN = object1.object2.something(a,b).dataColumnN.toString();
myValN==""?strN:"-"

I can also create a function:

private string CheckNull(object dataColumn){
    return dataColumn == System.DBNull?"-":dataColumn.toString();
}

myVal1 = CheckNull(object1.object2.something1(a,b).dataColumn.toString())
myVal2 = CheckNull(object1.object2.something2(a,b).dataColumn.toString())
myVal3 = CheckNull(object1.object2.something3(a,b).dataColumn.toString())

The easiest way is to use the ?? operator but the problem is that 'dataColumn' is not compatible with ?? because sometimes returns a system.DBNull instead of a null. Look at the immediate window output:

System.DBNull??"-"
'System.DBNull' is a 'type', which is not valid in the given context
null??"-"
"-"

I wonder if there is some (string) function or operator that can return "-" if dataColumn.toString()=="" without an if..then (because I would have to make many if..then for all of them. Otherwise I rather use the function approach shown above.

string str = object1.object2.something(a,b).dataColumn.toString().if("","-");


How about:

public static class StringHelper {
    public static string ValueOrDash(this string value) {
        return string.IsNullOrEmpty(value) ? "-" : value;
    }
}

Then you can just do:

myVal = object1.object2.something(a,b).DataColumn.ToString().ValueOrDash();

Or better yet:

public static class StringHelper {
    public static string ValueOrWhatever(this string value, string defaultValue) {
        return string.IsNullOrEmpty(value) ? defaultValue : value;
    }
}

myVal = object1.object2.something(a,b).DataColumn.ToString().ValueOrWhatever("-");


If the underlying object is always either a string or DbNull, you could use the as cast operator:

private string CheckNull(object dataColumn){
    return dataColumn == (dataColumn as string)??"-":dataColumn;
}

This will also return "-" if the value is an integer for example or some other type.

It's not clear from your code, but if these values are coming from DataRows you could use the new (.NET 3.5) DataRowExtensions.Field function, which will return null instead of DbNull.


You could write an extension method to call on the dataColumn. It wouldn't functionally be much different than your CheckNull method, though.

Or in your first example with str1, str2, strN and such, is there a reason you can't reuse str1 for each? I see you have a method call in your long line of code, you wouldn't want to waste the time running that more often than you need to (especially if it's going to result in the same output every time).

0

精彩评论

暂无评论...
验证码 换一张
取 消