简体   繁体   中英

assigning classes to an array of structs c#

I'm having an issue using an array declared in the "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".

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. There is 5 different pictures they can click to do so, which is where the array comes in need.

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. 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. 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. 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:

{
    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}; then one is specifying that variable declaration 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. 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 .

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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