Use of unassigned variable?

前端 未结 5 1001
Happy的楠姐
Happy的楠姐 2021-01-26 19:04

I\'m getting the error use of unassigned variable \"ps\" when declaring if paymentstatus is null or has value in the \"if\" statement. I\'m thinking that i allready declared ps

5条回答
  •  [愿得一人]
    2021-01-26 19:42

    You have declared the local variable but you have not assigned a value. Therefore the compiler helps you to prevent this bug.

    PaymentStatus? ps;  
    // ...
    if (ps.HasValue)
    

    So assign a value:

    PaymentStatus? ps = null;  
    // ...
    if (ps.HasValue)
    

    However, thix fixes the compiler error but is still pointless since it will never have a value. Maybe you want to use a method parameter instead:

    public IList DailyBestsellersReport(PaymentStatus? ps)
    {
    

提交回复
热议问题