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

本文关键字:另一个 位图 数组 访问 | 更新日期: 2023-09-27 18:16:26

我有这样一个数组:

        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;

我想要的数组和它的内容在一个不同的类,并从我的主要形式访问它。我不知道是否应该使用方法,函数或使用数组。在我的新类中是否有一些好的方法可以访问例如bildeListe[0] ?

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

最简单的方法是在类中添加属性以返回该数组。这样,即使你出于某种原因改变了数组,也能得到正确的数组。

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

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