I have dictionaries. First I initialize them.
public Dictionary<string, Bitmap> lookup;
public Dictionary<string, Bitmap> lookup2;
public Dictionary<string, Bitmap> lookup3;
Then dictionaries are loaded on button_click event.
private void button2_Click_1(object sender, EventArgs e)
{
lookup = new Dictionary<string, Bitmap>();
Bitmap l1 = new Bitmap(@"C:\1\1.bmp", true); lookup.Add("1", l1);
// add hundreds more
}
the problem is that this code takes up nearly half of my main file space, so I was thinking about creating a separate file to load dictionaries from it.
so I tried to create a separate class (cs) file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace getDictData
{
class mydict
{
public static void loadDict()
{
Dictionary<string, Bitmap> lookup = new Dictionary<string, Bitmap>();
Bitmap l1 = new Bitmap(@"C:\1\1.bmp", true); lookup.Add("1", l1);
}
}
}
in the main file I add:
using getDictData
and use it like this:
private void button2_Click_1(object sender, EventArgs e)
{
mydict.loadDict();
}
nothing seems to be working.. so how do you load methods from different files with C#?
EDIT: I’m using this code to check if something was loaded into a dictionary:
private void button8_Click(object sender, EventArgs e)
{
var targer = lookup.ToList(); // error is thrown here
string s = targer[0].Key;
textBox6.Text = "" + s;
}
the result is always null, no matter how I change the code.
“Value cannot be null. Parameter name: source”
and if I delete dictionary initialization from main file I’m getting
“The name ‘lookup’ does not exist in the current context”
The trouble you are facing is something very trivial. Indeed you can write functions in different files, classes etc and thats how it has to be done. If you try this, it definitely would work.
Your class should be:
And now,
Edit: I guess there are just a few dictionaries to be loaded. You could do this instead to answer your question in comment, but not elegant at all.
1) Return a list of dictionaries from dictionary loading class:
Your Dictionary loading class:
2) And then