C# 错误:返回 void,返回关键字后不能跟对象表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12843501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: returns void, a return keyword must not be followed by an object expression
提问by Zayn
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
}
Explanation: I have created an arraylist that allows me generate randomly between 1-70 but noticed my numbers where somewhat repeating. In a bid to prevent this, i noticed i was getting an error message below:
说明:我创建了一个数组列表,它允许我在 1-70 之间随机生成,但注意到我的数字有些重复。为了防止这种情况,我注意到我收到以下错误消息:
Error 1 Since 'wwTestAppV1.GenKnow.QuestionAnswer_Load(object, System.EventArgs)' returns void, a return keyword must not be followed by an object expression
错误 1 由于 'wwTestAppV1.GenKnow.QuestionAnswer_Load(object, System.EventArgs)'返回 void,返回关键字后不能跟对象表达式
i have tried everything i can to resolve this.the program runs well when i remove this statement but shows duplicate values. Could you please help?
我已尽我所能解决这个问题。当我删除此语句但显示重复值时,程序运行良好。能否请你帮忙?
回答by Kache
Your method signature is:
您的方法签名是:
private void QuestionAnswer_Load(object sender, EventArgs e)
The return type voidmeans you can't/aren't planning to return anything. However, at the bottom, you have:
返回类型void意味着您不能/不打算返回任何东西。但是,在底部,您有:
return lstNumbers;//returns once list is built
You're returning something! The compiler is complaining that you told it conflicting instructions. Either change the return type to ArrayListor do not return anything.
你正在返回一些东西!编译器抱怨你告诉它冲突的指令。将返回类型更改为ArrayList或不返回任何内容。
That being said, there are several improvements that could be made to the code to make it more readable, which will help you decipher problems, too. Consider submitting your code to https://codereview.stackexchange.com/.
话虽如此,可以对代码进行一些改进以使其更具可读性,这也将帮助您破译问题。考虑将您的代码提交到https://codereview.stackexchange.com/。

