c#类,结构或接口成员声明中的'while'无效标记(while循环不起作用)

Szy*_*zki 3 c# loops while-loop

我是c#的新手但对c非常熟悉,但这个错误对我来说没有多大意义,因为我已经特别遵循了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)
    {

    }
    }
   }
}
Run Code Online (Sandbox Code Playgroud)

Hab*_*bib 5

while直接在类里面,它应该是一个方法的一部分,你不能while直接在类中编写语句.

public partial class Form1 : Form
{
    int years = 1;
    while (years <=10) {
   //^^
Run Code Online (Sandbox Code Playgroud)

它应该是方法的一部分,如:

public partial class Form1 : Form
{
    int years = 1;
    public void SomeMethod()
    {
       while (years <=10) {
      //rest of your code
    }
Run Code Online (Sandbox Code Playgroud)