简体   繁体   中英

Visual Studio Win Form

I am having trouble figuring out these errors on my win form on Visual Studio.

The program runs although it does not properly calculate the add-ons. It will not calculate more than 75 cents even if all 3 add-ons are checked. Array names will not change either even though it does not show any errors related to arrays, but it might have alot to do with the event handlers showing as an error.

{

    private object groupSalad;
    private object groupPizza;
    private object groupHamburger;
    private int addon3;
    private int addon2;
    private int addon1;

    public Form1()
    {
        //created event handlers for change in radiobox
        InitializeComponent();
        hamburger.CheckedChanged += new System.EventHandler(groupHamburger.Checked);
        salad.CheckedChanged += new System.EventHandler(groupSalad.Checked);
        pizza.CheckedChanged += new System.EventHandler(groupPizza.Checked);
        // ERROR ON groupSalad/Pizza/Hambuger.CHECKED' and
        // group Pizza. object does not contain a definition for 'checked' 
        // and no extension method for 'checked' accepting a first argiument 

    }

    private void btnPlaceOrder_Click(object sender, EventArgs e)
    {

        //declared variables to hold
        decimal tax = .09m;
        decimal food = 0;
        decimal addOn;
        //food options. 3 radio buttons 
        decimal burger = 9.95m;
        decimal salad = 7.95m;
        decimal pizza = 8.95m;
        //variables to hold currency ammounts
        decimal subtotal = 0;
        decimal taxAmmount = 0;
        decimal total = 0;


        //depending on radio button options, addons will change text with array

        string[] addOnPizza = new string[2];
        //array type 1. not prefered
        addOnPizza[0] = "Suasage";
        addOnPizza[1] = "Olives";
        //salad add-ons
        string[] addOnSalad = new string[3];
        addOnSalad[0] = "Bacon Bits";
        addOnSalad[1] = "Bread Sticks";
        addOnSalad[2] = "Croutons";

        //array type. prefered
        string[] addOnBurger = { "Cheese", "Ketchup", "Fries" };

        //RadioButtons
        if (this.hamburger.Checked)
        {
            food = burger;
            addOn = .75m;
            chkAddOn1.Text = addOnBurger[0];
            chkAddOn2.Text = addOnBurger[1];
            chkAddOn3.Text = addOnSalad[2];
        }
        else if (this.salad.Checked)
        {
            food = salad;
            addOn = 45m;
            chkAddOn1.Text = addOnSalad[0];
            chkAddOn2.Text = addOnSalad[1];
            chkAddOn3.Text = addOnSalad[2];
        }
        else if (this.pizza.Checked)
        {
            addOn = .65m;
            food = pizza;
            chkAddOn1.Text = addOnPizza[0];
            chkAddOn2.Text = addOnPizza[1];
            chkAddOn3.Text = null;
        }


        decimal addOnTotal;
        if (chkAddOn1.Checked)
        {
            addOnTotal =+ addon1;
            if (chkAddOn2.Checked)
                addOnTotal =+ addon2;
            if (chkAddOn3.Checked) 
                addOnTotal =+ addon3;
        }

//Use of unassigned variable for addOnTotal
            subtotal = food + addOnTotal;

            taxAmmount = food * tax;
            total = taxAmmount + food;

            txtSubtotal.Text = subtotal.ToString("c");
            txtTax.Text = tax.ToString("c");
            txtTotal.Text = total.ToString("c");



        }
    }

}

there is problem with this line

 hamburger.CheckedChanged += new System.EventHandler(groupHamburger.Checked);

it should be

hamburger.CheckedChanged += new System.EventHandler(btnPlaceOrder_Click);

because event handler for CheckedChanged have signature which expect handler function which has sender & EvenArgs as prameters button1_Click(object sender, EventArgs e)


For second error

//Use of unassigned variable for addOnTotal
        subtotal = food + addOnTotal;

you must initialize addOnTotal variable to default value as its used with in function , as per guideline all variable local to method should be initialized, so do as below

decimal addOnTotal=0; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM