我更改了变量名称,我只是不想显示它
更新:我已经弄明白了,而不是返回方法,我可以直接转到变量。
如果您对解决以下问题感兴趣,请继续。
所以我有这一课(让我们说ClassA):
string EmployeeName;
string EmployeePosition;
string PokemonPromotion;
int EmployeeSalary;
int EmployeeProductivity;
public EmployeeInformation(string name, string job, string promotion, int salary, int productivity)
{
EmployeeName = name;
EmployeePosition = job;
EmployeePromotion = promotion;
EmployeeSalary = salary;
EmployeeProductivity = productivity;
}
这是另一个类(ClassB):
private EmployeeInformation[] Employee;
private int EmployeeAmount;
public Top()
{
Employee = new EmployeeInformation[100];
EmployeeAmount = 0;
}
public void HireEmployee(string name, string job, string promotion, int salary, int productivity)
{
Console.WriteLine("==HIRE PEOPLE==");
Employee[EmployeeAmount] = new EmployeeInformation(name, job, promotion, salary, productivity);
EmployeeAmount++;
}
当我创建一个返回EmployeeName,EmployeePosition,EmployeePromotion,EmployeeSalary和EmployeeProductivity的方法时,它不返回任何内容(如空白)。提前谢谢!
编辑:
以下是返回方法(方法是ClassA的一部分) 希望每个人都清楚
public string ReturnEmployeeName()
{
return EmployeeName;
}
public string ReturnEmployeePosition()
{
return EmployeePosition;
}
public string ReturnEmployeePromotion()
{
return EmployeePromotion;
}
public int ReturnEmployeeSalary()
{
return EmployeeSalary;
}
public int ReturnEmployeeProductivity()
{
return Employee Productivity;
}
编辑2:这是我尝试测试回报的方式。这里的想法是列出for循环中的所有内容。我只是交换" 0"进入循环中的任何东西。
Employee[0].ReturnEmployeeName();
编辑3:这是for循环(在ClassB中)
public void ListEmployees()
{
Console.WriteLine("=LIST OF EMPLOYEES=");
Console.WriteLine("ID" + "\t"
+ "NAME" + "\t"
+ "POSITION" + "\t"
+ "PROMOTION" + "\t");
for (int i = 0; i < EmployeeAmount; i++)
{
Console.WriteLine("{0}", i + 1, "\t"
, Employee[i].ReturnEmployeeName(), "\t"
, Employee[i].ReturnEmployeePosition(), "\t"
, Employee[i].ReturnEmployeePromotion(), "\t");
}
}
循环不会打印工资和生产率,但是当我创建一个方法来查看它是否返回某个东西时,它会像其他方式一样返回空白。
编辑4:每当我这样做时,实际上有一些东西在里面。唯一的问题是当我使用这些方法时。
Console.WriteLine(Employee[0].EmployeeName);
答案 0 :(得分:0)
我认为您需要为此创建属性:
public string EmployeeName{get;set;};
public string EmployeePosition {get;set;};
public string PokemonPromotion {get;set;};
public int EmployeeSalary {get;set;};
public int EmployeeProductivity {get;set;};
然后您就可以设置并获取属性
答案 1 :(得分:0)
public EmployeeInformation HireEmployee(string name, string job, string promotion, int salary, int productivity)
{
Console.WriteLine("==HIRE PEOPLE==");
EmployeeInformation employeeInformation = new EmployeeInformation(name, job, promotion, salary, productivity);
Employee[EmployeeAmount] = employeeInformation;
EmployeeAmount++;
return employeeInformation;
}
你正在寻找这个吗?您可以使用List而不是Array,因为如果要添加100个以上的元素,它将抛出异常。
答案 2 :(得分:0)
通过查看您发布的代码,我们唯一想到的是您可能尝试使用EmployeeInformation
变量索引EmployeeAmount
数组。
鉴于在数组中插入数据后EmployeeAmount
总是增加,它将始终索引一个空值(下一个要插入的值)。只是要确定,在添加至少一条记录后,尝试查看Employee[0]