I have this in my code:
SelectList(blah, "blah", "blah", cu.Customer.CustomerID.ToString())
It gives a error when it returns null, how can I make it CustomerID is an empty string if it is null?
/M
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(Update for C# 6.0)
If you are using C# 6 or newer (Visual Studio 2015 or newer), then you can achieve this using the null-conditional operator
?.:One useful property of the null-conditional operator is that it can also be “chained” if you want to test if several nested properties are null:
For C# versions prior to 6.0 (VS2013 or older), you could coalesce it like this:
Simply check if the object is non-null before you try to access its members, and return an empty string otherwise.
Apart from that, there are situations where null object pattern is useful. That would mean that you ensure that your
Customer‘s parent class (type ofcuin this case) always return an actual instance of an object, even if it is “Empty”. Check this link for an example, if you think it may apply to your problem: How do I create a Null Object in C#.