1
class employee
{
    int id;
    String name;
    int salary;
}

class employeeManager
{
   public void ExceptInputOutput()
    { 
    }
}

I have declared the variable in employee class & want to use that variable to the method ExceptInputOutput() of employeeManager class where both of the class are not main class. Main class will call the method of employeeManager class. Now how can I use the variable id, name & salary to ExceptInputOutput() method.

GSerg
  • 76,472
  • 17
  • 159
  • 346
nahida
  • 19
  • 1
  • 1
    @soumyasambitKunda inheritance maybe an approach OP should take. On other hand based on class names - "employee" and its "manager" is generally parent-child relation not "is-a" relation (indeed "manager" may be "employee" too, but doesn't have to). – Alexei Levenkov Sep 04 '15 at 16:42
  • nahida, Accessing member of other class covered in many SO questions - it is really good idea to search for existing posts first. If your question is actually about something else - you'd be better off asking new one (possibly linking to this). – Alexei Levenkov Sep 04 '15 at 16:44
  • @AlexeiLevenkov You are correct. However the requirement is not clear enough as ther is more than one way available to do that including inheritace, making employee static, or by parameter passing any one of the 3 can solve the propose pbased on the context – soumya sambit Kunda Sep 04 '15 at 16:48
  • @AlexeiLevenkov i have checked the existing post ... but that existing post dosen't give me any clear idea about that. Actually i am nobish in C# so don't know how to use inheritance.. Now i know how to use inheritance in C#.. – nahida Sep 04 '15 at 16:56
  • @user2946329 .... yes i checked your answer & it works... & i learn how to implement inheritance... – nahida Sep 08 '15 at 18:21

3 Answers3

1

Why don't you inject the employee inside the employeeManager class?

public class employee
{
    public int id { get; set; }
    public String name { get; set; }
    public int salary { get; set; }
}

class employeeManager
{
   public void ExceptInputOutput(employee model)
   { 
       // model.id
   }
}
David
  • 10,458
  • 1
  • 28
  • 40
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
1

The variables you have declaed in employee have two usage notes:

C# assumes an access modifier of private when none is specified (for class members) - you thus need to explicitly make those variables public to use them from another class.

In addition, the varuables are not static and thus are part of an instance of emplaoyee - you need to have an employee object to get values from. This can be declared new in ExceptInputOutput(), passed as a parameter, or declared a field in employeeManager.

David
  • 10,458
  • 1
  • 28
  • 40
0

By using inheritance Like this:

public class employee
{
    public int ID {get;set;}
    public String Name {get;set;}
    public int Salary {get;set;}
}

public class employeeManager : employee
{
   public void ExceptInputOutput()
   { 
       //ID,Name,Salary are accessible
   }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109