从方法返回变量

时间:2015-06-19 18:25:08

标签: c# variables return-value

我知道如何将变量传递给过程,但是如何将变量返回给调用过程?例如,这是我的语法,在变量arr中完全没有问题但是如何将值nummmm返回到Main?请忽略空的sqlconnection&更新语句等,它们在我的实际语法中有效。

namespace ConsoleTest
{
  class Test
  {
    public static string SQLConnectionString = "";
    public static string updatestatement = null;
    public static string[] arrArray;
    public int 

    static void Main(string[] args)
    {
        arrArray = new string[3] { "1412", "1618", "1223" };
        foreach (string arr in arrArray)
        {
          runupdates(arr, out nummmm);
        }
    }

    private static int runupdates(string arr, out int nummmm)
    {
        updatestatement = "";
        using (SqlConnection connection = new SqlConnection(SQLConnectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(updatestatement, connection))
            {
            command.CommandText = updatestatement;
            int nummmm = command.ExecuteNonQuery();
            connection.Close();
            }
        }
    }
  }
}

我编辑的代码现在给出了编译错误'Not Not Code Paths返回一个值。

2 个答案:

答案 0 :(得分:1)

只需使用函数本身即可返回值。您不需要额外的输出参数。

private static int runupdates(string arr)
{
    updatestatement = "";
    using (SqlConnection connection = new SqlConnection(SQLConnectionString))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand(updatestatement, connection))
        {
        command.CommandText = updatestatement;
        int nummmm = command.ExecuteNonQuery();
        connection.Close();
        }
    }
    return nummmm;
}

答案 1 :(得分:-2)

如果要从方法返回值,则必须指明它。 “void”类型意味着您的方法不会返回值。在您的情况下,如果要返回int,请为“int”更改类型“void”并添加返回句子

相关问题