So The same question is available but as I am new to c# or in Programming
can u tell me the answer in my case
here is the list which I want to Access
public class ProjectBusinessLogic
{
private List<Project> ProjectList { get; set; } = new List<Project>(); // List for project
public List<Project> projectlist
{
get { return ProjectList; } //to give access to list
}
so now here is the another class where I want to access the list for comparing the lists
public class ProjectEmployeeBusinessLogic
{
private List<ProjectEmployee> ProjectAddEmployeeList { get; set; } = new List<ProjectEmployee>();
public List<ProjectEmployee> projectaddemployeelist
{
get { return ProjectAddEmployeeList; }
}
so I want to comapre Project list with ProjectAddEmployee list with some fields..
for that I need to access the list from ProjectBusinessLogic class into ProjectEmployeeBusinessLogic class
How to do this??
simply need to create object of that class and access it or anything else??
its a simple thing but I am not able to do it...
CodePudding user response:
I'm guessing you are confusing a class with a object. So if you want to compare the two lists you would need an object of each class. Note that there can be multiple objects of the same class, so you need to compare right objects. Simply creating a new object will result in an empty list, and that is probably not what you want.
If you have a simple UI the code could look something like this:
public class MyUIClass{
private ProjectBusinessLogic projects = new ProjectBusinessLogic();
private ProjectEmployeeBusinessLogic employees = new ProjectEmployeeBusinessLogic();
public void OnAddProjectButtonClick(object sender, System.EventArgs e){
projects.ProjectList.Add(new Project(...));
}
public void OnAddEmployeeButtonClick(object sender, System.EventArgs e){
employees.ProjectAddEmployeeList.Add(new ProjectEmployee(...));
}
public void OnCompareButtonClick(object sender, System.EventArgs e){
var list1 = projects.ProjectList;
var list2 = employees.ProjectAddEmployeeList;
// Do comparison between list1 and list2
// present the result somehow
}
This creates a object of each type when the UI class is created. Have button event-handlers for adding objects to each list, presumably using some text-fields for any constructor parameters. And finally a button event handler for doing the comparison, and presumably presenting the result.
Note that you are exposing the full lists in your classes, so you could just use a public property with a private setter instead:
public List<ProjectEmployee> ProjectAddEmployeeList { get; private set; } = new List<ProjectEmployee>();
using a private field is mostly useful when you want to expose a different type, for example:
private List<ProjectEmployee> projectAddEmployeeList = new List<ProjectEmployee>();
public IReadOnlyList<ProjectEmployee> ProjectAddEmployeeList => projectAddEmployeeList ;
CodePudding user response:
It depends on your application design. If there is a 'main' class that is creating the objects of both ProjectBusinessLogic and ProjectEmployeeBusinessLogic, then you can pass the object of ProjectBusinessLogic to ProjectEmployeeBusinessLogic at the time of object creation in 'main' class.
Otherwise, you can try to create the object of ProjectBusinessLogic inside the constructor of ProjectEmployeeBusinessLogic. This might also work for you since you are not creating a new instance every time.
public class ProjectEmployeeBusinessLogic
{
ProjectBusinessLogic projectBusinessLogic
public ProjectEmployeeBusinessLogic()
{
projectBusinessLogic = new ProjectBusinessLogic();
}
private List<ProjectEmployee> ProjectAddEmployeeList { get; set; } = new
List<ProjectEmployee>();
public List<ProjectEmployee> projectaddemployeelist
{
get { return ProjectAddEmployeeList; }
}
public ComplareLists()
{
var pList = projectBusinessLogic.projectlist();
// your compare logic
}
