简体   繁体   English

如何从 c# 中的另一个 class 访问私人列表,以便我可以比较两个列表字段?

[英]How to access private list from another class in c# so that I can compare two list fields?

So The same question is available but as I am new to c# or in Programming所以同样的问题是可用的,但因为我是 c# 或编程的新手

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所以现在这里是另一个 class 我想访问列表以比较列表

   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..所以我想将项目列表与带有一些字段的 ProjectAddEmployee 列表进行比较。

for that I need to access the list from ProjectBusinessLogic class into ProjectEmployeeBusinessLogic class为此,我需要从 ProjectBusinessLogic class 访问列表到 ProjectEmployeeBusinessLogic class

How to do this??这个怎么做??

simply need to create object of that class and access it or anything else??只需要创建 class 的 object 并访问它或其他什么?

its a simple thing but I am not able to do it...这是一件简单的事情,但我做不到......

I'm guessing you are confusing a class with a object .我猜您将classobject混淆了。 So if you want to compare the two lists you would need an object of each class .因此,如果您想比较这两个列表,则需要每个classobject Note that there can be multiple objects of the same class, so you need to compare right objects.注意同一个class可以有多个对象,所以需要比较对的对象。 Simply creating a new object will result in an empty list, and that is probably not what you want.简单地创建一个新的 object 将导致一个空列表,这可能不是你想要的。

If you have a simple UI the code could look something like this:如果你有一个简单的 UI,代码可能看起来像这样:

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.这会在创建 UI class 时创建每种类型的 object。 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 ;
                      

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. 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.否则,您可以尝试在 ProjectEmployeeBusinessLogic 的构造函数中创建 ProjectBusinessLogic 的 object。 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
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM