Jorge Hyyest
Jorge Hyyest

Reputation: 351

Why i can't get access to public static variables on library class project from windows forms project?

I have a library class project on the project i added a new User Control. On the User Control top i added this variables:

public static string[] allfiles;
public static BackgroundWorker bgw;

Then in the bottom i have a click event:

private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "Upload")
            {
                allfiles = System.IO.Directory.GetFiles(e.ClickedItem.Text, "*.*", System.IO.SearchOption.AllDirectories);
                bgw.RunWorkerAsync();
            }
        }

The click event is working fine i see that allfiles contianing the files and directories. The problem is that in form1 constructor i can't get access to the variables allfiles and bgw bgw i need it since i want that when the user make the click the backgroundworker1 in form1 will run.

In form1 in the windows forms project i dragged over the dll file of the library class project from the toolbox. And then i tried in the form1 constructor to do:

public Form1()
        {
            InitializeComponent();

            explorerTree1.

But explorerTree1 dosen't contain allfiles or bgw And i can't type just ExplorerTree in form1.

So how can i get access to variables also method/functions from class library project user control ?

Upvotes: 1

Views: 1786

Answers (2)

CodeCaster
CodeCaster

Reputation: 151740

You made them static, so they're on the type:

ExplorerTree.allfiles

If you don't want that, create instance properties:

public string[] AllFiles { get; set; }

Then you can access them through an instance:

explorerTree1.AllFiles;

Upvotes: 1

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149646

Both your fields are declared static, which means they belong to your ExplorerTree type, not an instance of ExplorerTree.

If you want to access them, you'll need to do so on the type:

ExplorerTree.allfiles;

If what you need is to access them on an instance, don't declare them as static. I'd also recommend exposing them as properties, instead of public fields:

public string[] AllFiles { get; set; }
public BackgroundWorker Bgw { get; set; }

Upvotes: 2

Related Questions