必须初始化隐式类型的局部变量 [英] Implicitly-typed local variable must be initialized
问题描述
您好我有以下代码,我将读取excel文件数据并填入var。之后,我将根据传入的参数过滤内容。但我一直收到上面标题中显示的错误消息。
以下是我的代码:
Hi I have the following codes in which I will read excel file data and populate into a var. After that I will filter the content based on the passed in parameters. But I keep getting the error message as shown in title above.
Below are my codes:
public ActionResult LoadPackStatusDashboardTotalWeek(string numberFamily, int weekNumber)
{
string filePathPlannerModule = ConstantValue.PageName.PackStatus.FilePath + "PlannerModule.xlsx";
var excelDataPlannerModule = MakeCells(filePathPlannerModule);
var excelDataPlannerModuleNew;//error here
if (excelDataPlannerModule.ModelList.Count > 0)
{
excelDataPlannerModuleNew = (from x in excelDataPlannerModule.ModelList
where x.CustomerNumber.Equals(numberFamily) && x.WeekNumber.Equals(weekNumber)
select new {x.FamilyName, x.WeekNumber}).ToList();
}
return Json(new { data = excelDataPlannerModuleNew },JsonRequestBehavior.AllowGet);
}
我的尝试:
1.尝试初始化excelDataPlannerModuleNew。
What I have tried:
1. Tried to initialize excelDataPlannerModuleNew.
推荐答案
突出显示的行无法确定变量的类型。编译器正在寻找右侧不存在的返回类型。
The line that you have highlighted can't determine the type of the variable. The compiler is looking for a return type from the right-hand-side which is not present.
var excelDataPlannerModuleNew = ???;
如果您只是初始化一个您正在做的变量,那么需要明确声明变量类型。
这篇文章解释了单词的不同含义:隐式与显式:有什么区别? [ ^ ]
当您使用错误消息时,您可以使用谷歌搜索找到更多信息:必须初始化隐式类型的局部变量 [ ^ ]
If you are only initializing a variable, which you are doing, you need to explicitly declare the variable type.
Here is an article that explains the different meanings of the words: Implicit vs. Explicit: What’s the Difference?[^]
You can find lots more information using google search when you use the error message: Implicitly-typed local variable must be initialized[^]
而不是初始化var,试试这个
1.创建一个包含FamilyName,WeekNumber属性的类,让我们调用它MyNewClass
2.更改此行选择新的{x.FamilyName,x.WeekNumber}至
Instead of initializing the var, try this
1. Create a class with FamilyName, WeekNumber property in it, let call it MyNewClass
2. Change this line select new {x.FamilyName, x.WeekNumber} to
select new MyNewClass {FamilyName = x.FamilyName, WeekNumber = x.WeekNumber}
然后更改var excelDataPlannerModuleNew; //这里错误
Then change var excelDataPlannerModuleNew;//error here
IList<MyNewClass> excelDataPlannerModuleNew = new List<MyNewClass>() ;
这篇关于必须初始化隐式类型的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!