一个对象引用是所必需的非静态字段,方法或属性“QuickSharp.CokeMachine.TotalInsertedCoins' [英] An object reference is required for the non-static field, method or property 'QuickSharp.CokeMachine.TotalInsertedCoins'

查看:121
本文介绍了一个对象引用是所必需的非静态字段,方法或属性“QuickSharp.CokeMachine.TotalInsertedCoins'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译时,我得到了以下错误:

I get the following error when compiling:

cs0120:一个对象引用所需的非静态字段,方法或属性QuickSharp.CokeMachine.TotalInsertedCoins

cs0120: An object reference is required for the non-static field, method or property 'QuickSharp.CokeMachine.TotalInsertedCoins'

这是因为我尝试使用TotalInsertedCoins变量从另一个类(CokeForm)。我该如何解决这个问题?如果我想要添加在我CokeForm方法的按钮,我得到了同样的问题。

This is because I'm trying to use the TotalInsertedCoins variable from another class (CokeForm). How can I fix this? If I want to add the buttons in my CokeForm method, I'm getting the same issue.

我的代码:

using System;
using System.Windows.Forms;
using System.Drawing;

namespace QuickSharp
{
public class CokeMachine
{
    Button Coke;
    Button Sprite;
    Button Fanta;
    Button RedBull;
    Button Juice;
    Button Water;

    double CokeCost = 1.00;
    double SpriteCost = 1.00;
    double FantaCost = 1.00;
    double RedBullCost = 2.50;
    double JuiceCost = 2.00;
    double WaterCost = 0.50;

    Button Coin1;
    Button Coin2;
    Button Coin3;
    Button Coin4;
    Button Coin5;
    Button Coin6;

    double CoinAmount1 = 0.05;
    double CoinAmount2 = 0.10;
    double CoinAmount3 = 0.20;
    double CoinAmount4 = 0.50;
    double CoinAmount5 = 1.00;
    double CoinAmount6 = 2.00;

    public double TotalInsertedCoins = 0;

    //EventHandlers for drink buttons

    public void DrinkButtonsEvents()
    {
        Coke.Click += new EventHandler(Coke_ClickHandler);
        Sprite.Click += new EventHandler(Sprite_ClickHandler);
        Fanta.Click += new EventHandler(Fanta_ClickHandler);
        RedBull.Click += new EventHandler(RedBull_ClickHandler);
        Juice.Click += new EventHandler(Juice_ClickHandler);
        Water.Click += new EventHandler(Water_ClickHandler);
    }

    //Drink buttons - Click event handlers

    public void Coke_ClickHandler(object sender, EventArgs e)
    {
        if(TotalInsertedCoins >= CokeCost)
        {
            DispenseDrink("Coca-Cola" , CokeCost);
        }
        else
        {
            MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning");
        }
    }

    public void Sprite_ClickHandler(object sender, EventArgs e)
    {
        if(TotalInsertedCoins >= SpriteCost)
        {
            DispenseDrink("Sprite" , SpriteCost);
        }
        else
        {
            MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning");
        }
    }

    public void Fanta_ClickHandler(object sender, EventArgs e)
    {
        if(TotalInsertedCoins >= FantaCost)
        {
            DispenseDrink("Fanta" , FantaCost);
        }
        else
        {
            MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning");
        }
    }

    public void RedBull_ClickHandler(object sender, EventArgs e)
    {
        if(TotalInsertedCoins >= RedBullCost)
        {
            DispenseDrink("Red Bull" , RedBullCost);
        }
        else
        {
            MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning");
        }
    }

    public void Juice_ClickHandler(object sender, EventArgs e)
    {
        if(TotalInsertedCoins >= JuiceCost)
        {
            DispenseDrink("Orange Juice" , JuiceCost);
        }
        else
        {
            MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning");
        }
    }

    public void Water_ClickHandler(object sender, EventArgs e)
    {
        if(TotalInsertedCoins >= WaterCost)
        {
            DispenseDrink("Water" , WaterCost);
        }
        else
        {
            MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning");
        }
    }

    //EventHandlers for money buttons

    public void MoneyButtonEvents()
    {
        Coin1.Click += new EventHandler(Coin1_ClickHandler);
        Coin2.Click += new EventHandler(Coin2_ClickHandler);
        Coin3.Click += new EventHandler(Coin3_ClickHandler);
        Coin4.Click += new EventHandler(Coin4_ClickHandler);
        Coin5.Click += new EventHandler(Coin5_ClickHandler);
        Coin6.Click += new EventHandler(Coin6_ClickHandler);
    }

