I'm getting Resharper warnings here in the base form where I set the EditMyDataObject property, and in the extended form where I grab the Text value and then restore it afterwards.
In both cases nothing is blowing up when I run the app. I don't think there's any potential for problems as implemented because my overridden property doesn't depend on anything initialized in the constructor, but would appreciate a 2nd opinion before gagging Resharper.
public class MyDataObject
{
//Data Members
public MyDataObject()
{
}
}
public class MyDataObjectEx : MyDataObject
{
//Data Members
public MyDataObjectEx()
{
}
public MyDataObjectEx(MyDataObject myDataObject)
{
}
}
public partial class MyDataObjectEditFrm : Form
{
private MyDataObject _myDataObject;
protected virtual MyDataObject EditMyDataObject
{
get { return _myDataObject; }
set { _myDataObject = value; }
}
/// <summary>
/// Parameterless constructor needed for designer support of derived classes.
/// </summary>
protected MyDataObjectEditFrm()
{
InitializeComponent();
}
protected MyDataObjectEditFrm(MyDataObject myDataObject)
{
InitializeComponent();
EditMyDataObject = myDataObject; // Warning: Virtual member call in a constructor
Text = GetDialogNameFromInputParameters()
//Remainder of initialization here
}
GetDialogNameFromInputParameters()
{
//Figure out what the text should be
}
}
public partial class MyDataObjectExEditFrm : MyDataObjectEditFrm
{
private MyDataObjectEx _myDataObjectEx;
protected override MyDataObject EditMyDataObject
{
get { return _myDataObjectEx; }
set
{
if (value == null)
_myDataObjectEx = null;
else _myDataObjectEx = value as MyDataObjectEx ?? new MyDataObjectEx(value);
}
}
public MyDataObjectExEditFrm(MyDataObject myDataObject) : base(myDataObject)
{
//preserve the value computed and set in the base class to prevent the generic form name from the designer overriding it here
string dialogText = Text; // Warning: Virtual member call in a constructor
InitializeComponent();
Text = dialogText; // Warning: Virtual member call in a constructor
//Remainder of additional initialization for extended data here
}
}
As long as each derived class overrides this virtual member in a way that it doesn't rely on variables that are initialized in the constructor of that derived class, you are safe. Problem is, you can't know that every class that derives from your base class behaves like this. Because of that, the warning is legit.