I am working on a console app in which I add members to a list through user input. The logic used is :
public class Projectlogic
{
public List<Project> Projects { get; set; } = new List<Project>();
//Add a project to the list Projects.
public void Add(Project project)
{
Projects.Add(project);
}
public List<Project> GetAllProjects()
{
return Projects;
}
}
The main program for adding a project to the list Projects is given below. When I debug the code, the count in Projects is shown as 0. What part of logic am I missing or using incorrectly?
//Adding parameters PID, Name, Sdate, Edate to a project.
var newProject = new Project(PID, Name, Sdate, Edate);
var business2 = new ProjectLogic(); //declared
business2.Add(newProject);
break;
CodePudding user response:
See my comments to the right (shortened the variable name to create more space):
var newProject = new Project(PID, Name, Sdate, Edate);
var b2 = new ProjectLogic(); //A new object with 0 items is created.
//A breakpoint here will ALWAYS show zero items
b2.Add(newProject); //Add the item, but the change won't show until the next line.
//Breakpoints here will also ALWAYS show 0 items.
break; //A breakpoint here will ALWAYS show ONE item.
//However, "break" will kick you out of the current scope.
//This means the business2 object also immediately goes
//out of scope and is thrown away.