C# 我正在创建员工图表,我真的不知道';我不知道怎么了。我对这个完全陌生。

C# 我正在创建员工图表,我真的不知道';我不知道怎么了。我对这个完全陌生。,c#,class,derived,C#,Class,Derived,这是我的作业,我很难让我的班级和main一起工作,有人能帮我吗?这将在周二到期,我尝试过的每一种方法都遇到了麻烦。我所有的班级和表格都张贴了。请帮帮我,我完全迷路了,很沮丧 员工和生产工人类 创建具有以下数据属性的Employee类: 员工姓名 员工编号 接下来,创建从Employee类派生的名为ProductionWorker的类 ProductionWorker类应具有用于保存以下数据的属性: 移位数(整数,如1、2或3) 小时工资率工作日分为日班和夜班 Shift属性将保存一个整数

这是我的作业,我很难让我的班级和main一起工作,有人能帮我吗?这将在周二到期,我尝试过的每一种方法都遇到了麻烦。我所有的班级和表格都张贴了。请帮帮我,我完全迷路了,很沮丧

  • 员工和生产工人类
  • 创建具有以下数据属性的Employee类:

    • 员工姓名
    • 员工编号
    接下来,创建从Employee类派生的名为ProductionWorker的类

    ProductionWorker类应具有用于保存以下数据的属性:

    • 移位数(整数,如1、2或3)
    • 小时工资率工作日分为日班和夜班
    Shift属性将保存一个整数值,表示员工工作的班次。白班为1班,夜班为2班

    创建一个应用程序,该应用程序创建ProductionWorker类的对象,并允许用户为该对象的每个属性输入数据。检索对象的属性并显示其值


    这是我的员工参考图表,用来存储他们的姓名和身份证号码。 我在这个类上没有遇到编译错误,但是我不确定是否有 这样做是正确的,因为在我的主要代码中,我得到了一个编译错误

    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];
            }
        }
    
    
         }
    
    
    
    } 
    
    这是我的主要任务,我正在尝试分配将要在此表单中输入的输入值,并将它们传递到我的“花名册”类中,该类具有100个数组。我总是收到一个编译错误,上面写着“无法分配给'employeeName',因为它是一个'methodgroup”。我不知道它告诉了我什么,有人能给我解释一下,并给我一些如何做到这一点的指针

    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**".  
    
    
            }
    
    
    
    
        }
    }
    

    当你在森林里迷路时,通常最好退一步,把问题分成更小的任务

    创建具有以下数据属性的Employee类:

    • 员工姓名
    • 员工编号
    我从这个开始


    这里有一个(我在谷歌上发现的“”)如果这看起来还是有点令人畏惧的话。

    当你在森林中有点迷路时,通常最好退后一步,把问题分成更小的任务

    创建具有以下数据属性的Employee类:

    • 员工姓名
    • 员工编号
    我从这个开始

    这里有一个(我发现它在谷歌上搜索“”)如果这看起来还是有点令人畏惧的话。

    我已经这样做了(希望这能帮助你)

    首先,我制作了员工类

     public class Employee
     {
        public string EmployeeName { get; set; }
        public int EmployeeNumber { get; set; }
     }
    
    然后我创建了ProductionWorker类,它从Employee继承了这两个属性

    public class ProductionWorker : Employee
    {
        public float HourlyPayRate { get; set; }
        public Shift Shift { get; set; }
    }
    
    我创建了一个公共枚举,这样代码更可读

     public enum Shift
     {
        Day,
        Night
     }
    
    大体上,你可以简单地创建一个ProductionWorker,然后你可以做类似的事情

            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...
    
    private List<ProductionWorker> pw =n ew ProductionWorker();
    
    编辑1

    当然,您可以在枚举对象中指定所需的所有数字

     public enum Shift
     {
        Day=50,
        Night=25
     }
    
    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*/
    }
    }
    
    但在这种情况下不建议这样做(在其他情况下是:例如,如果您想使用枚举作为标志)。例如,它更常用于检查枚举,然后在变量中进行计算

        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);
    
    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});
    }
    
    编辑2 这是完整的枚举对象

     public enum Shift
     {
        Day=50,
        Night=25
     }
    
    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*/
    }
    }
    
    编辑3 如果你想储存一个以上的生产工人,你可以这样做

            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...
    
    private List<ProductionWorker> pw =n ew ProductionWorker();
    
    1) 首先,声明一个这样的全局列表

            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...
    
    private List<ProductionWorker> pw =n ew ProductionWorker();
    
    这是一个简单的实现,如果您想做得更好,必须检查EmployeeNumber是否已经存在。你可以用两种方法来做

    1) 使用foreach

    bool find = false;
    foreach(ProductionWorker w in pw)
       if(v.EmployeeNumber.Equals(EmployeeNumberInput))
       {
          find = true; 
          break;
       }
    
    2) 或者你可以使用
    System.Linq
    现在这是一种更加惯用的方法

    (我还建议遵循.NET命名约定。)

    我已经这样做了(希望这能帮助你)

    首先,我制作了员工类

     public class Employee
     {
        public string EmployeeName { get; set; }
        public int EmployeeNumber { get; set; }
     }
    
    然后我创建了ProductionWorker类,它从Employee继承了这两个属性

    public class ProductionWorker : Employee
    {
        public float HourlyPayRate { get; set; }
        public Shift Shift { get; set; }
    }
    
    我创建了一个公共枚举,这样代码更可读

     public enum Shift
     {
        Day,
        Night
     }
    
    大体上,你可以简单地创建一个ProductionWorker,然后你可以做类似的事情

            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...
    
    private List<ProductionWorker> pw =n ew ProductionWorker();
    
    编辑1

    当然,您可以在枚举对象中指定所需的所有数字

     public enum Shift
     {
        Day=50,
        Night=25
     }
    
    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*/
    }
    }
    
    但在这种情况下不建议这样做(在其他情况下是:例如,如果您想使用枚举作为标志)。例如,它更常用于检查枚举,然后在变量中进行计算

        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);
    
    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});
    }
    
    编辑2 这是完整的枚举对象

     public enum Shift
     {
        Day=50,
        Night=25
     }
    
    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*/
    }
    }
    
    编辑3 如果你想储存一个以上的生产工人,你可以这样做

            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...
    
    private List<ProductionWorker> pw =n ew ProductionWorker();
    
    1) 首先,声明一个这样的全局列表

            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...
    
    private List<ProductionWorker> pw =n ew ProductionWorker();
    
    这是一个简单的实现,如果您想做得更好,必须检查EmployeeNumber是否已经存在。你可以用两种方法来做

    1) 使用foreach

    bool find = false;
    foreach(ProductionWorker w in pw)
       if(v.EmployeeNumber.Equals(EmployeeNumberInput))
       {
          find = true; 
          break;
       }
    
    2) 或者你可以使用
    System.Linq
    现在这是一种更加惯用的方法

    (我还建议遵循.NET命名约定。)


    Eric,很明显,你提到了一个名为“EmployeeName”的方法,在主要形式中,你使用它作为变量。你应该写Chart.employeeName(name.Text);我刚刚尝试得到这样一条消息,“错误5‘Employee_References.Floster.employeeName(string[])的最佳重载方法匹配’有一些无效参数”,我能做些什么来修复这个错误???Eric看,你必须看到错误消息并理解这一点,此错误的意思是,您将字符数组作为数组传递,但在方法定义中,您使用的是字符串数组,字符串是可以使用s[i]访问的字符数组。但是字符串数组是另一回事,它意味着一个字符串位于一个位置,例如“Hello World”。所以字符串数组和字符数组在用法上是不同的。Eric,很明显你提到了一个名为“EmployeeName”的方法