0

Let me start off by explaining my goals first:

I am trying to create the highest bidder program for a single product: A vase. (There is only one product and the product does not change.)

I have 2 Inputs name and **offer **, I have a label right below it, which by default states (when no offer has yet been made) "There currently has been no offer made ".

Since the first time a user puts in an offer, he is automatically the highest bidder.

The label changes to state the following: "{name} has with {offer} the highest offer.

Now here gets the tricky part I have trouble figuring out how to make my program, compare the next's bidders offer to the previous bidder's offer.

I want it to state if it's lower: "Sorry {previous bidder} has currently the highest {offer}"

And when its higher the previous bidders offer, the label should change to the current bidders offer to be the highest: "{name} has with {offer} the highest offer."

Here is an Image of the WPF

https://i.stack.imgur.com/MqQ21.png

Here is the code: '''

https://ideone.com/e.js/DKVzNG

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    }

    private void butn1_Click(object sender, RoutedEventArgs e)
    {

        {
            //variabelen 
            string name = txtbox1.Text;
            string txtbod = txtbox2.Text;
            string bodtxt = txtbox1.Text;
            int bod = Convert.ToInt32(txtbod);
            int hoogstebieder = 0;

            if (bod > hoogstebieder); 
            {
                labeltext.Content = $" {name} heeft met {bod} het hoogste bod.";
                hoogstebieder = bod;

            }

                else
            {
                labeltext.Content = "Gelieve een postief getal in te voeren.";
            }



            if (bod > hoogstebieder)
            {
                labeltext.Content = $" {name} heeft met {bod} het hoogste bod.";
            }

            else
            {
                labeltext.Content = $" {name} heeft met {bod} het hoogste bod.";
            }
            }
            }

        }
    }

''

Here is the XAML:

'''

https://ideone.com/e.js/AIX1m5

'''

I hope I have well stated my question and not made any newbie mistakes.

I thank you all in advance.

yahok
  • 1

1 Answers1

0
{
    int hoogstebieder = 0;

    if ([...])
    {
        [...]
        hoogstebieder = bod;
    }
}

The variable hoogstebieder you define there exists only during the execution of your method. This means the next time you enter this method it starts again with 0. So you will always be the first bidder.

To solve the problem you have to save the bidder and the name of the previous person at a location where the values doesn't get deleted when the method complete. You can save them in your MainWindow as fields or properties.

public partial class MainWindow : Window
{ 
    /// <summary>
    /// The bidders name.
    /// </summary>
    private string bidderName;

    /// <summary>
    /// The bidders amount.
    /// </summary>
    private int bidderAmount;

    private void butn1_Click(object sender, RoutedEventArgs e)
    {
        [...]

        this.bidderAmount = bod;

        [...]
    }
}
Progman
  • 16,827
  • 6
  • 33
  • 48
  • Hello, I tried your suggestions, but for some reason when I put them into Main Window, they don't seem to get recognised in the rest of my program anymore. Do you know why ? – yahok Dec 08 '19 at 13:15
  • @yahok If you can access a variable/field/property depends on something called "variable scope" (as described in https://stackoverflow.com/questions/1196941/variable-scope-confusion-in-c-sharp) and ["visibility"](https://stackoverflow.com/questions/614818/in-c-what-is-the-difference-between-public-private-protected-and-having-no). – Progman Dec 08 '19 at 13:29
  • @yahok You have to define these variables as fields or as properties on the class level. You have just moved the variables from inside one method to another method (or in this case a constructor). They exists only inside the scope you define them, that's why you can't access them this way. – Progman Dec 08 '19 at 13:45