c# define own class

my vending machine

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2310 Views - Last Post: 16 December 2010 - 08:22 AM Rate Topic: -----

#1 staceyktaylor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 08-December 10

c# define own class

Posted 12 December 2010 - 01:37 PM

So i am very new to c# programming im currently creating a vending machine here is my code as of now (please excuse the outputs for message box i havent put the right outputs yet their just to test it)

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 vendingmachineprogram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        
        {
            label2.Text = "Computer 0.50";
            label2.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            label4.Text = "Camera - 0.90";
            label4.Visible = true;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            label3.Text = "T.V - 0.70";
            label3.Visible = true;
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            label5.Text = "Mobile Phone - 0.65";
            label5.Visible = true;
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            label6.Text = "Internet - 1.00";
            label6.Visible = true;
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            label7.Text = "Radio = 0.80";
            label7.Visible = true;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
            {
                button1.Enabled = true;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
            private void maskesTextBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if(!char.IsNumber(e.KeyChar))
                {
                    e.Handled = true;
        }
    }

            private void button1_Click(object sender, EventArgs e)
            {
                string input = this.textBox1.Text;

                if (input.Equals("0.50")) MessageBox.Show("Computer\n In 1975 Ed Roberts coined the term personal computer when he introduced the Altair 8800 \n The computer relied on a series of switches for inputting data and output data by turning on and off a series of lights.");

                else if (input.Equals("0.70")) MessageBox.Show("Television \n - Hd is coming");
                else if (input.Equals("0.90")) MessageBox.Show("Video Camera - Amazing");
                else if (input.Equals("0.65")) MessageBox.Show("Mobile Phone - They now come with applications");
                else if (input.Equals("1.00")) MessageBox.Show("Internet - Tim Berners Lee");
                else if (input.Equals("0.80")) MessageBox.Show("Radio - i work for galaxy");
            }

            private void textBox1_TextChanged_1(object sender, EventArgs e)
            {
                button1.Enabled = true;
                

            }

            private void label2_Click(object sender, EventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Clear();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
   }
}



i was wondering if anyone could help me with defining my own class just a simple one to get me going, in this code. thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: c# define own class

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: c# define own class

Posted 12 December 2010 - 01:58 PM

Hello again,

It sort of depends what the class is going to be doing as to how exactly you define it. However, generally speaking, a class if defined like this:

public class ClassName{

public ClassName(){

}


}




Put the class in it's own file, and name the file the same as the class name.

The method you see in the class above is called the constructor. It is the method that is run when you create an instance of the class. Therefore, you use that method to initialize the object and do any work that should be done when the object is created. You don't have to define a contructor. If you don't define one, c# will use a default constructor, which is just an empty constructor that does nothing (like the one above).

You create an instance of the class like this:

ClassName variableName = new ClassName();




Here is a class that represents a circle:


