2 Replies - 613 Views - Last Post: 29 October 2011 - 07:59 AM Rate Topic: -----

#1 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Windows forms (class)

Posted 26 October 2011 - 01:58 PM

Hi, I am trying to write a windows forms application that is a restaurant calculator, now I've built a similar application, but I need to make this one by creating a class for the entrees and so forth. I have been trying to figure it out, but every example I have seen has the class defined in a different section then the event handler, and has a different namespace for the event handler portion of the code,(Initialize component section), my book makes no mention of how to do this in a different section and I am really confused on how to create the class and use it with the application, since whenever I try to in satiate the class I get an "doesn't exist in current context error", I'm sure there is an easy answer of what to do I just can't figure it out, so any help would be welcomed, I will post my code in defining the class (Order):
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Order
        {
            private string Beverage;
            private string Appetizer;
            private string MainCourse;
            private string Dessert;
            private string Preps;
            private bool Water;
            private decimal BevPr;
            private decimal AppPr;
            private decimal MainPr;
            private decimal DesPr;
            private decimal PrepPr;

            public Order()
            {
                Beverage = "";
                Appetizer = "";
                MainCourse = "";
                Dessert = "";
                Preps = "";
                Water = false;
                BevPr = 0;
                AppPr = 0;
                MainPr = 0;
                DesPr = 0;
                PrepPr = 0;
            }
            public string beverage
            {
                get
                {
                    return Beverage;
                }
                set
                {
                    Beverage = value;
                    SetBevPr();
                }
            }
            public string appetizer
            {
                get
                {
                    return Appetizer;
                }
                set
                {
                    Appetizer = value;
                    SetAppPr();
                }
            }
            public string maincourse
            {
                get
                {
                    return MainCourse;
                }
                set
                {
                    MainCourse = value;
                    SetMainPr();
                }
            }
            public string dessert
            {
                get
                {
                    return Dessert;
                   
                }
                set
                {
                    Dessert = value;
                    SetDesPr();
                }
            }
            public string preps
            {
                get
                {
                    return Preps;
                }
                set
                {
                    Preps = value;
                    SetPrepPr();
                }
            }
            public bool water
            {
                set
                {
                    Water = value;
                }
            }
            public decimal bprice
            {
                get
                {
                    return BevPr;
                }
            }
            public decimal aprice
            {
                get
                {
                    return AppPr;
                }
            }
            public decimal mcprice
            {
                get
                {
                    return MainPr;
                }
            }
            public decimal dprice
            {
                get
                {
                    return DesPr;
                }
            }
            public decimal pprice
            {
                get
                {
                    return PrepPr;
                }
            }

            public void SetBevPr()
            {
                switch (Beverage)
                {
                    case "Soft Drink":
                        BevPr = 2.50m;
                        break;
                    case "Beer":
                        BevPr = 4.00m;
                        break;
                    case "Wine":
                        BevPr = 6.00m;
                        break;
                    case "Liquor":
                        BevPr = 8.00m;
                        break;
                    case "Coffee":
                    case "Tea":
                        BevPr = 2.95m;
                        break;

                }
            }
            public void SetAppPr()
            {
                switch (Appetizer)
                {
                    case "Calamari":
                        AppPr = 8.50m;
                        break;
                    case "Oysters":
                        AppPr = 12.95m;
                        break;
                    case "Shrimp Cocktail":
                        AppPr = 11.50m;
                        break;
                    case "Chicken Wings":
                        AppPr = 8.50m;
                        break;
                    case "Quesadillas":
                        AppPr = 6.95m;
                        break;
                    case "Cheese Mushrooms":
                        AppPr = 5.95m;
                        break;
                }
            }
            public void SetMainPr()
            {
                switch (MainCourse)
                {
                    case "Steak Dinner":
                        MainPr = 31.50m;
                        break;
                    case "Chicken Dinner":
                        MainPr = 19.95m;
                        break;
                    case "Fish Dinner":
                        MainPr = 21.95m;
                        break;
                    case "Pork Dinner":
                        MainPr = 20.50m;
                        break;
                    case "Crab Leg Dinner":
                        MainPr = 39.95m;
                        break;
                    case "Vegetarian Dinner":
                        MainPr = 18.50m;
                        break;
                }
            }
            public void SetDesPr()
            {
                switch (Dessert)
                {
                    case "Apple Pie":
                        DesPr = 5.50m;
                        break;
                    case "Creme Brulee":
                        DesPr = 6.95m;
                        break;
                    case "Chocolate Mousse":
                    case "Key Lime Pie":
                    case "Cheesecake":
                        DesPr = 5.95m;
                        break;
                    case "Ice Cream":
                        DesPr = 3.50m;
                        break;

                }
            }
            public void SetPrepPr()
            {
                switch (Preps)
                {
                    case "Food Allergy!!!":
                        PrepPr = 0.00m;
                        break;
                    case "Broccoli":
                    case "Asparagus":
                    case "Mushrooms and Onions":
                    case "Rice":
                        PrepPr = 4.95m;
                        break;
                    case "Baked Potato":
                    case "Au Gratin Potato":
                    case "Mac and Cheese":
                        PrepPr = 6.50m;
                        break;
                    case "Lobster Tail":
                        PrepPr = 32.95m;
                        break;
                    case "Shrimp Scampi":
                        PrepPr = 12.50m;
                        break;
                }
            }


           


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            newOrder = new Order(); //// error is here
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Windows forms (class)

#2 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Windows forms (class)

Posted 27 October 2011 - 02:41 AM

You probably want something like this:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // added code
        private Order newOrder;

        public Form1()
        {
            InitializeComponent();
        }

        public class Order
        {
            private string Beverage;
            private string Appetizer;
            private string MainCourse;
            private string Dessert;
            private string Preps;
            private bool Water;
            private decimal BevPr;
            private decimal AppPr;
            private decimal MainPr;
            private decimal DesPr;
            private decimal PrepPr;

            public Order()
            {
                Beverage = "";
                Appetizer = "";
                MainCourse = "";
                Dessert = "";
                Preps = "";
                Water = false;
                BevPr = 0;
                AppPr = 0;
                MainPr = 0;
                DesPr = 0;
                PrepPr = 0;
            }
            public string beverage
            {
                get
                {
                    return Beverage;
                }
                set
                {
                    Beverage = value;
                    SetBevPr();
                }
            }
            public string appetizer
            {
                get
                {
                    return Appetizer;
                }
                set
                {
                    Appetizer = value;
                    SetAppPr();
                }
            }
            public string maincourse
            {
                get
                {
                    return MainCourse;
                }
                set
                {
                    MainCourse = value;
                    SetMainPr();
                }
            }
            public string dessert
            {
                get
                {
                    return Dessert;
                   
                }
                set
                {
                    Dessert = value;
                    SetDesPr();
                }
            }
            public string preps
            {
                get
                {
                    return Preps;
                }
                set
                {
                    Preps = value;
                    SetPrepPr();
                }
            }
            public bool water
            {
                set
                {
                    Water = value;
                }
            }
            public decimal bprice
            {
                get
                {
                    return BevPr;
                }
            }
            public decimal aprice
            {
                get
                {
                    return AppPr;
                }
            }
            public decimal mcprice
            {
                get
                {
                    return MainPr;
                }
            }
            public decimal dprice
            {
                get
                {
                    return DesPr;
                }
            }
            public decimal pprice
            {
                get
                {
                    return PrepPr;
                }
            }

            public void SetBevPr()
            {
                switch (Beverage)
                {
                    case "Soft Drink":
                        BevPr = 2.50m;
                        break;
                    case "Beer":
                        BevPr = 4.00m;
                        break;
                    case "Wine":
                        BevPr = 6.00m;
                        break;
                    case "Liquor":
                        BevPr = 8.00m;
                        break;
                    case "Coffee":
                    case "Tea":
                        BevPr = 2.95m;
                        break;

                }
            }
            public void SetAppPr()
            {
                switch (Appetizer)
                {
                    case "Calamari":
                        AppPr = 8.50m;
                        break;
                    case "Oysters":
                        AppPr = 12.95m;
                        break;
                    case "Shrimp Cocktail":
                        AppPr = 11.50m;
                        break;
                    case "Chicken Wings":
                        AppPr = 8.50m;
                        break;
                    case "Quesadillas":
                        AppPr = 6.95m;
                        break;
                    case "Cheese Mushrooms":
                        AppPr = 5.95m;
                        break;
                }
            }
            public void SetMainPr()
            {
                switch (MainCourse)
                {
                    case "Steak Dinner":
                        MainPr = 31.50m;
                        break;
                    case "Chicken Dinner":
                        MainPr = 19.95m;
                        break;
                    case "Fish Dinner":
                        MainPr = 21.95m;
                        break;
                    case "Pork Dinner":
                        MainPr = 20.50m;
                        break;
                    case "Crab Leg Dinner":
                        MainPr = 39.95m;
                        break;
                    case "Vegetarian Dinner":
                        MainPr = 18.50m;
                        break;
                }
            }
            public void SetDesPr()
            {
                switch (Dessert)
                {
                    case "Apple Pie":
                        DesPr = 5.50m;
                        break;
                    case "Creme Brulee":
                        DesPr = 6.95m;
                        break;
                    case "Chocolate Mousse":
                    case "Key Lime Pie":
                    case "Cheesecake":
                        DesPr = 5.95m;
                        break;
                    case "Ice Cream":
                        DesPr = 3.50m;
                        break;

                }
            }
            public void SetPrepPr()
            {
                switch (Preps)
                {
                    case "Food Allergy!!!":
                        PrepPr = 0.00m;
                        break;
                    case "Broccoli":
                    case "Asparagus":
                    case "Mushrooms and Onions":
                    case "Rice":
                        PrepPr = 4.95m;
                        break;
                    case "Baked Potato":
                    case "Au Gratin Potato":
                    case "Mac and Cheese":
                        PrepPr = 6.50m;
                        break;
                    case "Lobster Tail":
                        PrepPr = 32.95m;
                        break;
                    case "Shrimp Scampi":
                        PrepPr = 12.50m;
                        break;
                }
            }      


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            newOrder = new Order(); //// error is here
        }


Hope this helps :) I believe it is because you haven't declared an instance of the object you wish to instantiate although I could be wrong ( pros help me out here if I got it wrong :) )
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Windows forms (class)

