I have a base class 'Description' for loading files describing an object. Its main purpose is to set up a dictionary with entries read out of the file using a reading-loading function from the class-method 'Read.Loader.RetrieveDescription'.
public class Description
{
// Variables
// =========
private Dictionary<string, string> entries;
// Constructors
// ============
public Description()
{
entries = null;
}
public Description(string fileId, Func<string, Read.Loader.Description> describer)
{
Read.Loader.RetrieveDescription(describer, fileId, ref entries);
}
}
Then I have a specialised class 'Element' that uses a specific describing function, DESCRIBER, from the class-method 'Read.Loader.RetrieveDescription'.
public class Element : Description
{
// Variables
// =========
private static Func<string, Read.Loader.Description> DESCRIBER = Read.Element.RetrieveDescription;
// Constructors
// ============
public Element()
: base() { }
public Element(string fileId, Func<string, Read.Loader.Description> describer)
: base(fileId, describer) { }
public Element(string fileId)
{
new Element(fileId, DESCRIBER);
}
}
The last constructor is meant to allow using the class downstream, without having to supply the 'DESCRIBER' argument. It does not work. For some reasons, doing so does not instantiate the object ("Object reference not set to an instance of an Object") of both Description and Element, whilst the second constructor does work.
When I call the constructor
Element elem = new Element(fileId)
from a static function outside the class, it seems to go directly to the no-argument constructor Description().
Thank you for your help.
Re-formulation of the question with a simpler (working) example
I have a base class 'Functional' for keeping the result of any functional of 'X' and 'Y' as 'fXY'.
namespace Space
{
public class Functional
{
private double X;
private double Y;
public double fXY;
public Functional()
{
X = 0.0;
Y = 0.0;
fXY = 0.0;
}
public Functional(double x, double y, Func<double, double, double> f)
{
X = x;
Y = y;
fXY = f(x, y);
}
}
}
For the sake of argument, I have two derived classes, ‘Plus’ and ‘Minus’, one handling addition and the other handling subtraction. In each, I have three constructors:
- The null-argument
- One with 'x', 'y' and the delegate function 'g'.
- One with 'x' and 'y', without the delegate function argument.
Say, I am always starting with a plus or a minus, before doing other things to the resulting variables 'fXY' and do not need to keep track of either 'X' or 'Y'. This setup allows me to do so.
To connect with the initial question, 'X' and 'Y' would be some files to load, and 'fXY' is the resulting dictionary I am interested in keeping and in working with.
namespace Space
{
public class Plus : Functional
{
public static double g(double x, double y) { return x + y; }
public Plus()
: base() { }
public Plus(double x, double y, Func<double, double, double> f)
: base(x, y, f)
{
this.fXY = base.fXY;
}
public Plus(double x, double y)
{
new Plus(x, y, g);
this.fXY = base.fXY;
}
}
public class Minus : Functional
{
public static double g(double x, double y) { return x - y; }
public Minus()
: base() { }
public Minus(double x, double y, Func<double, double, double> f)
: base(x, y, f)
{
this.fXY = base.fXY;
}
public Minus(double x, double y)
{
new Minus(x, y, g);
this.fXY = base.fXY;
}
}
}
In another namespace, I am now using the 'Minus' and 'Plus' objects.
- On the one hand, if I define the 'Plus' object using the two-argument constructor, as per below, the function 'fp' always returns zero.
- On the other hand, if I define the 'Minus' object using the three-argument constructor, the function 'fm' below returns the correct result.
Any idea as to how to why this is happening and how to fix this, so that using the simpler constructor becomes possible?
namespace Universe
{
public class Test
{
public static double fp(double x, double y)
{
Space.Plus gp = new Space.Plus(x, y);
return gp.fXY;
}
public static double fm(double x, double y)
{
Space.Minus gm = new Space.Minus(x, y, Space.Minus.g);
return gm.fXY;
}
}
}