Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Inheritance

Inheritance

Scheduled Pinned Locked Moved C#
csharpcomoophelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JammoD87
    wrote on last edited by
    #1

    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

    L S J 3 Replies Last reply
    0
    • J JammoD87

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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();

      J 1 Reply Last reply
      0
      • L Lost User

        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();

        J Offline
        J Offline
        JammoD87
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • J JammoD87

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          And yet you have to. That's the most specific type that variable can have - the least common ancestor of the types it may have at runtime. So if a property must be accessible, it has to be in the base type. Of course, the derived types may override it.

          1 Reply Last reply
          0
          • J JammoD87

            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

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #5

            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 boolean

            txtCaseID.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

            1 Reply Last reply
            0
            • J JammoD87

              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

              J Offline
              J Offline
              JammoD87
              wrote on last edited by
              #6

              Hi Both, Thanks for the replies, I think I need to look into changing this solution. :)

              Personal Blog: A Software Programmer Twitter: JammoD

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups