错误:返回void,返回关键字后面不能跟对象表达式

时间:2012-10-11 15:44:59

标签: c# return

    private void QuestionAnswer_Load(object sender, EventArgs e)
    {

        txtQuestion.Enabled = false;
        txtQuestion.BackColor = Color.White;
        grpMultipleChoice.Enabled = false;
        grpSingleChoice.Enabled = false;

        btnCheckAnswer.Enabled = false;
        btnNext.Enabled = false;
        btnQuit.Enabled = false;

        //force student to enter registration details
        if (txtStudentName.Text == "" && txtStudentNumber.Text == "" && txtModuleNumber.Text == "")
        {
            btnStart.Enabled = false;
            MessageBox.Show("You must enter your registration details at the upper right corner");
        }

        //declare a list
        lstNumbers = new ArrayList();

        //create a random number generator
        Random rndNumber = new Random();

        //generate 70 random numbers
        //int number = (int)(rndNumber.NextDouble() * 69) + 1;

        //lstNumbers.Add(number);
        ///use this counter to loop whenever a number is generated
        int count = 0;
        int maximumNumber = 69;

        ///disable answer button 
        btnCheckAnswer.Enabled = false;
        do
        {

            int number = (int)(rndNumber.NextDouble() * maximumNumber) + 1;
            lstNumbers.Add(number);
            if (!lstNumbers.Contains(number))
            {
                lstNumbers.Add(number);
            }
            count++;

        } while (count <= 15 * 70);//
        btnCheckAnswer_Click(sender, e);
        return lstNumbers;//returns once list is built
     }

说明: 我创建了一个arraylist,允许我在1-70之间随机生成但注意到了 我的数字有点重复。为了防止这种情况,我注意到我得到了一个 错误信息如下:

错误1由于'wwTestAppV1.GenKnow.QuestionAnswer_Load(object,System.EventArgs)'返回void,因此返回关键字后面不能包含对象表达式

我已尽力解决这个问题。当我删除它时程序运行良好 声明但显示重复值。你能帮忙吗?

1 个答案:

答案 0 :(得分:8)

您的方法签名是:

private void QuestionAnswer_Load(object sender, EventArgs e)

返回类型void表示您不能/不打算返回任何内容。但是,在底部,您有:

return lstNumbers;//returns once list is built

你回来了!编译器抱怨你告诉它有冲突的指令。将返回类型更改为ArrayList或不返回任何内容。

话虽如此,可以对代码进行一些改进,使其更具可读性,这也有助于解决问题。请考虑将您的代码提交到https://codereview.stackexchange.com/