Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在另一个类中访问位图数组?C#_C#_Arrays_Class_Bitmap - Fatal编程技术网

在另一个类中访问位图数组?C#

在另一个类中访问位图数组?C#,c#,arrays,class,bitmap,C#,Arrays,Class,Bitmap,我有这个阵列: Bitmap[] bildeListe = new Bitmap[21]; bildeListe[0] = Properties.Resources.ål; bildeListe[1] = Properties.Resources.ant; bildeListe[2] = Properties.Resources.bird; bildeListe[3] = Properties.Resources

我有这个阵列:

        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;

我希望该数组和它的内容在不同的类中,并从我的主窗体访问它。我不知道是否应该使用方法、函数或数组使用什么。在我的新类中,有什么好方法可以访问Instance bildeListe[0]吗?

最简单的方法是向类中添加属性以返回该数组。这样,如果出于某种原因更改了数组,则始终可以获得正确的数组

如果要使用返回图像的方法,请不要使用其他建议的方法。它会导致创建许多无用的对象。一种方法是使用静态数组和方法

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];

    }
}

通过这种方式,所有内容只初始化一次,它们可以被称为MYBitmap.MYarray(2);不创建类的实例。我不知道您是否实例化了该类(可能它包含其他内容),但在这里使用static仍然没有问题。

将数组放入该类的方法中,然后以主窗体创建一个对象

   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];

        }
    }
在你的主窗体中,用你想要的索引调用它

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

将它传递到主窗体的构造函数中。既然我们有静态类,为什么不利用它们呢?此外,每次调用时都会创建一个数组,然后从中获取一个位图并返回它。即使有开关箱结构,你也会过得更好。