0

This Is my assignment, I am having trouble getting my class to work with main for, can someone please help me? This is due on Tuesday and I have been hitting a brick wall in every approach I have tried. All my class and my forms are posted. Please help me I am totally lost and frustrated

  1. 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 ProductionWorker 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.


This is my employee reference chart to store their names and I.D numbers. I am getting no compiling errors on this class, however I am not sure if I am doing this correctly because in my main I get a compiling error.

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

namespace Employee_References
{
class Roster
{
     // Field for name, ID, dept, and position  
     private const int NAMES = 100;
     private static string [] employee = new string [NAMES];
     private const int NUMBER = 100;
     private static int [] id = new int [NUMBER];
     private int total = 0;



     public void Employee()
     {
         total = 0;
     }

   // This will recieve input from my main 
   public static void employeeName (string [] xArray)   

    {


            for (int index = 0; index < xArray.Length; index++)
            {
                xArray[index] = employee[NAMES];
            }



    }


   // This will recieve input from my main 
   public static void idNumber ( int [] zArray)
    {
       for (int index = 0; index < zArray.Length; index++)
        {
            zArray[index] = id[NUMBER];
        }
    }


     }



} 

This will be my next class that is derived from my first class. This class is suppose to store the shift numbers 1 through for 4, and an hourly wage setter for a Day and Night Shift. I am getting one compiling error in this class that says " The left-hand side of an assignment must be a variable, property or indexer" I am not sure What it is telling me, can someone please explain what it is trying to tell me.

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

namespace Employee_References
{
    class References : Roster
{

     // Field for name, ID, dept, and position  

     private int shift;
     private static const double PAYRATEDAY = 12.75;
     private static const double PAYRATENIGHT = 15.75;



     public void Employee()
     {

     }

   // This will recieve input from my main 
   public int shifts   

    {  
        set {shift = value;}  // this set the recieve value of name one and set it to name1
        get {return shift; }  //this will get name1 and send it to my main.

    }


   // This will recieve input from my main 
   public double payrate1 
    {
        set { PAYRATEDAY = value; } // ERROR!!The left-hand side of an assignment must be a variable, property or indexer
        get { return PAYRATEDAY; }  

    }


   // This will recieve input from my main 
   public double payrate2 
     {
         get { return PAYRATENIGHT; } // ERROR!!The left-hand side of an assignment must be a variable, property or indexer
           set { PAYRATENIGHT = value; }          
     }
}

This is my Main, I am trying to assign input values that are going to be entered in this form, and pass them into my "Roster" class That has an array of 100. How ever I keep getting a compiling error that says " Cannot assign to 'employeeName' because it is a 'method group". I am not sure What It is telling me can some one explain this to me, and give me some pointer on how to do this.

using System;
using System.Windows.Forms;

namespace Employee_References
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            Roster Chart = new Roster();
            Chart.employeeName = name.Text; // Error **Cannot assign to 'employeeName' because it is a 'method group**".  


        }




    }
}
Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44
ErickBRCC
  • 1
  • 5
  • Eric, clearly u have mentioned a method with name "EmployeeName" and in main form u are using it as a variable. you should write Chart.employeeName(name.Text); – Rafay Zia Mir Nov 16 '13 at 18:08
  • I just tried that I get this message," Error 5 The best overloaded method match for 'Employee_References.Roster.employeeName(string[])' has some invalid arguments", What can I do to fix this error??? – ErickBRCC Nov 16 '13 at 18:21
  • Eric look, you have to see the error messages and understand that, what this error is saying thatyou passed array of characters as an array but in your method definition is you are using array of strings, string is an array of characters you can access them using s[i]. But array of strings is a different thing, that means one string in a single location e.g "Hello World". So array of strings and array of characters are different in usage. – Rafay Zia Mir Nov 17 '13 at 11:21

2 Answers2

3

I have done this(Hope that this could help you)

First of all I have made Employee class

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

Then I have made the class ProductionWorker that inherit the two properties from Employee

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

I made a public enum so the code it's more readable

 public enum Shift
 {
    Day,
    Night
 }

In main you can simply create an istance of ProductionWorker and then you can do something like that

        ProductionWorker productionWorker = new ProductionWorker();
        productionWorker.EmployeeName = "Goofy";
        productionWorker.EmployeeNumber = 123;
        productionWorker.HourlyPayRate = 5;
        productionWorker.Shift = Shift.Day;//Or night as you want
        //Then you simply print the properties like that
        Console.WriteLine(productionWorker.EmployeeName);
        //Etc...

