-2

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.

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);
    EmployeeAmount++;
}

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

Edit:

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.

Employee[0].ReturnEmployeeName();

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.

Console.WriteLine(Employee[0].EmployeeName);
slavoo
  • 5,798
  • 64
  • 37
  • 39
Rinne
  • 1
  • 1
  • 4

3 Answers3

0

i think you need to create properties for that:

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;};

then you will be able to set and get the properties

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;
    }

Are you looking for this? You can use List instead of Array, because it will throw exception if you want to add more than 100 elements.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

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]

Isaak
  • 66
  • 2
  • when I do Console.WriteLine(Employee[0]); it just prints ".EmployeeInformation" – Rinne Nov 05 '16 at 09:37
  • You have to access the single properties of the object. Try Console.WriteLine(Employee[0].EmployeeName) (after you somehow rendered it public) – Isaak Nov 05 '16 at 09:53
  • Oh hey! It does actually work. The problem is, it's just not returning anything using the methods I used above. – Rinne Nov 05 '16 at 09:58
  • You have also an error at this line in your `ListEmployees()` function: `Console.WriteLine("{0}", i + 1, "\t" , Pokemon[i].ReturnEmployeeName(), "\t" , Pokemon[i].ReturnEmployeePosition(), "\t" , Pokemon[i].ReturnEmployeePromotion(), "\t");` It should be: `Console.WriteLine("{0}\t{1}\t{2}\t{3}", i + 1, Pokemon[i].ReturnEmployeeName(), Pokemon[i].ReturnEmployeePosition(), Pokemon[i].ReturnEmployeePromotion());` – Isaak Nov 05 '16 at 10:00
  • Or just: `Console.WriteLine((i + 1) + "\t" + Pokemon[i].ReturnEmployeeName() + "\t" + Pokemon[i].ReturnEmployeePosition() + "\t" + Pokemon[i].ReturnEmployeePromotion());` – Isaak Nov 05 '16 at 10:03
  • Thank you so much! I am just stupid, instead of using a separate method I can just go directly for the variable – Rinne Nov 05 '16 at 10:22
  • Yes, you can use the class variable directly. The problem is that, depending on the complexity of your project and whether your code must be reused, it is a better practice to access your single fields by using properties. For example, look at the answers that Steve and Surjeet gave – Isaak Nov 05 '16 at 10:29