开发者

Public LinkedList

I've a problem with my C# Code. At the moment I try to program a Windows Forms Application with more than one Window. Now my problem:

At the first window I've a combobox with some values. When I click on a button, the second window opens and there it should be possible to add a value to th开发者_开发问答is combobox on the first form.

The problem is that in the first window I´ve a LinkedList where my values are in.

Like this:

public LinkedList<String> sample = new LinkedList<String>();


hase.AddFirst("test");
combobox.Items.AddRange(sample.ToArray());

Now, in the second window the LinkedList isn't available, even if I make it public. What is the best way to solve this problem?

Hope you understand my problem...

Harald


Without knowing exactly how to are trying to access the LinkedList, it's hard to say why it isn't working for you.

Let's go over what you have. You have a LinkedList, which is an instance variable on a form. Since this LinkedList is an instance variable, it is associated with the instance of the form.

This example below, will not work because it tries to access it statically:

public class MyForm : Form
{
    public LinkedList<string> _list = new LinkedList<string>();
}

public class MySecondForm : Form
{
    public void Window_Loaded(object sender, EventArgs e)
    {
        MyForm._list.AddFirst("This doesn't work");
        //WRONG! list is an instance variable we are trying to access statically.
    }
}

So, we can see this does not work. We have a few options to get this working. First off, one very bad solution would be to actually make list static. Don't use this option. It's opens the door for concurrency problems, possibly leaking strong references, etc. Generally, using statics (like a singleton) I would discourage for passing data around for these reasons. The Singleton Pattern has a time and a place, but I don't think this is it since it can so easily be avoided.

OK, since we got the bad solution out of the way, let's look at a few possible good ones.

Set the list on MySecondForm. You have a few options for this. The constructor, a property, or a method. For example:

public class MyForm : Form
{
    private LinkedList<string> _list = new LinkedList<string>();

    public void Button1_Click(object sender, EventArgs e)
    {
        var secondForm = new MySecondForm();
        secondForm.SetList(_list);
        secondForm.ShowDialog();
        MessageBox.Show(_list.First.Value);
    }
}

public class MySecondForm : Form
{
    private LinkedList<string> _list;

    public void Window_Loaded(object sender, EventArgs e)
    {
        this._list.AddFirst("This will work");
    }

    public void SetList(LinkedList<string> list)
    {
        _list = list;
    }
}

This is one possible solution. The constructor is another possible solution as Billy suggested.

Because LinkedList is a reference type, any changes you make to it on the instance of MySecondForm will be reflected on the linked list of MyForm.


You can always pass it to the second window. As vcsjones points out below, you should only need to add the ref keyword if you are re-assigning the list. You will need a constructor that takes a linked list as a variable.

SecondWindow secondWindow = new SecondWindow(sample);

Another way would be create a class using the singleton pattern and you can place the linked list in there. You would then have access to it from both windows if it was in a common location.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