Gunnar
Gunnar

Reputation: 113

I can't make a message box, there is an error?

I am trying to add on to my car rental program. Sorry for all the questions. I am still learning :). So I want my form to display an error message that pops up when you don't enter a number or if the text box is blank. I have tried:

 //If nothing is inserted in text, error box.
        int value = 0;
        if (string.IsNullOrWhitespace(txtBegin.Text) || !int.TryParse(txtBegin.Text, out value)) // Test for null or empty string or string is not a number
            MessageBox.Show("Please enter a number!");
        else
            MessageBox.Show(string.Format("You entered: {0}!", value));

It is giving me an error: 'string' does not contain a definition for 'IsNullOrWhitespace'. Can anyone help me?

Upvotes: 2

Views: 117

Answers (2)

Hans Passant
Hans Passant

Reputation: 942020

Using String.IsNullOrWhiteSpace() requires targeting .NET 4.0 or higher. Project + Properties, Application tab, Target framework setting. VS2010 or higher required.

Watch the spelling, let IntelliSense help you fall in the pit of success.

You don't need it at all in this case. The Text property of a TextBox can never be null and TryParse() will already return false if the string is empty. Fix:

    int value = 0;
    if (!int.TryParse(txtBegin.Text, out value))
         MessageBox.Show("Please enter a number!");
    else MessageBox.Show(string.Format("You entered: {0}!", value));

Upvotes: 7

trinaldi
trinaldi

Reputation: 2950

The method is IsNullorWhiteSpace

If you care about backward compatibility use the String.IsNullOrEmpty method.

Upvotes: 0

Related Questions