1

I'm not completely sure the title was good for this. I'm stuck on an assignment for school there is suppose to be a video to shows how to do this but for the life of me I can't figure out how to download the student files from the pearson website. The following is the problem I'm working on.

Employee and ProductionWorker Classes

Create an Employee class that has properties for the following data:

  • Employee name
  • Employee number

Next, create a class named ProductionWorker that is derived from the Employee class. The ProudctionWorker class should have properties to hold the following data:

  • Shift number (an integer, such as 1, 2, or 3)
  • Hourly pay rate

The workday is divided into two shifts: day and night. The Shift property will hold an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2.

Create an application that creates an object of the ProductionWorker class and lets the user enter data for each of the object's properties. Retrieve the object's properties and display their values.

Here is the code I working on for it:

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

namespace classes
{
    public partial class frmMainClasses : Form
    {
        string EmpShift = "";
        string EmployeeName = "";
        int EmployeeNumber = 0;
        float HourlyPayRate = 0;



        public class Employee
        {
            public string EmployeeName { get; set; }
            public int EmployeeNumber { get; set; }
        }

        public class ProductionWorker : Employee
        {
            public float HourlyPayRate { get; set; }
            public Shift Shift { get; set; }
        }

        public enum Shift
        {
            Day = 1,
            Night = 2
        }

        public frmMainClasses()
        {
            InitializeComponent();
        }

        private void btxExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGetInfo_Click(object sender, EventArgs e)
        {
            string EmpShift = "";
            ProductionWorker productionWorker = new ProductionWorker();
            productionWorker.EmployeeName = txtName.ToString();
            EmployeeName = productionWorker.EmployeeName; //Added mostly because I couldn't get EmployeeName to show anything in the messagebox
            productionWorker.EmployeeNumber = Convert.ToInt32(txtIdNumb.text);
            productionWorker.HourlyPayRate = Convert.ToInt32(txtPay.text);
            EmpShift = Convert.ToString(txtShift.text);
            txtName.Text = "";
            txtIdNumb.Text = "";
            txtPay.Text = "";
            txtShift.Text = "";
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Name " + EmployeeName + "IDNumber " + EmployeeNumber + "Hourly rate " + txtPay + "Shift " + txtShift);

        }
    }

}

The code itself isn't showing any errors, but when I try to run it I get:

The string EmpShift thing is in there because I couldn't figure out how to work with the shift code more or less am using that as a place holder until I finger it out. I have no idea how to fix the problem, hopefully its a little mistake.

Thanks to help from the commets I was able to fix the first problem I had, now I'm having a problem with the message box at the end. The information I put into it is, Glitter for name, 12 for ID number, 1 for shift, and 12 for pay. Here's what its showing:

Name System.Windows.Forms.TextBox, Text: GlitterIDNumber 0Hourly rate System.Windows.Forms.TextBox, Text: Shift SystemWindowsForm.TextBox, Text:

Lyght
  • 49
  • 8
  • It should be `Convert.ToInt32(txtIdNumb.Text)`. You want to convert the text property, not the textbox itself. – Ron Beyer Dec 07 '15 at 03:48
  • My messagebox isn't working/displaying right, trying to work on that now. – Lyght Dec 07 '15 at 04:04
  • I no longer get the error, but I can't get the message box to display any of the entered data. I have a feeling its a little mistake, but I'm stumped.I added a new line of code EmployeeName = productionWorker.EmployeeName; which does get me the name that I can then pull up, but I feel like that is defeating the point of the problem. – Lyght Dec 07 '15 at 04:26
  • It seems in the getinfo click, u are not assigning the values to the class level variables that u r using while displaying messageBox – Kapoor Dec 07 '15 at 04:34
  • U have defined a new EmpShift in the get info click, at start of event handler. Incase this doesn't solve the issue, could u please add yr updated code which is not working. – Kapoor Dec 07 '15 at 04:37
  • Updated the code to what I have now, and also updated the problem/issues info. I'm not completely sure what you (Kapoor) want me to do to solve the issue, I'm still very much a newbie programmer. – Lyght Dec 07 '15 at 04:54
  • Sorry for late response, it's Monday morning so I'm on my way to work. But I'll restructure yr code so it works fine, as soon as I get the first opportunity today. If it's very urgent, please write me a comment. – Kapoor Dec 07 '15 at 05:30
  • Its not urgent and I am thankful you are willing to help. – Lyght Dec 07 '15 at 17:18
  • @Lyght, I've added a working code. I've made 5 changes to it, and for every change I've added a comments for explanation. Please write me a comment if you have any questions. Also I'll be happy to know if it works the way u wanted it to. – Kapoor Dec 07 '15 at 18:27
  • @Lyght, also please consider adding validations to text boxes so users cannot enter arbitrary values – Kapoor Dec 07 '15 at 18:29
  • Thank you Kapoor, I'll add the data validation and thank you again especially for the comments you added coding is what I want to do and getting help figuring it out is just as important to me as getting it right. – Lyght Dec 08 '15 at 00:07