public class Circle{

private double radius;

public Circle(double radius){

this.radius = radius;

}

public double CalculateArea(){

return Math.PI * (this.radius * this.radius);

}




To create a circle object with radius 1.3, we would do this:

Circle myCircle = new Circle(1.3);


The constructor then builds the object by setting the radius. We can then find the area of this circle by calling the CalculateArea method:

myCircle.CalculateArea();


You can have as many constructors in you class as you want, allowing you to configure your objects in whatever was you want when you create them. The constructor cannot be identical to each other though, they have to have a different signiture (i.e. different types of parameters, different number of parameters etc).

The fields provide the properties and characteristis of the object (radius in my example). The methods provide behaviour and make the class useful (the CalculateArea method in my example).

This post has been edited by CodingSup3rnatur@l-360: 12 December 2010 - 02:10 PM

Was This Post Helpful? 1
  • +
  • -

#3 staceyktaylor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 08-December 10

Re: c# define own class

Posted 12 December 2010 - 02:36 PM

wow now this is confusing, ive started to create my own class now and have name it "class vend"

is there a way in which i can define a class for my textbox so that if the input is incorrect it outputs error,and so that it asks for input, or that the input can only be numerical. or is this something different?

i have the c# for dummies book here with me too and cant seem to get my head around this.
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: c# define own class

Posted 12 December 2010 - 02:51 PM

You could create your own custom control or user control I suppose. See this link:

http://www.c-sharpco...gExTextBox.aspx

It basically involves creating a class that inherit from the TextBox class.

However, if it's simple validation you want, you could use a masked text box. If you want the user to be able to only enter numbers, you could use the numeric up down component instead of text boxes.

It is great (and quite rare) that you are trying to break everything into classes so early. It is the key to good design and a very good habit to get into. However, sometimes it's just easier to keep things simple and use what Microsft have provided ready built in the visual studio tool box :).

Keep going with the vending machine class though. Anything you need clarification on as you read your book, just ask. It take some time for the concepts to start making sense, but they will start to make sense if you stick at it.

This post has been edited by CodingSup3rnatur@l-360: 12 December 2010 - 02:56 PM

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is online

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

Reputation: 5316
  • View blog
  • Posts: 11,374
  • Joined: 02-June 10

Re: c# define own class

Posted 12 December 2010 - 05:07 PM

In the last 4 days there must be a dozen threads all about "I'm new to C# but I'm creating a vending machine <...>"

Are you all in the same university course? What text book is the assignment from?
Was This Post Helpful? -1
  • +
  • -

#6 staceyktaylor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 08-December 10

Re: c# define own class

Posted 14 December 2010 - 02:48 PM

i havent seen them, this isnt an assignment i got the idea of an ideas page on here and began to create the program to learn. The book i have is c# for dummies
Was This Post Helpful? 0
  • +
  • -

#7 staceyktaylor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 08-December 10

Re: c# define own class

Posted 14 December 2010 - 03:36 PM

Ok so im trying to define my own class in my program ive attempted this but think i need help not sure if im right.

so heres what i did.

namespace vendingmachineprogram
{
    public class Vend
    {
        public string Product { get; set; }

