Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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的数组_C#_Arrays_Struct - Fatal编程技术网

C# 将类分配给结构c的数组

C# 将类分配给结构c的数组,c#,arrays,struct,C#,Arrays,Struct,我在使用Form1\u加载中声明的数组时遇到问题。我的目标是能够为数组赋值,然后从数组中的某些值中减去 数组是一个结构数组。我想我必须在别处公开声明数组。现在我想尝试在Form1_负载内完成大部分工作 我试图实现的是,如果用户单击一张图片,它应该从20开始更新剩余的项目数量,并将总数添加到标签中。有5种不同的图片,他们可以点击,这就是阵列需要的地方 结构: struct Drink { public string name; public int

我在使用Form1\u加载中声明的数组时遇到问题。我的目标是能够为数组赋值,然后从数组中的某些值中减去

数组是一个结构数组。我想我必须在别处公开声明数组。现在我想尝试在Form1_负载内完成大部分工作

我试图实现的是,如果用户单击一张图片,它应该从20开始更新剩余的项目数量,并将总数添加到标签中。有5种不同的图片,他们可以点击,这就是阵列需要的地方

结构:

     struct Drink
    {
        public string name;
        public int cost;
        public int numberOfDrinks = 20;
    }
该结构位于命名空间内,位于分部类的上方* 加载事件:

        private void Form1_Load(object sender, EventArgs e)
    {
        const int SIZE = 5;
        Drink[] drink = new Drink[SIZE];


    }
这就是我想要阵列的地方* 以下是单击图片时应执行的操作示例:

        private void picCola_Click(object sender, EventArgs e)
    {
        drink[0].cost = 1.5;
        drink[0].name = "";
    }
但是,将显示消息“饮料”名称在当前上下文中不存在。阵列是否需要公开?
当您在函数Form1\u Load中声明数组时,它将仅成为该函数的本地。没有人能看到它。您需要将变量的范围更改为全局变量,而不需要是公共变量

private Drink[] drink;
private void Form1_Load(object sender, EventArgs e)
    {
        const int SIZE = 5;
        drink = new Drink[SIZE];
    }
但是,您可以在其他地方实例化它

public partial class Form1 : Form
{
    public Drink[] drinks;
    public const int SIZE = 5;

    private void Form1_Load( object sender, EventArgs e )
    {
        drinks = new Drink[ SIZE ];
    }

    private void picCola_Click( object sender, EventArgs e )
    {
        drinks[0].cost = 1.5;
        drinks[0].name = "";
    }
}
您需要正确地确定对象的范围!通过在Form1_Load中声明它,在调用其他方法时它不存在。您必须将其放在类的范围级别,从而使其成为类的字段。这样,Form1调用的所有方法都可以看到它。范围用大括号表示:{}。考虑以下事项:

{
    int a = 7;
    {
        int b = 5;
        {
            int c = 6;

            a = 1; // OK: a exists at this level
            b = 2; // OK: b exists at this level
            c = 3; // OK: c exists at this level
        }

        a = 1; // OK: a exists at this level
        b = 2; // OK: b exists at this level
        c = 3; // WRONG: c does not exist at this level
    }

    a = 1; // OK: a exists at this level
    b = 2; // WRONG: b does not exist at this level
    c = 3; // WRONG: c does not exist at this level
}

a = 1; // WRONG: a does not exist at this level
b = 2; // WRONG: b does not exist at this level
c = 3; // WRONG: c does not exist at this level

结构类型的变量或存储位置本质上是一组用胶带粘在一起的变量。如果一个声明结构对{public int X,Y};然后一个是指定变量声明对myPair;将有效地声明两个int变量myPair.X和myPair.Y,尽管其中一个可以在方便时将它们作为一对使用。如果其中一个声明了一对[]myArray,那么数组的每个项目myArray[index]将包含两个整数,myArray[index].X和myArray[index].Y


其目的是作为一组与管道胶带粘在一起的变量的结构应该简单地将这些变量声明为公共字段。公开字段结构是表示该行为的最佳数据类型。用于其他目的的结构通常应遵循MS指南。我不太清楚您打算如何使用数据类型,所以我不能说结构是否合适,尽管尝试在结构定义中初始化numberOfDrinks是行不通的。类对象实例及其字段只有在类被要求创建自身的实例时才存在——类可以控制这个过程。相反,结构定义指出,结构类型的存储位置应被视为一组与管道胶带粘在一起的变量,但它是该存储位置的所有者,而不是控制其创建的结构类型。

可变结构是邪恶的。考虑到它的用途和使用方式,它几乎肯定是一个类,而不是一个结构