【问题标题】:Accessing bitmap array in another class? C#在另一个类中访问位图数组? C#
【发布时间】:2013-06-01 07:58:00
【问题描述】:

我有这个数组:

        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;

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

【问题讨论】:

  • 将它传递给主窗体的构造函数。

标签: c# arrays class bitmap


【解决方案1】:

最简单的方法是向您的类添加一个属性以返回该数组。这样,如果您出于某种原因碰巧更改了数组,您总能得到正确的数组。

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

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 仍然没有问题。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 既然我们有静态类,为什么不利用它们呢?每次调用它时都会创建一个数组,然后从那里获取一个 Bitmap 并返回它。即使使用 switch-case 结构,你也会做得更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多