Alex
Alex

Reputation: 5227

loading dictionaries at startup with C#

I have 2 Dictionaries (10 entries in Dict1, 30 in Dict2). Right now I'm using this code to load them:

private void button1_Click(object sender, EventArgs e)
{
    Bitmap l0 = new Bitmap(@"C:\0money\0.bmp", true);
    // +100 more
    Dictionary<string, Bitmap> lookup = new Dictionary<string, Bitmap>();
    lookup.Add("0", l0);
    // +100 more
}

I'm planning on creating 3 more dictionaries, so total entry count will be up to 100+!

How do I load all these dictionaries at program Startup, rather that loading (repeatedly) on button_click event?

Edit: As many of you have suggested - I tried putting code on Form_Load event and as the result I'm getting "The name 'lookup' does not exist in the current context" error. I can't execute code on button_click event.

Upvotes: 2

Views: 1107

Answers (5)

Robar
Robar

Reputation: 1971

If you're using WinForms, put your code into the form load event.

private void Form1_Load(object sender, System.EventArgs e) {
    Dictionary<string, Bitmap> lookup = new Dictionary<string, Bitmap>() {
        {"0", new Bitmap(@"C:\0money\0.bmp", true)},
        {"1", new Bitmap(@"C:\0money\1.bmp", true)}
    }
}

UPDATE

If you want to use your Dictionary in your button_click event, you have to keep it on the class instance or define it static as Davy8 already mentioned.

public class MyFancyForm {

    private Dictionary<string, Bitmap> lookup;

    private void Form1_Load(object sender, System.EventArgs e) {
        // init dictionary
        lookup = new Dictionary<string, Bitmap>() {
            {"0", new Bitmap(@"C:\0money\0.bmp", true)},
            {"1", new Bitmap(@"C:\0money\1.bmp", true)}
        }
    }

    private void button1_Click(object sender, EventArgs e) {
        // do something with lookup
    }
}

Upvotes: 1

Sebastian Siek
Sebastian Siek

Reputation: 2075

You might want to store it in Application context (if it's going to be reused by whole app and it's not user specific).

The best way would be to do it on Application Start, which you can handle in Global.asax.

Hope it helps.

UPDATE I don't think that actually storing whole Bitmap objects is a good idea ! You might want to think again about the architecture of your app and what you are trying to achieve.

WEB FARM SCENARIO In webfarm scenario, that is going to be a different scenario. Each of your apps will have a different instance of Application variables.

I'm then tempted to suggest having a session state server and keeping that in session (which will then be shared accross all apps) - you might run into some issues with standard session state server though

You might also cosider copying resources (shared by apps) on to the network path or storing as binary in database and then loading them on each server node application start - you would still have an instance of these in each server node

Upvotes: 2

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

You could Cache them to be shared between the users. Bear in mind that if you will deploy on a server farm you have to use a "component" to manage the cache across the web farm.

I won't use the Application context for the resons below:

Application state is not shared among multiple servers serving the same application, as in a Web farm, or among multiple worker processes serving the same application on the same server, as in a Web garden. Your application therefore cannot rely on application state containing the same data for application state across different servers or processes. If your application will run in multi-processor or multi-server environments, consider using a more scalable option, such as a database, for data that must preserve fidelity across the application.

http://msdn.microsoft.com/en-us/library/ms178594.aspx

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

Assuming that your list of files are named from 0.bmp to 100.bmp you can do something like:

string startupFolder = @"C:\0money\";
Dictionary<string, Bitmap> lookup = new Dictionary<string, Bitmap>();
for(int i = 0; i <= 100; i++)
{
    Bitmap l = new Bitmap(startupFolder + i + ".bmp", true); 
    lookup.Add(i.ToString(), l);
} 

You might want to put this code in the form_load in order to add them in the start up.

Upvotes: 0

David Ly
David Ly

Reputation: 31586

You can either keep it on the class instance or static, depending on how it's used.

private static Dictionary<string, Bitmap> _lookup = new Dictionary<string, Bitmap>();
static MyClass()
{
    lookup.Add("0", l0);
    // +100 more
}

If the instance of the class is only created once then make it non-static. Here I'm assuming the class this is in is called MyClass change as appropriate.

Upvotes: 2

Related Questions