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. How to override Field

How to override Field

Scheduled Pinned Locked Moved C#
tutorialquestion
5 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.
  • G Offline
    G Offline
    god4k
    wrote on last edited by
    #1

    Dear Sir and Madam, It is possible to override method, but how to override field. Thank You very much. public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? }

    G F 2 Replies Last reply
    0
    • G god4k

      Dear Sir and Madam, It is possible to override method, but how to override field. Thank You very much. public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? }

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

      You can't override a member variable. What is it that you wan't to accomplish?

      --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • G god4k

        Dear Sir and Madam, It is possible to override method, but how to override field. Thank You very much. public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? }

        F Offline
        F Offline
        Filip van der Meeren
        wrote on last edited by
        #3

        I really don't think you can override a field, never did it anyway... I can't see why you would override a field. Overriding a field isn't really OOP i guess ... But if you really want something that is close to the thing you want, you can use properties: Instead of this: public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? } use this: public class BaseClass { private int myInt = 0; protected virtual int i { get { return myInt;} } } public class SuperClass : BaseClass { private int otherInt = 0; protected override int i { get { return otherInt; } } } I didn't implement the set, it is just to show you the picture -- modified at 5:09 Friday 11th August, 2006 -- modified at 5:09 Friday 11th August, 2006

        Students in Belgium still come in handy, don't they ?

        G 1 Reply Last reply
        0
        • F Filip van der Meeren

          I really don't think you can override a field, never did it anyway... I can't see why you would override a field. Overriding a field isn't really OOP i guess ... But if you really want something that is close to the thing you want, you can use properties: Instead of this: public class BaseClass { protected int i; } public class ClassChild: BaseClass { //how to override i ????????? } use this: public class BaseClass { private int myInt = 0; protected virtual int i { get { return myInt;} } } public class SuperClass : BaseClass { private int otherInt = 0; protected override int i { get { return otherInt; } } } I didn't implement the set, it is just to show you the picture -- modified at 5:09 Friday 11th August, 2006 -- modified at 5:09 Friday 11th August, 2006

          Students in Belgium still come in handy, don't they ?

          G Offline
          G Offline
          god4k
          wrote on last edited by
          #4

          ThanX Sir.

          F 1 Reply Last reply
          0
          • G god4k

            ThanX Sir.

            F Offline
            F Offline
            Filip van der Meeren
            wrote on last edited by
            #5

            Lol sir, I am just some noob student in a country no1 ever heared of :p (Belgium)

            Don't you also love the code?

            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