    //Money buttons - Click event handlers

    public void Coin1_ClickHandler(object sender, EventArgs e)
    {
        TotalInsertedCoins += CoinAmount1;
    }

    public void Coin2_ClickHandler(object sender, EventArgs e)
    {
        TotalInsertedCoins += CoinAmount2;
    }

    public void Coin3_ClickHandler(object sender, EventArgs e)
    {
        TotalInsertedCoins += CoinAmount3;
    }

    public void Coin4_ClickHandler(object sender, EventArgs e)
    {
        TotalInsertedCoins += CoinAmount4;
    }

    public void Coin5_ClickHandler(object sender, EventArgs e)
    {
        TotalInsertedCoins += CoinAmount5;
    }

    public void Coin6_ClickHandler(object sender, EventArgs e)
    {
        TotalInsertedCoins += CoinAmount6;
    }

    private void DispenseDrink(string drink , double cost)
    {
        if(TotalInsertedCoins - cost == 0.0)
        {
            MessageBox.Show("Enjoy your " + drink + "!");
            TotalInsertedCoins = 0.0;
        }
        else
        {
            MessageBox.Show("Enjoy your " + drink + "! Here is your change: €" + (TotalInsertedCoins - cost));
            TotalInsertedCoins = 0.0;
        }
    }
}

public class CokeForm : Form
{
   public CokeForm()
   {
        // General aspect of machine

        this.Text = "Cola Machine";
        this.Size = new Size(450 , 500);

        Label Header;
        Header = new Label();
        Header.Text = "Coca-Cola Machine";
        Header.Font = new Font("Arial" , Header.Font.Size +5);
        Header.ForeColor = Color.DarkRed;
        Header.Location = new Point(132, 20);
        Header.AutoSize = true;
        this.Controls.Add(Header);

        TextBox TextBox1 ;
        TextBox1 = new TextBox();
        TextBox1.BackColor = Color.Black;
        TextBox1.ForeColor = Color.Red;
        TextBox1.Font = new Font("Arial" , TextBox1.Font.Size +3);
        TextBox1.ReadOnly = true;
        TextBox1.Size = new Size(210,300);
        TextBox1.Location = new Point(112,50);

        //TextBox1.SelectionStart = TextBox1.Text.Length;
        //TextBox1.ScrollToCaret();
        //TextBox1.Refresh();


        if(CokeMachine.TotalInsertedCoins == 0.00)
        {
            TextBox1.Text = "Buy Your Ice Cold Drinks Here!";
        }
        else
        {
            TextBox1.Text = "Inserted Coins: €" + CokeMachine.TotalInsertedCoins;
        }

        this.Controls.Add(TextBox1);

        // Money aspect of machine

        Label Money;
        Money = new Label();
        Money.Text = "Insert Coins Here:";
        Money.Location = new Point(20, 100);
        this.Controls.Add(Money);

        //Money buttons will be here


        // Drink aspect of machine

        Label Drinks;
        Drinks = new Label();
        Drinks.Text = "Choose Your Drink:";
        Drinks.Location = new Point(315 , 100);
        Drinks.AutoSize = true;
        this.Controls.Add(Drinks);

        //Drink buttons will be here
   }
}

public class Test
{
    public static void Main()
    {                      
        CokeForm ColaForm;
        ColaForm = new CokeForm();
        Application.Run(ColaForm);
    }
}
}



谁能帮我这个?如果您对本CS0120错误的其他问题没有给我带来任何进一步的

Can anyone help me with this? Other questions about the CS0120 error don't bring me any further.

感谢

PS:抱歉丑陋的布局后,我仍然在试图找出所有的东西:)

PS: Sorry about the ugly post layout, I'm still trying to figure everything out :)

推荐答案

您从未创建CokeMachine的一个实例:

You never create an instance of CokeMachine:

if(CokeMachine.TotalInsertedCoins == 0.00)
{
  TextBox1.Text = "Buy Your Ice Cold Drinks Here!";
}
else
{
  TextBox1.Text = "Inserted Coins: €" + CokeMachine.TotalInsertedCoins;
}

在表单中创建CokeMachine的一个实例,然后使用。

Create an instance of CokeMachine in the form, and then use that.

...
CokeMachine machine = new CokeMachine();
...

if (machine.TotalInstertedCoins == 0.00)
{
 ....
}

这篇关于一个对象引用是所必需的非静态字段,方法或属性“QuickSharp.CokeMachine.TotalInsertedCoins'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