30

While sending back parameters getting this error

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

Code is

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

calling it as

GetPapers(web, out Id1, out Id2);

Related to this question

Related question

Community
  • 1
  • 1
fdgfdgs dfg
  • 669
  • 4
  • 12
  • 22
  • 1
    Out parameters must be assigned along all paths before control leaves the method. Since you have an if statement, they may not get assigned. – Tim M. Jul 31 '12 at 10:28
  • 3
    What do you want the values to be if `rdr.Read()` returns false? – Jon Skeet Jul 31 '12 at 10:28
  • @JonSkeet code only works when there is something in DB, its like a event receiver – fdgfdgs dfg Jul 31 '12 at 10:29
  • 3
    @fdgfdgsdfg: So perhaps you ought to be throwing an exception when there isn't anything in the database? – Jon Skeet Jul 31 '12 at 11:02
  • @JonSkeet yup, I will do the validations later on then :) – fdgfdgs dfg Jul 31 '12 at 11:05
  • @JonSkeet What is the wisdom behind this requirement? if I initialize Count=0 and then call a function foo(..., out Count); that only should increment Count if it found matches, why should the function update Count otherwise? Thank you. – Meryan Mar 30 '23 at 01:24
  • @Meryan: Because that's what `out` means... if it's intended to *increment* `count` then it should use `ref` instead of `out`. – Jon Skeet Mar 30 '23 at 05:41
  • @JonSkeet thank you! I was not aware ref existed :), viva Pascal – Meryan Mar 30 '23 at 09:05

2 Answers2

38

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.

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.

See: 5.3 Definite assignment

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.

Habib
  • 219,104
  • 29
  • 407
  • 436
3

You need to initialise those variables ;

it must hold some value before returned from the Getpapers() method

Dhanasekar Murugesan
  • 3,141
  • 1
  • 18
  • 21