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 of class properties

Inheritance of class properties

Scheduled Pinned Locked Moved C#
tutorialoopquestion
3 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.
  • R Offline
    R Offline
    redivider
    wrote on last edited by
    #1

    I'm trying to figure out how to override the value of a propery for a parent class in a child class. For example, i have public class Base { public virtual string _Caption="parentresult";} public class Child { public override string _Caption="childresult";} But the "virtual" and "override" keywords raise errors during compilation("modifier not valid for this item). They work if I set them up accessor methods, but I shouldn't have to do that (should I?). Is it possible to make them consts, also?

    G G 2 Replies Last reply
    0
    • R redivider

      I'm trying to figure out how to override the value of a propery for a parent class in a child class. For example, i have public class Base { public virtual string _Caption="parentresult";} public class Child { public override string _Caption="childresult";} But the "virtual" and "override" keywords raise errors during compilation("modifier not valid for this item). They work if I set them up accessor methods, but I shouldn't have to do that (should I?). Is it possible to make them consts, also?

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      redivider.geo, I think you can only override methods/properties and not variables. Also, your Child class does not inherit Base so it has nothing to override.

      public class Base
      {
      private string _caption = "parentresult";
      public virtual string Caption
      {
      get { return _caption; }
      set { _caption = value; }
      }
      }

      public class Child : Base
      {
          public override string Caption
          {
              get { return base.Caption; }
              set { base.Caption = value; }
          }
      }
      

      Regards, Gareth. (FKA gareth111)

      1 Reply Last reply
      0
      • R redivider

        I'm trying to figure out how to override the value of a propery for a parent class in a child class. For example, i have public class Base { public virtual string _Caption="parentresult";} public class Child { public override string _Caption="childresult";} But the "virtual" and "override" keywords raise errors during compilation("modifier not valid for this item). They work if I set them up accessor methods, but I shouldn't have to do that (should I?). Is it possible to make them consts, also?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Variables can not be virtual. What you are doing is not overriding, it's shadowing. To tell the compiler that you intend to shadow the variable in the parent class, you use the new keyword:

        public class Base {
        public string _Caption="parentresult";
        }

        public class Child : Base {
        public new string _Caption="childresult";
        }

        If you want to override something, it has to be a property or a method.

        Despite everything, the person most likely to be fooling you next is yourself.

        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