使用未分配的变量? [英] Use of unassigned variable?
问题描述
在声明paymentstatus是否为空或在"if"语句中具有值时,我得到未分配变量"ps"的错误使用.我以为我已经声明了ps,但显然我做错了什么.为什么编译器会对此抱怨?
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 but obviously im doing something wrong. Why does the compiler complain about this?
这是上下文中的错误:
public IList<BestsellersReportLine> DailyBestsellersReport()
{
OrderStatus os;
PaymentStatus? ps;
ShippingStatus ss;
int billingCountryId = 0;
int recordsToReturn = 999;
int orderBy = 1;
int groupBy = 1;
int? paymentStatusId = null;
if (ps.HasValue)
paymentStatusId = (int)ps.Value;
// Specifies the time range for sold products/day
var range = new
{
startTimeUtc = DateTime.Today.AddDays(-1),
endTimeUtc = DateTime.Today.AddSeconds(-1),
CreatedOnUtc = DateTime.Today.AddDays(-1),
};
var query1 = from opv in _opvRepository.Table
join o in _orderRepository.Table on opv.OrderId equals o.Id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
where (o.CreatedOnUtc >= range.startTimeUtc && o.CreatedOnUtc <= range.endTimeUtc) &&
(!paymentStatusId.HasValue || paymentStatusId == o.PaymentStatusId)
select opv;
}
谢谢!
推荐答案
您已经声明了局部变量,但尚未分配值.因此,编译器可以帮助您防止该错误.
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)
因此分配一个值:
PaymentStatus? ps = null;
// ...
if (ps.HasValue)
但是,thix修复了编译器错误,但仍然毫无意义,因为它永远不会有值.也许您想改用方法参数:
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<BestsellersReportLine> DailyBestsellersReport(PaymentStatus? ps)
{
这篇关于使用未分配的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!