0

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,

6
  • can i see the output? of this query, so that i can easily identify the problem Commented Jul 21, 2014 at 16:29
  • 2
    I think you mean Ternary Commented Jul 21, 2014 at 16:29
  • 1
    Try enclosing the Ternary operations in (). Not sure what the order of operations is, but () makes it clear. Commented Jul 21, 2014 at 16:30
  • @ByteBlast doing ("string" + null) does not result in "string\0", it results in "string". The null value resolves to an empty string. Commented Jul 21, 2014 at 16:31
  • Additionally, you mean yyyy-MM-dd. Note the M not m. Commented Jul 21, 2014 at 16:39

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.