繁体   English   中英

c# scope 问题

[英]c# scope question

对不起这个愚蠢的问题。 我刚开始 C# 编程。 我已经研究了好几个小时了,并在这里阅读了我能读到的内容。 这似乎很常见,我只是不明白为什么它不起作用。 我想在父方法中声明一个变量并更改子 class 中的值。 如果这不是这样做的方法,我该如何制作返回有用数据的方法(即使是 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;

您需要返回值。 例如,在GetMessageRow中的while循环之后,您应该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领域的方法之间更改值(即值本身) - 但我建议不要这样做......但是。

返回有用的值,只需return它。 特别是循环的一个问题是它们甚至可能不会迭代一次,因此就“明确分配”而言,您通常需要在循环之外分配一个默认值。

如果您没有进入循环,另一种选择是throw ,或者使用 LINQ 方法,如.First()或 .Single .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;
}

如果要为每一行调用存储过程,则需要在循环内调用RunStoredProcedure ,并传递当前 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 中。

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM