抱歉这个愚蠢的问题。我刚开始 C# 编程。我已经为此研究了好几个小时,并在这里阅读了我可以阅读的内容。这看起来很常见,我只是不明白为什么它不起作用。我想在父方法中声明一个变量并更改子类中的值。如果这不是实现它的方法,我如何制作返回有用数据的方法(甚至是 for 循环或 while 循环)?
示例 1:
static void Main(string[] args)
{
int rowID;
for (int i = 1; i < 500; i++ )
{
rowID = i;
}
Console.WriteLine(rowID);
}
示例 2:
private void SendButtonClicked(object sender, System.EventArgs e)
{
// finds the first row that has not been sent
int rowID = GetMessageRow();
//then process the row and figure out what stored procedure to run
RunStoredProcedure(rowID);
}
public int GetMessageRow()
// finds the first row that has not been sent
{
int rowID;
// skipping some code
while (drGetMessageRow.Read())
{
// while loop does not understand the variable and errors
rowID = drGetMessageRow.GetInt32(0);
MessageBox.Show("1: RowID is " + rowID.ToString());
}
}
最佳答案
使用return
关键字从方法中返回一个值。这个关键字退出方法。示例:
return rowID;
关于c#范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6636543/