Master
Master

Reputation: 2153

Concatenating multiple strings with nullables

I Have a messagebox to display some text and data (if existing) within database. The current Issue is trying to show nulls and trying to convert to ShortDate. I've taken two approach but none quite work in the way I need.

The first approach uses Ternary concatenation within the string but it behaves really weird.

DialogResult DuplicateMessage = MessageBox.Show("A contact name " + DuplicateName.Forename + " " + DuplicateName.Surname + " already exists within the System."
                            + "\n Existing Client:  " + DuplicateName.Forename + " " + DuplicateName.Surname
                            + "\n Date of Birth:  " +  DuplicateName.DOB != null ? Convert.ToDateTime(DuplicateName.DOB).ToString("yyyy-mm-dd") : " ",  
                            ,"Possible Duplicate Client", MessageBoxButtons.YesNo);

Currently The message box only shows the line breaks and the Date Of birth. Not even the text "Date of Birth"

If I remove Tertiary and conversion and simply have

DialogResult DuplicateMessage = MessageBox.Show("A contact name " + DuplicateName.Forename + " " + DuplicateName.Surname + " already exists within the System."
                            + "\n Existing Client:  " + DuplicateName.Forename + " " + DuplicateName.Surname
                            + "\n Date of Birth:  " +  DuplicateName.DOB
                            ,"Possible Duplicate Client", MessageBoxButtons.YesNo);

This works, shows everything. Only issue is that the Date of birth is in the wrong format. Was wondering how do I make it so the date is in short date format and will show everything.

all Properties Of 'DuplicateName' are nullable,

Upvotes: 0

Views: 71

Answers (2)

Grant Winney
Grant Winney

Reputation: 66439

You can fix it by using another pair of parentheses:

(DuplicateName.DOB != null ? Convert.ToDateTime(DuplicateName.DOB))

In your first case, you're concatenating a huge string together (because you don't use any parentheses) and then testing that for null. It's equivalent to this:

var stringToTest = "A contact name " + DuplicateName.Forename + " " + DuplicateName.Surname + " already exists within the System."
                        + "\n Existing Client:  " + DuplicateName.Forename + " " + DuplicateName.Surname
                        + "\n Date of Birth:  " +  DuplicateName.DOB;


DialogResult DuplicateMessage =
    MessageBox.Show(stringToTest != null ? Convert.ToDateTime(DuplicateName.DOB).ToString("yyyy-mm-dd") : " ",  
                    ,"Possible Duplicate Client", MessageBoxButtons.YesNo);

Upvotes: 1

David
David

Reputation: 218818

I suspect this is a problem with operator precedence using the conditional operator. It's likely including string concatenations as part of the condition being tested, rather than as part of the result. You can explicitly enclose the elements of that operator with parentheses to identify which strings belong therein and which do not:

"\n Date of Birth:  " +  (DuplicateName.DOB != null ? Convert.ToDateTime(DuplicateName.DOB).ToString("yyyy-mm-dd") : " ")

Additionally, if DOB is a DateTime? then you can simplify your code a little:

"\n Date of Birth:  " +  (DuplicateName.DOB.HasValue ? DuplicateName.DOB.Value.ToString("yyyy-mm-dd") : " ")

There's no need to use Convert on Nullable<T> types, you can more easily (and safely) make use of the HasValue and Value properties.

Upvotes: 2

Related Questions