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 GuyCash { public partial class Form1 : Form { Guy joe; Guy bob; int bank = 100; public Form1() { InitializeComponent(); joe = new Guy(); joe.Name = "Joe"; joe.Cash = 50; } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (bank >= 10) { bank -= joe.ReceiveCash(10); UpdateForm(); } else { MessageBox.Show("В банке нет денег"); } } private void button2_Click(object sender, EventArgs e) { bank += bob.GiveCash(5); UpdateForm(); } } public void UpdateForm () { joesCashLabel.Text = joe.Name + "имеет $" + joe.Cash; bobsCashLabel.Text = bob.Name + "имеет $" + bob.Cash; bankCashLabel.Text = "В банке сейчас $" + bank; } } 

Class Guy works for me separately, without jambs. I decided to bind it to the Button and Label interface.

The UpdateForm method is responsible for Label, which persistently complains about void and does not want to work. The error comes out in the form: Expected class, delegate, enum, interface, or struct. I checked that there were no errors (but unfortunately I do not know how to use the debtor in full measure). Program started a few days ago. Help me please. Thanks for attention.

  • one
    Move UpdateForm inside the class, you have this method fell outside the curly brackets of the class. - Athari
  • five
    I vote for the closure of this issue as a matter off topic, because it is caused by a banal typo and will not be useful to other visitors. - Athari
  • @Athari, on the other hand, if you rename the subject to C #: Expected class, delegate, enum, interface, or struct, then another newcomer can save time - SanŚ́́́́́́́́́́́́́́́́́́́́́́́́́́́
  • @SanSYS There are 100,500 reasons for this error. You can shove anything where a type declaration is expected. The fact that in this particular case the method turned out to be outside - the rest, by no means, will help. - Athari
  • 2
    @VladD, presumably, Integrated Development Environment :) - Memoizer

1 answer 1

Understand the error. It was necessary to rearrange the UpdateForm method above.

  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 GuyCash { public partial class Form1 : Form { Guy joe; Guy bob; int bank = 100; public Form1() { InitializeComponent(); joe = new Guy(); joe.Name = "Joe"; joe.Cash = 50; } public void UpdateForm() { joesCashLabel.Text = joe.Name + "имеет $" + joe.Cash; bobsCashLabel.Text = bob.Name + "имеет $" + bob.Cash; bankCashLabel.Text = "В банке сейчас $" + bank; } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (bank >= 10) { bank -= joe.ReceiveCash(10); UpdateForm(); } else { MessageBox.Show("В банке нет денег"); } } private void button2_Click(object sender, EventArgs e) { bank += bob.GiveCash(5); UpdateForm(); } } } 
  • one
    Eh, just wanted to answer :) - SanŚ́́́́́́́́́́́
  • Thank you very much! :) - Klugge
  • @Klugge I would suggest an edit: you had to move the UpdateForm method not higher, but inside the form class. And then, after reading the answer, you might think that C # is tied to the order of declaring members inside the class ... - Sergey Teplyakov