EDIT 1

Of course you can assign all numbers that you want in an Enum object

 public enum Shift
 {
    Day=50,
    Night=25
 }

But it's not recommended in these case(in other yes: for example if you want use an enum as Flag). It's more used check the enum and then do the calculations in a variable for example

    float salary = 0F;
    ProductionWorker productionWorker = new ProductionWorker();
    productionWorker.EmployeeName = "Goofy";
    productionWorker.EmployeeNumber = 123;
    productionWorker.HourlyPayRate = 5;
    productionWorker.Shift = Shift.Day;//Or night as you want
    if(productionWorker.Shift.Equals(Shift.Day))
        salary = productionWorker.HourlyPayRate*yourCostant;
    else
        salary = productionWorker.HourlyPayRate*otherYourCostant;
    //Then you simply print the properties like that
    Console.WriteLine(productionWorker.EmployeeName);

EDIT 2 This is the complete enum object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Example_Enum
{
public enum Shift 
{
    Day/* = 50 this is optional by default is 0*/,
    Night/* = 0.12 this is optional by default is 1*/
}
}

EDIT 3 If you want store more then one productionWorker you can do something like that

1)First off all declare a global List like that

private List<ProductionWorker> pw =n ew ProductionWorker();

2)Add a ProductionWorker class to the list(for example)

private void btn_Ok(EventArgs e, object sender)
{ 
    //Before add the class check that all fields are complete!
    pw.Add(new ProductionWorker{EmployeeName = "Goofy",EmployeeNumber = 123, HourlyPayRate = 5, Shift = Shift.Day});
}

This is a simple implementation if you want do it better you must check if EmployeeNumber already exist. You can do it in two ways

1)Using a foreach

bool find = false;
foreach(ProductionWorker w in pw)
   if(v.EmployeeNumber.Equals(EmployeeNumberInput))
   {
      find = true; 
      break;
   }

2)Or you can use System.Linq nowadays it's a much more idiomatic approach

(I'd also suggest following the .NET naming conventions.)

var temp=pw.Find(item=> item.EmployeeNumber.Equals(EmployeeNumberInput))
if(temp!=null)
//ID already exist show an error
else{
    //Add a new object to list
}
Tinwor
  • 7,765
  • 6
  • 35
  • 56
  • Thank You Tinwor, you are awesome and I appreciate your comment, how ever I a novice to all of this, would I be able to assign a numeric wage to the enum class? and should I make a separate class window for it in my visual studio? – ErickBRCC Nov 16 '13 at 18:52
  • Yes you can assign all numbers that you want(see the edit 1). And yes make a other class for the public enum – Tinwor Nov 16 '13 at 21:22
  • Ok I took you example, however this only store one input from the user, how can I make it store atleast 10 different employees? – ErickBRCC Nov 17 '13 at 22:41
1

When you're a bit lost in the woods, it's generally a good idea to take a step back and divide the problem into smaller tasks.

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

  • Employee name
  • Employee number

I'd start with that.

Here's a nice example (I found it googling "c# class with properties") if this still seems a little daunting.

flup
  • 26,937
  • 7
  • 52
  • 74
  • You'd rather want to use [auto-implemented properties](http://msdn.microsoft.com/en-us/library/vstudio/bb384054.aspx). Les typing, less error-prone. – CodeCaster Nov 16 '13 at 18:14
  • I created a class named roster for that, Should I have done it in another way please help me????!!! – ErickBRCC Nov 16 '13 at 18:14
  • I think the text says to create a class named Employee, and to give it two properties, name, and number. – flup Nov 16 '13 at 18:15
  • @CodeCaster is fine too, but we must generally learn to walk before we can run. – flup Nov 16 '13 at 18:17
  • Lol Yes, I apologize I named mines roster. It is holding The both properties employee name, and employee number. – ErickBRCC Nov 16 '13 at 18:18
  • Well, @Tinwor has gone and written down a full answer to your assignment. Looks good to me. About your current class named Roster, the first thing wrong with it is the name. Name the class after what it represents. Because you named the class Roster, you're now adding arrays, trying to put the entire roster into it. It should house one single Employee instead. – flup Nov 16 '13 at 18:26
  • Hey but How will I use The Enum Class? Shoulw I make a new class Window For it, and would I be able to assign a numeric value to the day shift ans well as the night shift? – ErickBRCC Nov 16 '13 at 18:50
  • Converting between data types is called casting. You want to cast the int to an enum value? See for example http://stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp – flup Nov 16 '13 at 20:13