C# 是在给定的上下文错误中无效的“方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16548097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:18:53  来源:igfitidea点击:

Is a 'method' which is not valid in the given context error

c#

提问by Rene

This is an example form the "Head First CSharp - page 113" I'm getting the following error

这是“Head First CSharp - page 113”的示例,我收到以下错误

Error 1 'Guys.Form1.joesCashLabel(object, System.EventArgs)' is a 'method', which is not valid in the given context c:\temp\Guys\Guys\Form1.cs 20 12 Guys

错误 1 ​​'Guys.Form1.joesCashLabel(object, System.EventArgs)' 是一个“方法”,在给定的上下文中无效 c:\temp\Guys\Guys\Form1.cs 20 12 Guys

And the same with the other two labels

与其他两个标签相同

This is the code:

这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Guys
{
    public partial class Form1 : Form
    {
        Guy Joe;
        Guy Bob;
        int Bank = 100;

        public void UpdateForm()
        {
           joesCashLabel.Text = Joe.Name + "$" + Joe.Money;
            bobsCashLabel.Text = Bob.Name + "$" + Bob.Money;
            bankCashLabel.Text = "Bank has" + Bank;
        }


        public Form1()
        {
            InitializeComponent();

            Guy Bob = new Guy();
            Bob.Name = "Bob";
            Bob.Money =100;

            Guy Joe = new Guy();
            Joe.Name = "Joe";
            Joe.Money =50;

            UpdateForm();

        }

        private void joesCashLabel(object sender, EventArgs e)
        {

        }

        private void bobsCashLabel(object sender, EventArgs e)
        {

        }

        private void bankCashLabel(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Bank >= 10) 
            {
                Bank -= Joe.ReceiveMoney(10);
                UpdateForm();
            }
            else
            {
                MessageBox.Show("No money in the bank");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bank = Bank + Bob.GiveMoney(5);
            UpdateForm();
        }
    }
}

采纳答案by Damith

You can't define two types in same project with same name, here you have three controls and three events with same name. so remove the below methods to compile without errors.

你不能在同一个项目中定义两个同名的类型,这里你有三个同名的控件和三个事件。所以删除下面的方法来编译没有错误。

private void joesCashLabel(object sender, EventArgs e){}

private void bobsCashLabel(object sender, EventArgs e){}

private void bankCashLabel(object sender, EventArgs e){}

If you want to add events make sure you are following naming standard like ControlName_EventName

如果要添加事件,请确保遵循命名标准,例如 ControlName_EventName

回答by Sam I am says Reinstate Monica

it's a method(an event at that),

这是一个方法(一个事件),

private void joesCashLabel(object sender, EventArgs e)
{

}

but you're using it as a variable

但你把它当作一个变量

joesCashLabel.Text = Joe.Name + "$" + Joe.Money;

My guess, is that there's some sort of label that that event is supposed to be associated with.

我的猜测是,该事件应该与某种标签相关联。