Praise
Praise

Reputation: 557

Accessing bitmap array in another class? C#

I have this array :

        Bitmap[] bildeListe = new Bitmap[21];

        bildeListe[0] = Properties.Resources.ål;
        bildeListe[1] = Properties.Resources.ant;
        bildeListe[2] = Properties.Resources.bird; 
        bildeListe[3] = Properties.Resources.bear;
        bildeListe[4] = Properties.Resources.butterfly;
        bildeListe[5] = Properties.Resources.cat;
        bildeListe[6] = Properties.Resources.chicken;
        bildeListe[7] = Properties.Resources.dog;
        bildeListe[8] = Properties.Resources.elephant;
        bildeListe[9] = Properties.Resources.fish;
        bildeListe[10] = Properties.Resources.goat;
        bildeListe[11] = Properties.Resources.horse;
        bildeListe[12] = Properties.Resources.ladybug;
        bildeListe[13] = Properties.Resources.lion;
        bildeListe[14] = Properties.Resources.moose;
        bildeListe[15] = Properties.Resources.polarbear;
        bildeListe[16] = Properties.Resources.reke;
        bildeListe[17] = Properties.Resources.sheep;
        bildeListe[18] = Properties.Resources.snake;
        bildeListe[19] = Properties.Resources.spider;
        bildeListe[20] = Properties.Resources.turtle;

I want that array and it´s content in a diffenrent class, and access it from my main form. I don´t know if should use method, function or what to use with arrays. Are there some good way for me to access for instanse bildeListe[0] in my new class?

Upvotes: 0

Views: 267

Answers (2)

ayman
ayman

Reputation: 58

Put your array in a method in the class, and then create an object in your main form

   class MYBitamp
    {
            public Bitmap MYarray (int index){
          Bitmap[] bildeListe = new Bitmap[21];

        bildeListe[0] = Properties.Resources.ål;
        bildeListe[1] = Properties.Resources.ant;
        bildeListe[2] = Properties.Resources.bird; 
        bildeListe[3] = Properties.Resources.bear;
        bildeListe[4] = Properties.Resources.butterfly;
        bildeListe[5] = Properties.Resources.cat;
        bildeListe[6] = Properties.Resources.chicken;
        bildeListe[7] = Properties.Resources.dog;
        bildeListe[8] = Properties.Resources.elephant;
        bildeListe[9] = Properties.Resources.fish;
        bildeListe[10] = Properties.Resources.goat;
        bildeListe[11] = Properties.Resources.horse;
        bildeListe[12] = Properties.Resources.ladybug;
        bildeListe[13] = Properties.Resources.lion;
        bildeListe[14] = Properties.Resources.moose;
        bildeListe[15] = Properties.Resources.polarbear;
        bildeListe[16] = Properties.Resources.reke;
        bildeListe[17] = Properties.Resources.sheep;
        bildeListe[18] = Properties.Resources.snake;
        bildeListe[19] = Properties.Resources.spider;
        bildeListe[20] = Properties.Resources.turtle;
            return bildeListe[index];

        }
    }

and in your main form call it with the index you want

        MYBitamp aabc = new MYBitamp();
       aabc.MYarray(5);

Upvotes: 0

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31193

The simplest way would be to add a property to your class to return that array. That way you always get the correct array if you happen to change it for some reason.

If you want to use a method for returning the image, don't use the other suggested method. It causes many useless objects to be created. One way is to use a static array and method.

class MYBitamp
{
    static Bitmap[] bildeListe = new Bitmap[] {
            Properties.Resources.ål,
            Properties.Resources.ant,
            Properties.Resources.bird,
            Properties.Resources.bear,
            Properties.Resources.butterfly,
            Properties.Resources.cat,
            Properties.Resources.chicken,
            Properties.Resources.dog,
            Properties.Resources.elephant,
            Properties.Resources.fish,
            Properties.Resources.goat,
            Properties.Resources.horse,
            Properties.Resources.ladybug,
            Properties.Resources.lion,
            Properties.Resources.moose,
            Properties.Resources.polarbear,
            Properties.Resources.reke,
            Properties.Resources.sheep,
            Properties.Resources.snake,
            Properties.Resources.spider,
            Properties.Resources.turtle
        };

    public static Bitmap MYarray(int index)
    {
        return bildeListe[index];

    }
}

This way everything is initialized only once and they can be called just my MYBitmap.MYarray(2); without creating an instance of the class. I don't know if you do instantiate the class (maybe it contains something else), but there's still no problem using static here.

Upvotes: 1

Related Questions