        public Vend(string product)
        {
            Product = product;
        }
    }
}


and then this is what i did in my main code.
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 vendingmachineprogram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Please choose an item to vend");
           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        
        {
            label2.Text = "Computer 0.50";
            label2.Visible = true;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            label4.Text = "Camera - 0.90";
            label4.Visible = true;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            label3.Text = "T.V - 0.70";
            label3.Visible = true;
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            label5.Text = "Mobile Phone - 0.65";
            label5.Visible = true;
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            label6.Text = "Internet - 1.00";
            label6.Visible = true;
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            label7.Text = "Radio = 0.80";
            label7.Visible = true;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
            {
                button1.Enabled = true;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
            private void maskesTextBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if(!char.IsNumber(e.KeyChar))
                {
                    e.Handled = true;
        }
    }

            private void button1_Click(object sender, EventArgs e)
            {
                string input = this.textBox1.Text;

                if (input.Equals("0.50")) MessageBox.Show("Computer\n In 1975 Ed Roberts coined the term personal computer when he introduced the Altair 8800 \n The computer relied on a series of switches for inputting data and output data by turning on and off a series of lights.");

                else if (input.Equals("0.70")) MessageBox.Show("Television \n - Hd is coming");
                else if (input.Equals("0.90")) MessageBox.Show("Video Camera - Amazing");
                else if (input.Equals("0.65")) MessageBox.Show("Mobile Phone - They now come with applications");
                else if (input.Equals("1.00")) MessageBox.Show("Internet - Tim Berners Lee");
                else if (input.Equals("0.80")) MessageBox.Show("Radio - i work for galaxy");
            }

            private void textBox1_TextChanged_1(object sender, EventArgs e)
            {
                button1.Enabled = true;
                Vend computer = new Vend("0.50 = Computer");
                Vend computer = new Vend("0.70 = Television");
                Vend computer = new Vend("0.90 = Video Camera");
                Vend computer = new Vend("0.65 = MobilePhone");
                Vend computer = new Vend("1.00 = Internet");
                Vend computer = new Vend("0.60 = Radio");
    

            }

            private void label2_Click(object sender, EventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Clear();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
   }
}



have i even defiend a class or am i very wrong. Help please. and be gentle :D
Was This Post Helpful? 0
  • +
  • -

#8 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: c# define own class

Posted 14 December 2010 - 03:47 PM

Hello there,

You have indeed defined a class! There is no need to say 'be gentle' because considering your new to this, you've done very well there I think. The syntax is all valid, you've used your constructor nicely.

I will say that you should probably have a vending machine class as you have, but you should probably have each of your products as separate classes, and have each of the products held within the vending machine class (depending to an extent what you are wanting to do with the program). Or at least something to that effect...

I'm not going to go into the actual design of your program and how that class fits into the design too much as I don't think it's relevant for you at this stage. Learning how to build well designed programs, with well designed classes will come as you get more experience.


One thing I will say though is that you probably shouldn't be creating a new object on every text changed event. Think about it this way, every single time the text in that text box changes, you are creating 6 new objects. That could mean a lot of objects are being created unnecessarily.

I would also say, in your Product property, I would be tempted to make the set accessor private like this:

public string Product{get; private set;}


Just to preserve encapsulation by making sure that the only class that can change that value is the Vend class. Other classes don't need to be able to change it really. That's Vend's job :).

This post has been edited by CodingSup3rnatur@l-360: 14 December 2010 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 staceyktaylor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 08-December 10

Re: c# define own class

Posted 14 December 2010 - 03:58 PM

Hi, thankyou for your feedback, and i know but some people on here seem to be quite rude at times (no offence people reading this) its hard enough trying to learn something new without people being mean :( ill keep at it though and look out for my posts i enjoy your feedback you have helped me a lot :D
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4317
  • View blog
  • Posts: 7,486
  • Joined: 08-June 10

Re: c# define own class

Posted 14 December 2010 - 04:07 PM

Quote

but some people on here seem to be quite rude at times (no offence people reading this) its hard enough trying to learn something new without people being mean


I'll provide a bit of perspective here. You're coming to a forum full of professionals. Most of us do this for a living. We do our best to help out dozens of people a day. Sometimes, we let our frustration show. You may see it as rudeness, but to us, you have to realize the kind of people we see and deal with every day, for some reason. Most of the people we help won't even tell us "thanks." They just get the solution they needed and vanish without even confirming that it worked. Many of these people will never be back. I'd actually love to see the users database here to find just how many accounts have less than 10 posts.

A good quarter of the people that want help are looking for someone to write their homework for them, and another quarter want us to do their actual paying work for them. They don't care about learning, they just want someone to do the hard stuff they're supposed to do.

And then there are the people that we just plain can't understand. They may fit in any other category, but they're so bad at composing their thoughts that we have no idea.

And that leaves us with a very small group of people that actually want and appreciate professional help, and who reciprocate and get better. Those are the people we love to see.

So if some of us make some assumptions, or come off as short or rude, please understand where we're coming from.
Was This Post Helpful? 3
  • +
  • -

#11 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: c# define own class

Posted 14 December 2010 - 04:35 PM

Yes, to be fair stacyktaylor, some of the people that ask for help on here drive me around the bend. It's not because of the questions they ask or anything like that. If the person has shown a willingness to learn and listen, and have put the effort in themselves, I will gladly explain even the simplist of concepts until they understand. No problem.

It's just the people that just all the code handed to them on a plate, without any effort from themselves, that get to me. You take the time to explain to them, but they don't even bother to read your post or acknowledge it. All they are interested in, and all they keep going back to is getting you to write the whole program for them. They post back what they have done in response to your post, but it is obvious they didn't think anything through. They just through it in anywhere and anyhow as they just aren't interested.

It's the people with terrible attitudes that get everyone's back up. I think any of the professionals here will be glad to help someone like yourself out, for example. You clearly want to learn, you are putting the time in, you read what is posted and you genuinely are trying to learn. Someone like you can ask me the most ridiculously easy and simple question, and I will still gladly answer it without anger or rudeness, because I know you listen and really try and understand instead of saying 'Gimmme teh codez' or some incomprehensible equivalent. We all remember what it was like the very first time we started learning this stuff. I'm sure it wasn't easy for anyone at first.

Some people just refuse to think at all for themselves :).

This post has been edited by CodingSup3rnatur@l-360: 14 December 2010 - 04:37 PM

Was This Post Helpful? 1
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: c# define own class

Posted 14 December 2010 - 04:51 PM

Merged duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#13 staceyktaylor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 08-December 10

Re: c# define own class

Posted 14 December 2010 - 05:11 PM

me again i created a new class again

namespace vendingmachineprogram
{
    public class Vend
    {
        public string Product { get; set; }
        public string Item { get; set; }
        public string Cost { get; set; }


