11 Replies - 2926 Views - Last Post: 02 April 2012 - 08:20 PM Rate Topic: -----

#1 easybe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 20-February 12

Employee Class Question

Posted 01 April 2012 - 07:44 PM

Write a class named Employee that has the following properties:



• Name: The Name property holds the employee’s name.

• idNumber: The idNumber property holds the employee’s ID number.

• Department: The Department property holds the name of the department in which the employee works.

• Position: The Position property holds the employee’s job title.

The class should have the following overloaded constructors:



• A constructor that accepts the following values as arguments and assigns them to the appropriate member properties: employee’s name, employee’s ID number, department, and position.

• A constructor that accepts the following values as arguments and assigns them to the appropriate member properties: employee’s name and ID number. The Department and Position fields should be assigned an empty string (“ “);

• A parameterless constructor that assigns empty strings (“ “) to the Name, Department, and Position member properties, and 0 to the idNumber property.



In an application create three Employee objects to hold the following data.



Name ID Number Department Position

Susan Meyers 47899 Accounting Vice President

Mark Jones 39119 IT Programmer

Joy Rogers 81774 Manufacturing Engineer



The program should store this data in three objects and then display the data for each employee on screen.


I believe I did the constructors right, but I probably missed something. The only problem I am having is displaying the ID number. But here is 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 HW10CH9_4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void employeeInfoButton_Click(object sender, EventArgs e)
        {
          //  public void People()
        
            Employee employee1 = new Employee();
            Employee employee2 = new Employee();
            Employee employee3 = new Employee();

            employee1.ID = int.Parse(employeeInfoButton.Text);


            employee1.Name = "Susan";
            employee1.ID = 47899;
            employee1.Dept = "Accounting";
            employee1.Position = "Vice President";

            MessageBox.Show(Employee(employee1.Name));
            
            
        
        }
    }

    class Employee
    {
        // Field for name, ID, dept, and position
        public string name, dept, position;
        public int ID;


        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Dept
        {
            get { return dept; }
            set { dept = value; }
        }

        public string Position
        {
            get { return position; }
            set { position = value; }
        }

        public int numID
        {
            get { return ID; }
            set { ID = value; }
        }




        // 1st Construtor parameterless 
        public Employee()
        {
            position = "";
            dept = "";
            ID = 0;
            name = "";

        }

        // 2nd constructor
        public Employee(int number, string employeeName, string department, string occupation)
        {
            number = ID;
            employeeName = name;
            department = dept;
            occupation = position;
        }

        //3rd constructor
        public Employee(int number, string employeeName)
        {
            number = ID;
            employeeName = name;
            dept = "";
            position = "";

        }

        

        


    }
}
 


oh I forgot ignore MessageBox.Show(Employee(employee1.Name));

it was suppose to be MessageBox.Show(employee1.Name);

Is This A Good Question/Topic? 0
  • +

Replies To: Employee Class Question

#2 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Employee Class Question

Posted 01 April 2012 - 07:52 PM

So what exactly is wrong with your code? Also, what is the point of having two sets of practically the same variable and they're both public. Aren't Name and name the pretty much the same? And in your constructors, you aren't assigning something to the value of name. You are assigning the value of name to employeename, same with the other properties. I think you are looking to have it the other way around.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

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

Reputation: 5678
  • View blog
  • Posts: 12,214
  • Joined: 02-June 10

Re: Employee Class Question

Posted 01 April 2012 - 08:50 PM

Quote

The only problem I am having is displaying the ID number.

Not by a long shot: There are lots of other problems.

Lines 23-25 make three new employee instances WITHIN THE BUTTON CLICK SCOPE. All of these will evaporate as soon as the method completes.
Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing with a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}


Line 27 assigns a value to the employe1.ID that comes from the text on some button? What's the point? You assign a new value in line 31


You're still not laying out a good logical design for your programs before you start coding individual methods. Once again, I urge you to read this article about designing and planning before you start banging on the keyboard.

You are also still putting all the work in the GUI method handlers instead of making methods for a dedicated purpose.

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 0
  • +
  • -

#4 easybe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 20-February 12

Re: Employee Class Question

Posted 02 April 2012 - 10:10 AM

Thank you both! I believe I am confused about the constructors since they have to be overloaded. If I don't need a button, how would the information display when the program runs? The book really didn't go into detail about the get and set methods.
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Employee Class Question

