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:
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
5 Points
18 Posts
use of unassigned local variable 'p'
Feb 21, 2021 11:19 AM|samaremad|LINK
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
All-Star
53001 Points
23593 Posts
Re: use of unassigned local variable 'p'
Feb 21, 2021 02:29 PM|mgebhard|LINK
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.
Please use the "Insert/edit code sample" toolbar option when sharing code in the forum.
Contributor
2690 Points
774 Posts
Re: use of unassigned local variable 'p'
Feb 22, 2021 02:21 AM|YihuiSun|LINK
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:
Best Regards,
YihuiSun