Reputation: 39
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
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
Reputation: 19486
The dictionary is static
, so you can just use the type name:
Form1.documents.Add(pageGuid, addcapitulo.blankRtfText);
Upvotes: 0