Posted 29 October 2011 - 07:59 AM

Quote

279  newOrder = new Order(); //// error is here


That's not the error you need to concern yourself with. The big error is that you aren't following the instructions:

Quote

but I need to make this one by creating a class for the entrees and so forth.

But you have not tried to make a class for the entrees. Or anything else. And because of that you have this horrible case structure through out your code.

I just answered another question about "what is a class" so I'm guessing this is the area of study for courses this week.

So you're basically asking: Can someone teach me what a class is and how to use it?

That's a pretty big subject for a forum thread.
These tutorials cover this to some extent:

Bulding an application - Part 1
Building an application - Part 2


But here's a short way to look at it:

Class is the hypothetical definition of something. The plan or blueprint for a thing.
Object is the instance of that thing when you make one.
'Dodge Ram' is a concept. A class.
MyDodgeRam with VIN number 12345 is an instance of that class that has been constructed.

Everything is an instance of an object. That's how to look at the world around you. And each object is made up of smaller object. And each object has properties and functions.

MyDodgeRam is an instance of the Dodge Ram class.
MyDodgeRam has properties like paint color and interior color.
MyDodgeRam has functions like StartEngine, Turn wheels, add fuel
MyDodgeRram has smaller objects in it like Radio, Engine and transmission.

In code it looks like this

