I implemented an extension method to get fields from an Excel file (everything in .NET Framework 4.8), but the fields
object is not populatedd with Field
values in the Main()
method, while it really has values inside the extension method. Does anyone see any issue? Am I missing an out
keywork somewhere?
I'mm afraid that there are two references / pointers towards different fields objects..
Also, do I really have to instantiate fields
before "applying" an extension method?
static void Main(string[] args)
{
Fields fields = new Fields(); // Fields is a class which implements IEnumerable<Field>
fields.ReadFromExcel(); // fields is still null here, despite having values within the extension method
}
Extension method (simplified)
public static class ExtensionMethods
{
public static void ReadFromExcel(this Fields fields)
{
ExcelConnector excel = new ExcelConnector();
fields = excel.ReadFields(); // fields is correctly populated with fields from Excel here
}
}
Thanks for any insights :-)
Update
If I change to fields = Processor.ReadFromExcel();
and
public static class Processor
{
public static Fields ReadFromExcel()
{
// ...
return excel.ReadFields;
}
}
it works well, but then loose the extension method features...