Inheritance
-
Hi, 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. Thanks,
Personal Blog: A Software Programmer Twitter: JammoD
-
Hi, 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. Thanks,
Personal Blog: A Software Programmer Twitter: JammoD
-
C#'s ternary operator is annoying like that[^] (even when var is not used), but you can just cast everything to the base type and it will work. Like this (not tested)
var pdv1 = isHandheld ? (basePDV)new PDVHH() : (basePDV)new PDV();
-
Hi, If I cast it as basePDV, I am unable to access the properties inside PDV and PDVHH. Thanks,
Personal Blog: A Software Programmer Twitter: JammoD
-
Hi, 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. Thanks,
Personal Blog: A Software Programmer Twitter: JammoD
Suggestions: If this isn't a quickly abstracted example but your actual code then don't include the type name in the name of fields or properties and also not the class name. "PDVString" doesn't convey any useful meaning. If it is a description, name it "Description" or likewise for whatever it is. And to me it looks like you could just as well make it a base class member. For handhelds it will contain the description (or whatever) for handhelds and else for non-handhelds. Likewise you could replace the TextBoxes "txtPDV" and "txtPDVHH" by a single TextBox "txtDescription" and thus do away with any type distinction:
var pdv1 = isHandheld /* == true */ ? (basePDV)new PDVHH() : (basePDV)new PDV();
// ^^^ superfluous because isHandheld already is a booleantxtCaseID.Text = pdv1.CaseID;
txtDescription.Text = pdv1.Description;If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Hi, 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. Thanks,
Personal Blog: A Software Programmer Twitter: JammoD