locked
use of unassigned local variable 'p' RRS feed

  • Question

  • User-1566284277 posted

    hi every one 

    can you please check my code and correct the erorr i dont know what is the wrong in it 

     List<Nominee> p;
                        List <votenominee>  d = db1.votenominees.Where(b => b.idvoteR == id).ToList();
                        int o;
                        
                        for (int j = 0; j < d.Count(); j++)
                        {
                            o = d[j].idnomineeR;
                            p = db1.Nominees.Where(b => b.nominee_id == o).ToList();
                        }
                        
                        // var e = p.Count();
                        
                        Nominee v = p[0];
                       for(int i = 1; i < p.Count(); i++)
                        {
                            if (v.num_vote < p[i].num_vote)
                            {
                                v = p[i];
                            }
                        }

    Nominee v = p[0]; the error reffer to p here 

    Sunday, February 21, 2021 11:19 AM

Answers

  • User475983607 posted

    There is a potential that p is never assigned a value due to the conditional statements yet you are using p as if it always has a value.  

    List<Nominee> p = new List<Nominee>();

    Please use the "Insert/edit code sample" toolbar option when sharing code in the forum.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 21, 2021 2:29 PM
  • User1686398519 posted

    Hi samaremad, 

    If the compiler detects the use of a variable that might not have been initialized, it generates compiler error CS0165:Use of unassigned local variable 'name'.

    You need to initialize this variable p.

    List<Nominee> p=new List<Nominee>();

    The reason why the variable "d" does not have this error is that you assign an initial value to it when you create the variable "d", which is equivalent to already initialized.

    List <votenominee>  d = db1.votenominees.Where(b => b.idvoteR == id).ToList();

    Here are some links, you can click on them to learn more:

    1. Compiler Error CS0165
    2. Fields (C# Programming Guide)

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2021 2:21 AM

All replies

  • User475983607 posted

    There is a potential that p is never assigned a value due to the conditional statements yet you are using p as if it always has a value.  

    List<Nominee> p = new List<Nominee>();

    Please use the "Insert/edit code sample" toolbar option when sharing code in the forum.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 21, 2021 2:29 PM
  • User1686398519 posted

    Hi samaremad, 

    If the compiler detects the use of a variable that might not have been initialized, it generates compiler error CS0165:Use of unassigned local variable 'name'.

    You need to initialize this variable p.

    List<Nominee> p=new List<Nominee>();

    The reason why the variable "d" does not have this error is that you assign an initial value to it when you create the variable "d", which is equivalent to already initialized.

    List <votenominee>  d = db1.votenominees.Where(b => b.idvoteR == id).ToList();

    Here are some links, you can click on them to learn more:

    1. Compiler Error CS0165
    2. Fields (C# Programming Guide)

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2021 2:21 AM