开发者

How to add to a list not yet created. Please help!

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


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


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


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.


employeeList must be a member of your class


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


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


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


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