RodMcKay
RodMcKay

Reputation: 37

Why is this code working without an Exception

At a windows form I call a method at the load event. There I fill some TextBoxes with this:

     foreach (TabPage page in tabControl_Berichte.TabPages)
        {
            uc_MusterberichtProzessdetails bla = (uc_MusterberichtProzessdetails)page.Controls[0];

            object val = musterbericht.GetType().GetProperty(page.Name + "_Bem").GetValue(musterbericht, null);
                bla.tB_Bemerkung.Text = val.ToString();

            val = musterbericht.GetType().GetProperty(page.Name + "_Bearb").GetValue(musterbericht, null);
                bla.tB_Bearbeiter.Text = val.ToString();

            val = musterbericht.GetType().GetProperty(page.Name + "_Datum").GetValue(musterbericht, null);
                bla.tB_Datum.Text = val.ToString().Substring(0, 10);

            val = musterbericht.GetType().GetProperty(page.Name + "_Erledigt").GetValue(musterbericht, null);
                bla.chB_Erledigt.Checked = (bool)val;

            val = musterbericht.GetType().GetProperty(page.Name + "_Dauer").GetValue(musterbericht, null);
                bla.tB_Dauer.Text = val.ToString();
        }

All the properties are strings except of the "_Datum" which is a DateTime? When the method is being called, the data will be loaded for each TabPage.

In my example one TabPage is loaded correct. At the 2nd TabPage the Date Property is null. So after this:

val = musterbericht.GetType().GetProperty(page.Name + "_Datum").GetValue(musterbericht, null);

val is null.

At the next row:

bla.tB_Datum.Text = val.ToString().Substring(0, 10);

I can see during debugging that when the mouse hovers ToString() there is shown "" (empty string). But if I now click F10 my Form will be shown. The method is stoped. The program runs (the following TextBoxes and tabs are not filled with data).

There is NO Exception!! I don't get it. And there is no try catch anywhere.

If I try this directly with an object wich is set to null an exception is thrown.

So what happens here? Why is my program still working??

(Now I test after each val = ... if val != null, so that's not the problem.)

EDIT:

I just tried this out:

    DateTime? dttest = null;
    object objtest = dttest;
    string strtest = objtest.ToString();

And here I get an exception at ToString().

At the example above I also tried

    val = musterbericht.GetType().GetProperty(page.Name + "_Datum").GetValue(musterbericht, null);
if (val.ToString().Length > 0)
    bla.tB_Datum.Text = val.ToString().Substring(0, 10);

Here it is almost the same behaviour as above. Here the method stops at the if (val.ToString().Length > 0) and the load event is being aborted. (Still no exception)

It looks like val.ToString() is working but val.ToString().xxxxx will cause this strange behaviour.

Upvotes: 0

Views: 117

Answers (1)

Alden
Alden

Reputation: 6703

You can check the msdn docs, or use reflector/dotPeek to see that Nullable<T> overrides ToString() to return "" if null, instead of throwing.

DateTime? is just Nullable<DateTime>, which is why you're getting an empty string and no exception.

Edit: The method is probably "ending" when .Substring(0, 10) executes because you're going to get an ArgumentOutOfRangeException. The empty string has length 0, so you cannot ask for a substring of length 10.

Upvotes: 1

Related Questions