Mecanik
Mecanik

Reputation: 1059

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.

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":

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

And the class "FilesDataClass":

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

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

The namespace they are using is "FilesListData".

So I go to Class "B" in MYPROJECT and declare:

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 .. ):

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...

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...

Upvotes: 0

Views: 2126

Answers (2)

Grant Winney
Grant Winney

Reputation: 66511

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. 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).

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

Upvotes: 1

archnae
archnae

Reputation: 391

You need to make B.m_ListWorker public (or better, a public property).Then you can access it as someInstanceOfB.ListWorker if it's an instance property or as B.ListWorker if it's static.

Upvotes: 0

Related Questions