tags:

views:

109

answers:

11

I am trying to program a simple employee registry and I want to use a generic List to "save" the persons I am creating.

The manager class got one constructor and one method (see below). The constructor creates the List and the method adds to it, or should be adding to it. The problem is that I cannot do it like below because Visual Studio says that employeeList does not exist in the current context. How am I else going to write this?

public EmployeeManager()
{
     List<string> employeeList = new List<string>();
}

public void AddEmployee()
{
     employeeList.add("Donald");
}
+12  A: 

You need to make employeeList a member variable of the class:

class EmployeeManager
{
    // Declare this at the class level
    List<string> employeeList;

    public EmployeeManager()
    {
         // Assign, but do not redeclare in the constructor
         employeeList = new List<string>();
    }

    public void AddEmployee()
    {
         // This now exists in this scope, since it's part of the class
         employeeList.add("Donald");
    }
}
Reed Copsey
@Reed Goodness that was fast. I struggled with formatting before giving up and typing an explanation of what I was trying to do :)
Dave McClelland
This would still throw a null reference if the list hasn't been instantiated yet. Make the list a lazy-constructed private property.
@devinb only if another constructor was used, as it stands this will never throw a null ref.
Adam
Thanks Reed! This solved it!
@devinb, no it wouldn't - this isn't a static class, the member variable is instantiated in the constructor which *must* have been called prior to the AddEmployee method being called
Rob
@user If @Reed's post answered your question, mark it as an answer. It will give both of you reputation and let people in the future know which answer ultimately helped you out.
Dave McClelland
+1  A: 

Did you declare the employeeList as a member of the class?

private List<string> employeeList = new List<string();

public EmployeeManager()
{

}

public void AddEmployee()
{
     employeeList.add("Donald");
}
Matthew Jones
+1  A: 
List<string> employeeList = new List<string>(); 

public EmployeeManager() 
{ 
} 

public void AddEmployee() 
{ 
     employeeList.add("Donald"); 
} 

or, alternately

List<string> employeeList;

public EmployeeManager() 
{ 
     employeeList = new List<string>(); 
} 

public void AddEmployee() 
{ 
     employeeList.add("Donald"); 
} 

As you defined it, employeeList lives only in the ctor. Once it completes, employeeList goes away and it's memory is garbabge collected. By moving the declaration to the class level, it lives for the whole life of the object.

James Curran
+1  A: 

employeeList must be a member of your class

Arseny
+1  A: 

Declare the list outside the scope of the add employee function. Then if you instantiate it in the constructor, you'll be okay.

Dave McClelland
A: 

The reason you are getting this error, is because the scope of employeeList is only in the constructor. In order to use employeeList it must be defined in a larger scope, like so:

class EmployeeManager
{
    private List<string> employeeList;

    public EmployeeManager()
    {
         employeeList = new List<string>();
    }

    public void AddEmployee()
    {
         employeeList.add("Donald");
    }
}
KLee1
A: 

The problem that you have is with scope. When the constructor fires, it creates the employeeList List, then exits. At that point, the employeeList disapears from memory (the stack).

What you need is to declare the employeeList at class level like this :

List<string> employeeList = null;
public EmployeeManager()
{
     employeeList = new List<string>();
}

public void AddEmployee()
{
     employeeList.add("Donald");
}
Eric
A: 

You need it to be a member level variable

public class EmployeeManager
{
    private List<string> _employeeList;
    public EmployeeManager()  
    {  
        _employeeList = new List<string>();  
    }  

    public void AddEmployee()  
    {  
        _employeeList.add("Donald");  
    } 
}
Jimmy Hoffa
A: 

You need to instantiate your EmployeeManager in a scope that is accessible by your Add method -- you are simply defining the class.

George
A: 

Your EmployeeManager class needs a field or property to correspond to employeeList. Like so:

public class EmployeeManager
{
   private List<string> employeeList; //Create Field

   public EmployeeManager()
   {
     this.employeeList = new List<string>();
   }

   public void AddEmployee()
   {
     this.employeeList.Add("Donald");
   }
}
AllenG
A: 

You've run into something known as variable scope. Currently your list is scoped to the constructor. You need to scope it to the class by defining it as a class member, other answers have provided code samples.

http://www.blackwasp.co.uk/CSharpVariableScopes.aspx

http://msdn.microsoft.com/en-us/library/aa691132(VS.71).aspx

Adam