1 Answers1

0

Convert.ToString doesnt give you an error because one of it calls its overload with object - public static string ToString(object value). However, since you are interested in user entered value, please use - TextBox.Text property instead of passing the TextBox.

Update 1 : some insides about this behavior

System.Convert.ToString(object value) is implemented as follows in .net -

public static string ToString(Object value, IFormatProvider provider) {
        IConvertible ic = value as IConvertible;
        if (ic != null) 
            return ic.ToString(provider);
        IFormattable formattable = value as IFormattable;
        if (formattable != null) 
            return formattable.ToString(null, provider);
        return value == null? String.Empty: value.ToString();
    }

therefore it ends up calling TextBox.ToString()

and System.Convert.ToInt32(object value) is as follows

   public static int ToInt32(object value) {
        return value == null? 0: ((IConvertible)value).ToInt32(null);
    }

therefore an invalid cast exception because of this - ((IConvertible)value).ToInt32(null)

Update 2 : Code refactored for it to work

public partial class frmMainClasses : Form
{
    //Change 1 
    //I have removed all class level string since they tend to make your code complicated & difficult to manage
    //I'll replace all of them this a single instance of ProductionWorker class, a single object is easy to manage than a bunch 

    ProductionWorker productionWorker = new ProductionWorker(); // Creating the production Worker at class level


    public class Employee
    {
        public string EmployeeName { get; set; }
        public int EmployeeNumber { get; set; }
    }

    public class ProductionWorker : Employee
    {
        public float HourlyPayRate { get; set; }
        public Shift Shift { get; set; }
    }

    public enum Shift
    {
        Day = 1,
        Night = 2
    }

    public frmMainClasses()
    {
        InitializeComponent();
    }

    private void btxExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnGetInfo_Click(object sender, EventArgs e)
    {
        //Change 2 : set the values of the class level variable
        productionWorker.EmployeeName = txtName.Text; //.ToString();  // Change 3 removing the .ToString();
        productionWorker.EmployeeNumber = Convert.ToInt32(txtIdNumb.Text);
        productionWorker.HourlyPayRate = Convert.ToInt32(txtPay.Text);
        productionWorker.Shift = (Shift)Enum.Parse(typeof(Shift), txtShift.Text);

        //change 4 : using .ResetText() instead of Text
        txtName.ResetText();// .Text = "";
        txtIdNumb.ResetText();//.Text = "";
        txtPay.ResetText();//.Text = "";
        txtShift.ResetText();//.Text = "";
    }

    private void btnShow_Click(object sender, EventArgs e)
    {
        // change 5 : accessing class level productionWorker instead of bunch of strings
        MessageBox.Show("Name " + productionWorker.EmployeeName + " IDNumber " + productionWorker.EmployeeNumber + " Hourly rate " + productionWorker.HourlyPayRate + " Shift " + productionWorker.Shift);

     }


}

I've added comments to elaborate what all changes I've made, please write me a comment if you have any questions.

Also, at the moment your code does not validate user inputs in text boxes.

Kapoor
  • 1,388
  • 11
  • 21