c# - Is a 'method' which is not valid in the given context error -
this example form "head first csharp - page 113" i'm getting following error
error 1 'guys.form1.joescashlabel(object, system.eventargs)' 'method', not valid in given context c:\temp\guys\guys\form1.cs 20 12 guys
and same other 2 labels
this 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 bank"); } } private void button2_click(object sender, eventargs e) { bank = bank + bob.givemoney(5); updateform(); } } }
you can't define 2 types in same project same name, here have 3 controls , 3 events same name. remove below methods 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 want add events make sure following naming standard controlname_eventname
Comments
Post a Comment