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:
Next, create a class named ProductionWorker that is derived from the Employee class. The ProudctionWorker class should have properties to hold the following data:
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:
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.