        public Vend(string product)
        {
            Product = product;
        }
    }


heres where i put it in my code

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 vendingmachineprogram
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Vend newVend = new Vend();
            newVend.Item = "Computer";
            newVend.Cost = "0.50";

            Vend newVend1 = new Vend();
            newVend1.Item = "Television";
            newVend1.Cost = "0.70";

            

           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        
        {
            label2.Text = "Computer 0.50";
            label2.Visible = true;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            label4.Text = "Camera - 0.90";
            label4.Visible = true;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            label3.Text = "T.V - 0.70";
            label3.Visible = true;
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            label5.Text = "Mobile Phone - 0.65";
            label5.Visible = true;
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            label6.Text = "Internet - 1.00";
            label6.Visible = true;
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            label7.Text = "Radio = 0.80";
            label7.Visible = true;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
            {
                button1.Enabled = true;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
            private void maskesTextBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if(!char.IsNumber(e.KeyChar))
                {
                    e.Handled = true;
        }
    }

            private void button1_Click(object sender, EventArgs e)
            {
                string input = this.textBox1.Text;

                if (input.Equals("0.50")) MessageBox.Show("Computer\n In 1975 Ed Roberts coined the term personal computer when he introduced the Altair 8800 \n The computer relied on a series of switches for inputting data and output data by turning on and off a series of lights.");

                else if (input.Equals("0.70")) MessageBox.Show("Television \n - Hd is coming");
                else if (input.Equals("0.90")) MessageBox.Show("Video Camera - Amazing");
                else if (input.Equals("0.65")) MessageBox.Show("Mobile Phone - They now come with applications");
                else if (input.Equals("1.00")) MessageBox.Show("Internet - Tim Berners Lee");
                else if (input.Equals("0.80")) MessageBox.Show("Radio - i work for galaxy");
            }

            private void textBox1_TextChanged_1(object sender, EventArgs e)
            {
                button1.Enabled = true;
      
            }

            private void label2_Click(object sender, EventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Clear();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

            private void button4_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Vend" + newVend.Item + "\n" +
                           "Cost" + newVend.Cost + "\n");
            }
   }
}



i dont understand why it wont work and what the errors mean. :S
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: c# define own class

Posted 14 December 2010 - 05:17 PM

You can't just tell us

Quote

i dont understand why it wont work and what the errors mean

without telling HOW it doesn't work and WHAT the errors are...remember, we're not standing behind you looking over your shoulder!

If you have errors, copy and paste them exactly as they appear.
Was This Post Helpful? 1
  • +
  • -

#15 staceyktaylor  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 08-December 10

Re: c# define own class

Posted 14 December 2010 - 05:24 PM

Error 1 'vendingmachineprogram.Vend' does not contain a constructor that takes 0 arguments C:\Documents and Settings\stacey taylor\My Documents\Uni\year 2\vendingmachineprogram\vendingmachineprogram\Form1.cs 14 24 vendingmachineprogram
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2