I want to load a screen with a ViewModel depending on the option selected.
I thought Inheritance would be key here, as a lot of the properties are the same. Below is an extract of the code that I have.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bool isHandheld = false;
var pdv1 = isHandheld == true ? new PDVHH() : new PDV();
txtCaseID.Text = pdv1.CaseID;
txtPDV.Text = isHandheld == true ? pdv1.PDVString : string.Empty;
txtPDVHH.Text = isHandheld == true ? pdv1.PDVHHString : string.Empty;
}
}
class basePDV
{
public string CaseID { get; set; }
}
class PDV : basePDV
{
public string PDVString { get; set; }
}
class PDVHH : basePDV
{
public string PDVHHString { get; set; }
}
The error I am receiving is... "Type of conditional expression cannot be determined because there is no implicit conversion between 'WindowsFormsApplication1.PDVHH' and 'WindowsFormsApplication1.PDV'"
I'm hoping someone can give me some guidance on a solution for this.
Ok, your question is not really about inheritance or ViewModels but about how
?:
(the conditional operator) works.The following will fix it:
While your code looks plausible, the docs say that you need a conversion between PDVHH and PDV, either direction, but the conversion to basePDV isn't considered.
When you can find a common name for PDVString and PDVHHString and implement it as a single property in the base class, it might work like you want. And even simpler. Note that
class PDV : basePDV {}
is Ok.