Quite new to C#, from a long time ago C++ background and so I seem to be having my trouble transitioning away from pointers to ref in C#.
I have a class (EColour) which I create using the constructor shown.
I assign (or at least try to) a reference to cellTemplate to the variable m_template.
Looking in debug, at the time of construction, m_template is most definitely NOT null.
However, by the time I come to handle OnMouseClick event, I get a null exception error because m_template has magically turned to null.
Could anyone please shed light on what I have done wrong and how to fix it?
public EColour(ref ICellTemplate cellTemplate)
{
m_template = (ColourTemplate)cellTemplate;
}
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.AnyColor = m_template.AnyColour; // This throws an exception because m_template is null
base.OnMouseClick(e);
}
ColourTemplate m_template;
In C# we have two main kinds of types:
value type - its all digit types (int, float, double, long ...)
reference type -its types that inherited from object
ICellTemplate
is a reference class. So what you need - just send it in argument as a regular variable.
public class EColour
{
private ColourTemplate m_tamplate;
public EColour(ICellTemplate cellTemplate)
{
m_template = (ColourTemplate)cellTemplate;
}
}