简体   繁体   English

将类分配给结构体数组c#

[英]assigning classes to an array of structs c#

I'm having an issue using an array declared in the "Form1_Load". 使用“ Form1_Load”中声明的数组时出现问题。 My Goal is to be able to assign values to the array and then subtract from certain values within it. 我的目标是能够为数组分配值,然后从数组中的某些值中减去。

The array is an array of structs. 该数组是一个结构数组。 I am thinking that I have to publicly declare the array elsewhere. 我认为我必须在其他地方公开声明该数组。 Right now I wanna try to do the majority of this within the "Form1_load". 现在,我想尝试在“ Form1_load”中完成大部分操作。

What I am trying to achieve would be if a user clicks a picture, it should update how many items are left(starting at 20) and add the total to a label. 我要实现的目标是,如果用户单击图片,它应该更新剩余的项目数量(从20开始),并将总数添加到标签中。 There is 5 different pictures they can click to do so, which is where the array comes in need. 他们可以单击以获取5张不同的图片,这是需要阵列的地方。

the structure: 结构:

     struct Drink
    {
        public string name;
        public int cost;
        public int numberOfDrinks = 20;
    }
  • The structure is within the namespace, above the partial class. 该结构在局部类上方的名称空间中。 * *

load event: 加载事件:

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


    }
  • This is where I want to have the array* 这是我想要的阵列*

here is an example of what should happen if a picture is clicked: 这是单击图片时应该发生的示例:

        private void picCola_Click(object sender, EventArgs e)
    {
        drink[0].cost = 1.5;
        drink[0].name = "";
    }
  • However, the message "The name 'drink' does not exist in the current context" appears. 但是,出现消息“名称'drink'在当前上下文中不存在”。 Does the array need to be public? 阵列需要公开吗?

When you declare the array drink inside the function Form1_Load it becomes local to that function only. 当您在函数Form1_Load中声明数组Drink时,它仅成为该函数的局部变量。 No one else can see it. 没有人可以看到它。 You need to change the scope of your variable to become global (it doesn't need to be public). 您需要将变量的范围更改为全局变量(不必是公共变量)。

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

You can, however, instantiate it elsewhere. 但是,您可以在其他地方实例化它。

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 = "";
    }
}

You need to properly scope your objects! 您需要适当地调整对象范围 By declaring it in Form1_Load , it doesn't exist when other methods are called. 通过在Form1_Load声明它,当调用其他方法时它不存在。 You have to put it at the scope level of the class (thereby making it a field of the class. This way it is visible to all methods called by Form1 . Scope is indicated by curly braces : { }. Consider the following: 您必须将其放在类的作用域级别(从而使其成为类的字段 。这样,它对于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

A variable or storage location of structure type is essentially a bunch of variables stuck together with duct tape. 结构类型的变量或存储位置本质上是一堆用胶带粘在一起的变量。 If one declares struct pair{public int X,Y}; 如果声明了struct pair{public int X,Y}; then one is specifying that variable declaration pair myPair; 然后指定变量声明pair myPair; will effectively declare two int variables-- myPair.X and myPair.Y --though one will be able to use them as a pair when convenient. 将有效地声明两个int变量myPair.XmyPair.Y尽管在方便时一个变量可以pair使用。 If one declares a pair[] myArray , then each item myArray[index] of the array will hold two integers, myArray[index].X and myArray[index].Y . 如果声明了pair[] myArray ,则数组的每个项目myArray[index]将保存两个整数,即myArray[index].XmyArray[index].Y

Structures whose purpose is to behave as a bunch of variables stuck together with duct tape should simply declare those variables as public fields (exposed-field structures are the best kind of data type to represent that behavior). 目的是充当一堆用胶带粘在一起的变量的结构,应简单地将这些变量声明为公共字段(暴露字段结构是表示该行为的最佳数据类型)。 Structures which are intended for other purposes should often follow the MS guidelines. 用于其他目的的结构应经常遵循MS准则。 I'm not really clear how you're intending to use your data type, so I can't say whether a struct is appropriate, though trying to initialize numberOfDrinks within a struct definition won't work. 我不太清楚您打算如何使用数据类型,因此尽管尝试在结构定义中初始化numberOfDrinks无效,但我不能说结构是否合适。 Class object instances and their fields only come into existence when a class is asked to create an instance of itself--a process over which the class has control. 类对象实例及其字段仅在要求类创建其自身实例时才存在-该过程由类控制。 By contrast, a struct definition directs that a storage location of the structure type should be regarded as a bunch of variables stuck together with duct tape, but it is the owner of that storage location, rather than the struct type, which has control over its creation. 相反,结构定义指示应将结构类型的存储位置视为一堆与胶带绑在一起的变量,但它是该存储位置的所有者,而不是对其类型具有控制权的结构类型创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM