Rinne Rinne - 1 year ago 211
C# Question

Nothing is returning?

I changed the variable names, I simply do not wish to show it

UPDATE: I have figured it out, instead of a return method I can just go directly to the variable. Thank you for the help everyone!
If you're interested in solving the following problem then go ahead :)

So I have this one class(let's say 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;

And this on another class(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);

When I made a method for returning EmployeeName, EmployeePosition, EmployeePromotion, EmployeeSalary and EmployeeProductivity it returns nothing(as in blank). Thank you in advance!


Here are the methods for returning(the methods are part of ClassA)
Hope it becomes clearer to everyone

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;

Edit 2: This is how I try to test the returns. The idea here is to list everything in a for loop. I just exchange the "0" into whatever it is I put in the loop.


Edit 3: This is the for loop(in 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");

The loop does not print the salary and the productivity, however when I made a method to see if it returns something it returns blank like the rest.

Edit 4: Whenever I do this there's actually something in it. The only problem is when I use the methods.


Answer Source

Well by just looking at the code that you posted the only thing that comes in mind is that you may trying to index the EmployeeInformation array by using the EmployeeAmountvariable.

Given that EmployeeAmount is always increased after you insert data in the array it will always index an empty value (the next to be inserted). Just to be sure, after you add at least one record try to see what values you have in Employee[0]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download