简体   繁体   English

C#在类和名称空间中列出,从另一个类访问它

[英]C# List inside class and namespace, accessing it from another class

Please excuse my little knowledge in C#, as this question may be very easy for some of you but I cannot get my head around it. 请原谅我对C#的一点了解,因为对于某些人来说,这个问题可能很容易,但我无法解决。

I have tried and searched many examples and tutorials, but all of them are only partial examples so I could not understand it properly. 我已经尝试并搜索了许多示例和教程,但是所有这些都只是部分示例,因此我无法正确理解。

I have this class "FilesDataStruct": 我有此类“ FilesDataStruct”:

namespace MYPROJECT.FilesListData
{
    class FilesDataStruct
    {
        public int ID;
        public string Name;
    }
}

And the class "FilesDataClass": 和类“ FilesDataClass”:

namespace MYPROJECT.FilesListData
{
    class FilesDataClass
    {
        public List<FilesDataStruct> Files;

        public FilesDataClass()
        {
            Files = new List<FilesDataStruct>();
        }
    }
}

The namespace they are using is " FilesListData ". 他们使用的名称空间是“ FilesListData ”。

So I go to Class " B " in MYPROJECT and declare: 因此,我进入MYPROJECT的B ”类并声明:

private FilesDataClass m_ListWorker;

And then add items to my list: 然后将项目添加到我的列表中:

m_ListWorker = new FilesDataClass();

FilesDataStruct item = new FilesDataStruct();

for (int i = 0; i < 10; i++)
{
    item.ID = i;
    item.Name = "Just_Testing";

    m_ListWorker.Files.Add(item);
}

Debugging this I can see the items are added properly. 对此进行调试,我可以看到项目已正确添加。

Now I need to access this list in the " Form1 " class ( or any other .. ): 现在,我需要在“ Form1 ”类(或任何其他..)中访问此列表:

So I declare again: 所以我再次声明:

 private FilesDataClass m_ListWorker;

But reading the examples and tutorials I found, I have done this: 但是阅读我发现的示例和教程,我已经做到了:

m_ListWorker = new FilesDataClass(); // this is weird, if I call the class again it's normal to be empty

for (int i = 0; i < m_ListWorker.Files.Count; i++)
{
    Console.WriteLine(m_ListWorker.Files[i].ID);
    Console.WriteLine(m_ListWorker.Files[i].Name);
}

And of course, nothing is there. 当然,那里什么也没有。 I am guessing the problem is the constructor of " FilesDataClass " because each time you call " new " it will become a new empty class... 我猜问题是“ FilesDataClass ”的构造函数,因为每次调用“ new ”时,它将变成一个新的空类...

Please explain to me what in the world I am doing wrong, and how should I access my class to retrieve the items in ANY class that I want... 请向我解释世界上我做错了什么,以及如何访问我的班级以检索想要的任何班级中的项目...

You need to pass a reference to the existing class, not create a new instance of it. 您需要传递对现有类的引用,而不是为其创建新实例。

public class Form1 : Form
{
    FilesDataClass m_ListWorker = null;

    public Form1(FilesDataClass f)
    {
        this.m_ListWorker = f;
    }
}

var newForm = new Form1(m_ListWorker);

You have a second issue too. 您还有第二个问题。 That for loop will not work like you expect. for循环将无法正常工作。 You're simply adding the same instance of FilesDataStruct 10 times... in the end, you have 10 references that will all have the same values (the values you assigned in the last iteration of the loop). 您只需添加10次相同的FilesDataStruct实例...最后,您将拥有10个引用,它们将具有相同的值(在循环的上一迭代中分配的值)。

m_ListWorker = new FilesDataClass();

FilesDataStruct item = new FilesDataStruct();

for (int i = 0; i < 10; i++)
{
    item.ID = i;
    item.Name = "Just_Testing";

    m_ListWorker.Files.Add(item);
}

Instantiate the class inside the loop instead: 实例化循环内的类:

m_ListWorker = new FilesDataClass();

for (int i = 0; i < 10; i++)
{
    FilesDataStruct item = new FilesDataStruct();
    item.ID = i;
    item.Name = "Just_Testing";

    m_ListWorker.Files.Add(item);
}

您需要公开B.m_ListWorker(或者更好的是公开属性)。然后,如果它是实例属性,则可以使用someInstanceOfB.ListWorker进行访问;如果是静态的,则可以使用B.ListWorker进行访问。

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

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