Reputation: 249
I am using C# with VS2013 and .Net 4.5.2.
I create a variable, assign it a value if a certain other variable is a boolean set to true. Using the same variable later, to see if I should process anything involving that variable.
FileInfo newFile;
ExcelPackage assetTemplate;
ExcelWorksheet assetWorkbook;
if (load.assetDelivery) // Bool in another class
{
newFile = new FileInfo(load.assetDeliveryTemplate);
assetTemplate = new ExcelPackage(newFile);
assetWorkbook = assetTemplate.Workbook.Worksheets[1];
}
// In this function later on, assetWorkbook and assetTemplate are causing errors
// Error 1 Use of unassigned local variable 'assetWorkbook'
if (load.assetDelivery)
{
assetWorkbook.Cells.AutoFitColumns();
destination = load.exportFileDestination + load.assetDeliveryName + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
assetTemplate.SaveAs(new FileInfo(destination));
}
Upvotes: 0
Views: 97
Reputation: 101681
Assign null
to your variables:
ExcelPackage assetTemplate = null;
ExcelWorksheet assetWorkbook = null;
Upvotes: 2
Reputation: 6359
You can use defaults (c.f. the other answers), but this looks like an example of some missing functional composition. If you had a function that looked something like
public void CreateSheet(string exportDestination, string assetName)
{
FileInfo newFile = new FileInfo(load.assetDeliveryTemplate);
ExcelPackage assetTemplate = new ExcelPackage(newFile);
ExcelWorksheet assetWorkbook = assetTemplate.Workbook.Worksheets[1];
assetWorkbook.Cells.AutoFitColumns();
destination = exportDestination + assetName + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
assetTemplate.SaveAs(new FileInfo(destination));
}
It would always be safe.
Upvotes: 0
Reputation: 223207
Compiler is complaining because there is no way for it to know that control flow will enter the check (true part) and your fields/variables will be definitely assigned. Hence the error.
You can fix that by either assigning those fields default values,
ExcelPackage assetTemplate = default(ExcelPackage);
ExcelWorksheet assetWorkbook = default(ExcelWorksheet);
or by specifying an else part:
if (load.assetDelivery) // Bool in another class
{
newFile = new FileInfo(load.assetDeliveryTemplate);
assetTemplate = new ExcelPackage(newFile);
assetWorkbook = assetTemplate.Workbook.Worksheets[1];
}
else
{
ExcelPackage assetTemplate = default(ExcelPackage);
ExcelWorksheet assetWorkbook = default(ExcelWorksheet);
}
See: 5.3 Definite assignment - MSDN
Upvotes: 2
Reputation: 40393
In C#, you must guarantee that you initialize a local variable prior to using it. In this scenario, assetWorkbook
will never be assigned a value if your first if
fails, which means the compiler can't guarantee that it was ever initialized prior to use.
If you give them an initial value, even null
, then your code will compile - just remember that if the value is null
, then you're subject to a NullReferenceException
if it never gets a real value, so make sure you either do a null check first, or are 100% sure you gave it a value.
Upvotes: 0
Reputation: 59232
Just initialize it with default
value.
ExcelWorksheet assetWorkbook = default(ExcelWorksheet);
Upvotes: 0