I have a stupid yet braincracker issue about casting types. As you can see from the code I have a variable lprod_monthylReport that depending on the ytm value can be a List<Monthly_Report> or simply a Monthly_Report. I need the variable to have the same name in both cases.
var lprod_monthlyReport = new List<Monthly_Report>;
if (ytm == true)
{
lprod_monthlyReport = _ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.item_ID);
}
else
lprod_monthlyReport = _ProductRep.GetSpecificYTMReport(prod.Prod_ID, item.item_ID);
The problem is that if I declare the variable inside each if (or else) section the compilers gives error because it says that the variable is already declared in this context.
I already tried casting
lprod_monthlyReport = (Monthly_Report) _ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.Item_ID);
But it does not work. I also tried the as keyword without success.
May you please help me with this one? Thanks
Francesco
This should work:
in case you’ve got a
Monthly_Reportjust add it to the list…