6

I'm trying to make a program which reserves and shows available and unavailable seats for an "event". The errors I'm getting are as follows:

'System.Nullable' does not contain a definition for 'Yes' and no extension method 'Yes' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)

(Same goes for "No") and

'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'.

This is what I have so far:

private void btnSeat1_Click(object sender, RoutedEventArgs e)
{
    if (!Seat1)
    {
        DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);

        if (Result == DialogResult.Yes)
        {
            MessageBox.Show("You Reserved this seat");
            btnSeat1.Text = "Reserved";
        }
        else if (Result == DialogResult.No)
        {
            Environment.Exit(0);
        }

Note: I only used Environment.Exit as a placeholder. It was intentional and will be changed accordingly. It isn't the source of the problem.

Russ
  • 4,091
  • 21
  • 32
Nathan
  • 61
  • 1
  • 2
  • 6
  • 3
    What specifically isn't working in this code? – David Dec 02 '15 at 15:36
  • 2
    Do you really want to exit the program if the user selects "no"? – Ron Beyer Dec 02 '15 at 15:38
  • You say your code isn't working--so what is it doing wrong? And why are you using Environment.Exit()? That should be for aborting your application--which sounds pretty counter to what you would intend. – Russ Dec 02 '15 at 15:38
  • Yes, you should instead use a `return;` statement. – Fᴀʀʜᴀɴ Aɴᴀᴍ Dec 02 '15 at 15:41
  • I only used Environment.Exit(0) as a placeholder. That was intentional – Nathan Dec 02 '15 at 15:42
  • 1
    It looks like the class this method is in contains a property which is called DialogResult – Philippe Dec 02 '15 at 15:46
  • I've edited your post to make it a little clearer - feel free to revert anything that isn't right. Also, I removed this sentence as it's an entirely different question and should probably be asked separately if you still have issues: _"Also, How can I change the text in a button to whatever is in the associated text box, when clicked?"_ – James Thorpe Dec 02 '15 at 15:49

1 Answers1

19

This line:

DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);

Should actually be this:

var Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);

And then if you hover over Result you can see its not of a DialogResult, but a MessageBoxResult, or, if you prefer to explicitly type it, try:

MessageBoxResult Result = MessageBox.Show(...

So you would use it like this in your if statements:

if (Result == MessageBoxResult.Yes)
{
    MessageBox.Show("You Reserved this seat");
    btnSeat1.Text = "Reserved";
}
else if (Result == MessageBoxResult.No)
{
    Environment.Exit(0);
}

You are getting the error because DialogResult is actually a property of the Window class, and you are trying to use it like its a type (as the compiler says).

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • Thanks Hans, I had to make another change but you are right, WPF not Winforms. – Ron Beyer Dec 02 '15 at 15:55
  • Thank you! Really appreciate the feedback!:) – Nathan Dec 02 '15 at 15:57
  • Use `DialogResult` if your application is a `WinForms` application. Use `MessageBoxResult` if your application is a `WPF/Silverlight` application. `Namespaces are different`: System.Windows.Forms (Winforms) vs System.Windows (WPF) – nam Nov 30 '19 at 16:40