Alex
Alex

Reputation: 5227

loading dictionaries from a file with C#

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"

Upvotes: 0

Views: 3670

Answers (2)

nawfal
nawfal

Reputation: 73183

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.

public Dictionary<string, Bitmap> lookup;
public Dictionary<string, Bitmap> lookup2;
public Dictionary<string, Bitmap> lookup3;

 private void button2_Click_1(object sender, EventArgs e)
 {
     lookup = mydict.loadDict(); //now that you have loaded it.
 }

Your class should be:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace getDictData
{
    class mydict
    {
        public static Dictionary<string, Bitmap> loadDict()
        {
            Dictionary<string, Bitmap> lookup = new Dictionary<string, Bitmap>();
            Bitmap l1 = new Bitmap(@"C:\1\1.bmp", true); lookup.Add("1", l1);
            return lookup;
        }
    }
}

And now,

private void button8_Click(object sender, EventArgs e)
{  
    var targer = lookup.ToList(); //hopefully lookup is not null anymore    

    if (lookup.Count == 0) //or whatever way you want to validate depending upon the indexing u use
    {
         MessageBox.Show("No valid items"); //or something similar that you dont get index out of bound exception.
         return;
    }

    string s = targer[0].Key;
    textBox6.Text = "" + s;
}

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:

   class mydict
   {
       public static List<Dictionary<string, Bitmap>> loadAllDicts()
       {
           List<Dictionary<string, Bitmap>> lookups = new List<Dictionary<string, Bitmap>>();

            Dictionary<string, Bitmap> lookup1 = new Dictionary<string, Bitmap>();
            Bitmap l1 = new Bitmap(@"C:\1\1.bmp", true); 
            lookup1.Add("1", l1);
            lookups.Add(lookup1);

            Dictionary<string, Bitmap> lookup2 = new Dictionary<string, Bitmap>();
            Bitmap l2 = new Bitmap(@"C:\1\1.bmp", true); 
            lookup2.Add("1", l2);
            lookups.Add(lookup2);

            //etc

            return lookups;
        }
    }

2) And then

   private void button2_Click_1(object sender, EventArgs e)
   {
        List<Dictionary<string, Bitmap>> lookups = mydict.loadAllDicts();

        lookup = lookups[0];
        lookup2 = lookups[1];
        lookup3 = lookups[2];
   }

Upvotes: 2

Alex
Alex

Reputation: 8116

The problem is that Dictionary<string, Bitmap> lookup = new Dictionary<string, Bitmap>(); is defined in your static method loadDict() and has no reference to outside. How would you get that data? You could add a property to your mydict class.

class mydict
{
    public static Dictionary<string, Bitmap> MyLookup { get; private set; }
    public static void loadDict()
    {
                    MyLookup.Clear();
        Bitmap l1 = new Bitmap(@"C:\1\1.bmp", true);
        MyLookup.Add("1", l1);
    }
}

Now , call mydict.loadDict(); and then you can access your bitmaps through

mydict.MyLookup

Or use a function with a return value.

 public static Dictionary<string, Bitmap> loadDict()
        {
                    var dict = new Dictionary<string, Bitmap>();
            Bitmap l1 = new Bitmap(@"C:\1\1.bmp", true);
            dict .Add("1", l1);
                    return dict;
        }

Upvotes: 0

Related Questions