3
votes

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

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

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();
        }
    }
}
2
As you can see by looking at your code, joesCashLabel is in fact a method. And you are using it as though it were a textbox. - Kirk Woll
In your project explorer, there might be a file called Form1.Designer.cs. Although you should never ever change the code in the designer file, we might be able to glean some information from reading it. can you post the code from that file? - Sam I am says Reinstate Monica

2 Answers

3
votes

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.

3
votes

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