错误:out参数必须控制之前分配离开当前方法 [英] Error : The Out Parameter must be assigned before control leaves the current method

查看:2702
本文介绍了错误:out参数必须控制之前分配离开当前方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然发回的参数收到此错误

While sending back parameters getting this error

错误:控制离开之前out参数必须被分配
  当前方法

Error : The Out Parameter must be assigned before control leaves the current method

code是

 public void GetPapers(string web, out int Id1, out int Id2)
    {
        SqlConnection conn = new SqlConnection(ConnectionString());
        conn.Open();
        SqlCommand cmd = new SqlCommand("GetPapers", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@URL", String(web)));

        SqlDataReader rdr = cmd.ExecuteReader();

        if (rdr.Read())
        {
            Id1 = (int)rdr["ID1"];
            Id2 = (int)rdr["ID2"];
        }

        rdr.Close();
    }

称其为

GetPapers(web, out Id1, out Id2);

与此相关的问题。

Related to this question

<一个href=\"http://stackoverflow.com/questions/11737497/sending-2-parameters-back-to-main-function/11737538#comment15577485_11737538\">Related问题

推荐答案

您正在分配为Id1 的Id2 内if语句和编译器无法确定它是否会被分配在运行时的值,从而错误。

You are assigning Id1 and Id2 inside an if statement and compiler can't determine if it will be assigned a value at run time, thus the error.

您可以在if语句之前给它们一些默认值。就像是。

You could assign them some default value before the if statement. Something like.

Id1 = 0;
Id2 = 0;

if (rdr.Read())
{
    Id1 = (int)rdr["ID1"];
    Id2 = (int)rdr["ID2"];
}

或指定一些默认值否则你的病情的一部分。

or specify some default values in else part of your condition.

这是退出类型参数必须分配一定的价值,控制叶片的功能之前。你的情况,编译器无法确定您的变量是将被分配与否,因为它正在一个如果语句中指定。

An out type parameter must be assigned some value, before the control leaves the functions. In your case, compiler can't determine whether your variables will be assigned or not, because it is being assigned inside an if statement.

请参阅:

在函数成员可执行code给定的位置,一
  变量被认为是明确赋值的如果编译器
  证明,静态流分析,
该变量已
  自动初始化或已经至少一个的目标
  分配。

At a given location in the executable code of a function member, a variable is said to be definitely assigned if the compiler can prove, by static flow analysis, that the variable has been automatically initialized or has been the target of at least one assignment.

这篇关于错误:out参数必须控制之前分配离开当前方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