I’m creating a query string in a web form, and I’m dealing with values from the parameters that might be null. There are lots of ways to check and fix these, but the most elegant one I found is the following:
string pass;
pass = "BillabilityDetail.aspx?_startDate=" + _startDate.Text + "&_endDate=" + _endDate.Text +
"&_isContractor=" + _isContractor.SelectedValue + "&_busUnit=" + _busUnit.Text
+ "&_projectUnit=" + _projectUnit.SelectedValue + "&_leadCon=" + _leadCon.Value ?? -1
+ "&_acctExec=" + _acctExec.Value ?? -1 + "&_isBillable=" + _isBillable.SelectedValue +
"&_isActive=" + _isActive.SelectedValue + "&_include=" + _include.SelectedValue;
The only issue is… it doesn’t work. When the code reaches this part,
"&_leadCon=" + _leadCon.Value ?? -1 + "&_acctExec=" + _acctExec.Value ?? -1
the string stops assigning values. So the string would end with &_leadCon=.
I know of ways to work around this, but I don’t know why it stopped working in the first place. Any tips?
The
+has higher precedence than??, thus you need to surround your null-coalescing expressions in parenthesis.See the precedence chart here