Posted 02 April 2012 - 11:18 AM

Try replacing this:

MessageBox.Show(Employee(employee1.Name));


For this:

MessageBox.Show(employee1.ID);


With this new code you go:

"For the object 'employee1', use the getter for the 'ID' property to fetch the value, and output it via MessageBox."
Was This Post Helpful? 0
  • +
  • -

#6 easybe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 20-February 12

Re: Employee Class Question

Posted 02 April 2012 - 11:32 AM

View PostSergio Tapia, on 02 April 2012 - 11:18 AM, said:

Try replacing this:

MessageBox.Show(Employee(employee1.Name));


For this:

MessageBox.Show(employee1.ID);


With this new code you go:

"For the object 'employee1', use the getter for the 'ID' property to fetch the value, and output it via MessageBox."



I tried that already and I am getting an error

Error: Argument 1: cannot convert from 'int' to 'string'
Error: The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string)' has some invalid
Was This Post Helpful? 0
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Employee Class Question

Posted 02 April 2012 - 12:25 PM

Since "ID" is of type int, you need to cast it using .ToString() on it. :)
Was This Post Helpful? 0
  • +
  • -

#8 easybe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 20-February 12

Re: Employee Class Question

Posted 02 April 2012 - 12:30 PM

ok I decided to add a class since it was easier for me to display in a console. So here is my updated code I am not sure about the second constructor (its the second bullet in the problem) about where the empty string for Department and Position.

  

class Employee
{
        private int _ID;
        private string employeeName, dept, _POS;

        public string Name
        {
            get
            {
                return employeeName;
            }
        }

        public int idNumber
        {
            get
            {
                return _ID;
            }
        }

        public string Department
        {
            get
            {
                // dept = "";
                return dept;
            }
        }

        public string Position
        {
            get
            {
                // _POS = "";
                return _POS;
            }
        }

        // First constructor
        public Employee(string name, int employNum, string department, string position)
        {
            employeeName = name;
            _ID = employNum;
            dept = department;
            _POS = position;
        }


        // 2nd constructor
        public Employee(string _name, int _id)
        {
            employeeName = _name;
            _ID = _id;
        }

        // 3rd constructor
        public Employee()
        {
            employeeName = "";
            dept = "";
            _POS = "";
            _ID = 0;
        }



    } 



Console App:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW10CH9_4
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee employee1 = new Employee("Susan", 67);
            Console.Write(employee1.Name + employee1.idNumber);
         //   Console.Write(employee1.idNumber);
           // Console.Write(employee1.Department);
           // Console.Write(employee1.Position); 
            

            Console.Read();
        }
    }
}
 



Also when I tried to run the console without Console.Read() and it didn't work. Is this a problem on my end?

This post has been edited by easybe: 02 April 2012 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Employee Class Question

Posted 02 April 2012 - 01:16 PM

It does work, it's just the Console.Read() at the end pauses your program and waits for you to press enter. Otherwise, your code is just executing and exiting right away, not waiting for you to actually see the results of the code. For Console Apps, you should probably always have a Console.Read() or Console.ReadKey() as the very last line in the main method.
Was This Post Helpful? 0
  • +
  • -

#10 easybe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 20-February 12

Re: Employee Class Question

Posted 02 April 2012 - 01:27 PM

View Postsuperkb10, on 02 April 2012 - 01:16 PM, said:

It does work, it's just the Console.Read() at the end pauses your program and waits for you to press enter. Otherwise, your code is just executing and exiting right away, not waiting for you to actually see the results of the code. For Console Apps, you should probably always have a Console.Read() or Console.ReadKey() as the very last line in the main method.


I tried it again, and it seems like the console would display but it wouldn't. I have no clue :dots:
Was This Post Helpful? 0
  • +
  • -

#11 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Employee Class Question

Posted 02 April 2012 - 04:25 PM

What wouldn't display?
Was This Post Helpful? 0
  • +
  • -

#12 easybe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 20-February 12

Re: Employee Class Question

Posted 02 April 2012 - 08:20 PM

View Postsuperkb10, on 02 April 2012 - 04:25 PM, said:

What wouldn't display?


It is just basically like my last reply, when I add Console.Read() to my code the program will run, but for some reason when I take Console.Read() out the program will run but the console app works only for a few seconds and vanish.

I believe that since I debugged it vanishes quickly, but when I pressed CTRL + F5 it stayed open.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1