Kawyllainy Vi
Kawyllainy Vi

Reputation: 39

add items in the dictionary from another form

I'm trying to access my dictionary of another form, but I can not ...

Form1.cs

public partial class Form1 : Form
    {
        public DataNode rootNode;
        public static Dictionary<string, string> documents = new Dictionary<string, string>();


        public Form1() {

        }

form2.cs

public partial class addCapitulo : Form
    {
        Form1 addcapitulo;

        public addCapitulo(Form1 treeList)
        {
            InitializeComponent();
            addcapitulo = treeList;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string pageGuid = Guid.NewGuid().ToString();
            addcapitulo.rootNode.Nodes.Add(new DataNode("Testando", pageGuid));
            addcapitulo.documents.Add(pageGuid, addcapitulo.blankRtfText);
        }
    }

Error: cannot be accessed with an instance reference; qualify it with a type name instead.

How can I accomplish this access ? able to access and use the dictionary in any form ...

Upvotes: 0

Views: 291

Answers (2)

Mrinal Kamboj
Mrinal Kamboj

Reputation: 11482

To access a Static type you need:

Form1.documents

You cannot access using instance of Form1 as the error suggest.

I would rather suggest to make the Dictionary Non-Static:

public Dictionary<string, string> documents = new Dictionary<string, string>();

Then your current code would work as is. I don't find rationale for Dictionary to be defined static

Upvotes: 2

itsme86
itsme86

Reputation: 19486

The dictionary is static, so you can just use the type name:

Form1.documents.Add(pageGuid, addcapitulo.blankRtfText);

Upvotes: 0

Related Questions