简体   繁体   中英

how do I use a custom class in an existing C# program that did not use a class?

I'm new here and forgive me if I don't do things right in the beginning.

I have an existing program that I had to create for a course. The program asks for a person first and last name and how many cakes he would like to order and the output in the groupbox show a thank you message with the persons full name along with how many cakes were ordered and the total cost of the cakes with tax included. The next lab asks me to modify the program so that I can use a custom class to produce the same results of the former program with the new class. I hope this makes sense. Below is part of my original program.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Order_Cake
{
    public partial class frmOrderCake : Form
    {
        public frmOrderCake()
        {
        InitializeComponent();
        }

    private void frmOrderCake_Load(object sender, EventArgs e)
    {     
    }

    private void btnEnter_Click(object sender, EventArgs e)
    {
        lblThanks.Text = "Thank you" + (" ") + txtFirstName.Text + (" ") + txtLastName.Text + "!";
        txtCakesOrdered.Text = nudCakes.Value.ToString();
        txtTotalCost.Text = (20 * Convert.ToDouble(nudCakes.Value) * 1.13).ToString("C");
        grpOutput.Visible = true; 
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtFirstName.Clear();
        txtLastName.Clear();
        txtCakesOrdered.Clear();
        txtTotalCost.Clear();
        nudCakes.Value = 1; 
        grpOutput.Visible = false; 
    }
    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close(); 
    }
}

}

for one of the classes I've started the below.

namespace Order_Cake
{
    class Cost
    {
       private int basePrice = 0;
       {
           get
           {
               return basePrice;
           }
           set
           {
               basePrice = value;
           }
       }
   }
} 

of course this would include a class for tax and totalCost and such. I simply don't know how I can incorporate the class into my Cake program. Any help or suggestions will be greatly appreciated.

Thanks,

First write your property like this

namespace Order_Cake
{
    class Cost
    {
       public int BasePrice { get; set; }
       public double Tax { get; set; }
    }
} 

Then to use it like this.

private void btnEnter_Click(object sender, EventArgs e)
{
    lblThanks.Text = "Thank you" + (" ") + txtFirstName.Text + (" ") + txtLastName.Text + "!";
    txtCakesOrdered.Text = nudCakes.Value.ToString();
    Cost obj=new Cost();
    obj.BasePrice = 20;
    obj.Tax=1.13;
    txtTotalCost.Text = (obj.BasePrice * Convert.ToDouble(nudCakes.Value) * obj.Tax).ToString("C");
    grpOutput.Visible = true; 
}

This is only for hint. Make necessary changes as required.

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