c#范围问题

c#范围问题,c#,scope,C#,Scope,对不起,这个愚蠢的问题。我刚开始C#编程。我已经研究了好几个小时了,在这里我能读到什么。这似乎很普遍,我只是不明白为什么它不起作用。我想在父方法中声明一个变量,并更改子类中的值。如果不是这样做的话,我该如何创建返回有用数据的方法(即使是循环或while循环) 例1: static void Main(string[] args) { int rowID; for (int i = 1; i < 500; i++ ) {

对不起,这个愚蠢的问题。我刚开始C#编程。我已经研究了好几个小时了,在这里我能读到什么。这似乎很普遍,我只是不明白为什么它不起作用。我想在父方法中声明一个变量,并更改子类中的值。如果不是这样做的话,我该如何创建返回有用数据的方法(即使是循环或while循环)

例1:

   static void Main(string[] args)
    {
        int rowID;
        for (int i = 1; i < 500; i++ )
        {
            rowID = i;
        }
        Console.WriteLine(rowID);
    }

使用
return
关键字从方法返回值。此关键字退出该方法。例如:

 return rowID;

使用
return
关键字从方法返回值。此关键字退出该方法。例如:

 return rowID;

您需要返回值。例如,在
GetMessageRow
中执行
while
循环之后,您应该
返回rowID

您需要返回值。例如,在
GetMessageRow
中执行
while
循环之后,您应该
返回rowID

您的GetMessageRow方法需要返回rowID。这就是允许rowID(另一个变量)在SendButtonClicked方法中设置其值的原因

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 rowID;
}

GetMessageRow方法需要返回rowID。这就是允许rowID(另一个变量)在SendButtonClicked方法中设置其值的原因

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 rowID;
}
要在方法之间更改值(即值本身),您需要进入
ref
区域-但我建议不要这样做。。。然而

要返回有用的值,只需返回它即可。特别是循环的一个问题是,它们甚至可能不会迭代一次,因此在“确定赋值”方面,您通常需要在循环之外指定一个默认值

另一种选择是,如果没有进入循环,则抛出
或使用LINQ方法,如
.First()
.Single()
,在开始进入
ref
区域的方法之间更改值(即值本身)——但我建议不要这样做。。。然而

要返回有用的值,只需返回它即可。特别是循环的一个问题是,它们甚至可能不会迭代一次,因此在“确定赋值”方面,您通常需要在循环之外指定一个默认值


另一个选项是,如果不进入循环,则抛出
或使用LINQ方法,如
.First()
.Single()
假设要为
GetMessageRow
方法返回的单个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 rowID;
}
如果要为每一行调用存储过程,则需要在循环内调用
runstoredprocesdure
,传递当前rowId:

public void ProcessMessageRows()
// finds the first row that has not been sent
{
      // skipping some code
       while (drGetMessageRow.Read())
        {
         // while loop does not understand the variable and errors  
            int rowID = drGetMessageRow.GetInt32(0);
            MessageBox.Show("1: RowID is " + rowID.ToString());
            RunStoredProcedure(rowId);
        }
}

假设要为
GetMessageRow
方法返回的单个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 rowID;
}
如果要为每一行调用存储过程,则需要在循环内调用
runstoredprocesdure
,传递当前rowId:

public void ProcessMessageRows()
// finds the first row that has not been sent
{
      // skipping some code
       while (drGetMessageRow.Read())
        {
         // while loop does not understand the variable and errors  
            int rowID = drGetMessageRow.GetInt32(0);
            MessageBox.Show("1: RowID is " + rowID.ToString());
            RunStoredProcedure(rowId);
        }
}

我想我可以把所有的东西都放到这样的课堂上

class Sproc 
{ 
public static int rowID = 0; 
public int GetMessageRow(); ... 
public void RunStoredProcedure(int rowID)... 
}
。。。。然后我可以打电话如下

    SProc s;
    s = new SProc();

    // finds the first row that has not been sent
    int rowID = s.GetMessageRow();

    //then process the row and figure out what stored procedure to run - this could be (should be) also in the config.
    s.RunStoredProcedure(rowID);

我想我可以把所有的东西都放到这样的课堂上

class Sproc 
{ 
public static int rowID = 0; 
public int GetMessageRow(); ... 
public void RunStoredProcedure(int rowID)... 
}
。。。。然后我可以打电话如下

    SProc s;
    s = new SProc();

    // finds the first row that has not been sent
    int rowID = s.GetMessageRow();

    //then process the row and figure out what stored procedure to run - this could be (should be) also in the config.
    s.RunStoredProcedure(rowID);

C#使用词汇范围界定与动态范围界定相反。简而言之,这意味着变量范围与代码的结构直接相关。但是,“while循环不理解变量”是不正确的语句。再次阅读错误消息——它到底说了什么?变量drGetMessageRow来自哪里?是班级级别吗?嗯,我再加上这个。例如,它是一个try块。显然,try块中的变量是有作用域的。代码如下所示:int rowID;试试{/*…*/SqlDataReader drGetMessageRow=cmdGetmEssageRow.ExecuteReader;while(drGetMessageRow.Read()){rowID=drGetMessageRow.GetInt32(0);}}}我想我可以把所有东西都放在这样的类中。类存储过程{public static int rowID=0;public int GetMessageRow();…public void RunStoredProcedure(int rowID).}C#使用词法作用域与动态作用域相反。简而言之,这意味着变量范围与代码的结构直接相关。但是,“while循环不理解变量”是不正确的语句。再次阅读错误消息——它到底说了什么?变量drGetMessageRow来自哪里?是班级级别吗?嗯,我再加上这个。例如,它是一个try块。显然,try块中的变量是有作用域的。代码如下所示:int rowID;试试{/*…*/SqlDataReader drGetMessageRow=cmdGetmEssageRow.ExecuteReader;while(drGetMessageRow.Read()){rowID=drGetMessageRow.GetInt32(0);}}}我想我可以把所有东西都放在这样的类中。类存储过程{public static int rowID=0;public int GetMessageRow();…public void RunStoredProcedure(int rowID)…}