class vehicle
{
   // Most generic level
   public Color PaintColor;
   public Color InteriorColor;
   public Radio RadioModel;
   public Engine EngineModel;

   public virtual TurnWheels(Direction direction)
   {
   }
}

class truck : vehicle
{
   // a truck inherits from vehicle and is a more specific type
   // Properties of PaintColor, InteriorColor and so on are inherited from the parent class vehicle so I don't need to recreate them here
}

class DodgeRam : truck
{
   // a DodgeRam inherits from truck and is an even more specific type
  // Properties of PaintColor, InteriorColor and so on are inherited from the parent class vehicle so I don't need to recreate them here

   public DodgeRam(Color newPaintColor)
   {
       PaintColor = newPaintColor;// When I create a new instance of the DodgeRam class it gets the color that is passed in to the constructor

       override TurnWheels(Direction direction)
       {
           // A virtual method in the parent class can then be
           // overriden by a child class.  That's because the parent
           // doesn't know the specifics of the model.  In this case
           // this instance knows its turning radius so it won't
           // try to turn tighter than is physically possible.
       }
   }
}


You would do the same with your food items.

class item
class entree : item
class beverage : item

Your item class would have properties for .Cost .Description .AlergyWarning and so on.
class Item
{
    string name;
    decimal price;
    string AlergyWarning;
}


Now think about what an order is. It is a list of Items with a subtotal, tax, total, tip and server name. So make a class that has these items.

You are also assuming an order has exactly this many of each type, and trying to define an individual variable for each. How well would that work if you went into Denny's? You could simply have a List<Item> and keep adding to it all day long if you like. Work on being more open ended and less predictive and micro-managing. The user should be able to have lots of freedom.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1