3

I'm new to c# but quite familiar with c, this error however doesn't make much sense to me, as I have specifically followed the required syntax shown on http://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
    int years = 1;
    while (years <=10) {

    public Form1()
    {
        InitializeComponent();
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        int value = Convert.ToInt32(bushels1.Text);
        numericUpDown1.Maximum = value;
    }

    private void bushels1_Click(object sender, EventArgs e)
    {

    }

    private void nextYear_Click(object sender, EventArgs e)
    {

    }
    }
   }
}

3 Answers3

5

You have while directly inside the class, it should be part of a method, You can't write statements like while directly inside the class.

public partial class Form1 : Form
{
    int years = 1;
    while (years <=10) {
   //^^

It should be part of method like:

public partial class Form1 : Form
{
    int years = 1;
    public void SomeMethod()
    {
       while (years <=10) {
      //rest of your code
    }
Habib
  • 219,104
  • 29
  • 407
  • 436
1

your while loop must be in a method, you have it in the variable/method declaration area. Remove it and place inside a method.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
1

You have a "while (years <=10) {" line that is not inside a function, near the top. That definitely is not going to work. What are you expecting it to do?

You say you're familiar with c, but think about it - would you expect a while {} construct to work at the class level in c, either? (It would not. Loops only work inside functions, in most languages, including C#.)

neminem
  • 2,658
  • 5
  • 27
  • 36