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 invoke base class methods instead overridens [modified]

How to invoke base class methods instead overridens [modified]

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
16 Posts 5 Posters 1 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.
  • realJSOPR realJSOP

    First, you have to do this:

    class DervClass : BaseClass

    Second, in your derived class, you have to do this:

    protected override void Fun2()
    {
    base.Fun2();
    }

    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

    E Offline
    E Offline
    ElCachubrey
    wrote on last edited by
    #5

    John Simmons / outlaw programmer wrote:

    First, you have to do this: class DervClass : BaseClass

    Yes, it's just misstyping.

    John Simmons / outlaw programmer wrote:

    econd, in your derived class, you have to do this: protected override void Fun2() { base.Fun2(); }

    I can't do this in derived class snice implemented logic.

    realJSOPR 1 Reply Last reply
    0
    • E ElCachubrey

      N a v a n e e t h wrote:

      you can use base.FunctionName(). But the code provided by you is not showing the inheritance.

      Thats the point. I want to invoke base method from the base class!!!

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #6

      El'Cachubrey wrote:

      I want to invoke base method from the base class!!!

      :confused: Base classes methods are invoked using base keyword from a child class. What are you trying to do?

      Navaneeth How to use google | Ask smart questions

      E 1 Reply Last reply
      0
      • N N a v a n e e t h

        El'Cachubrey wrote:

        I want to invoke base method from the base class!!!

        :confused: Base classes methods are invoked using base keyword from a child class. What are you trying to do?

        Navaneeth How to use google | Ask smart questions

        E Offline
        E Offline
        ElCachubrey
        wrote on last edited by
        #7

        Not base classes!!!. I try to invoke from base class method other method of the same base class even if one overriden in derived class. Like that in C++ ((BaseClass)this).Fun2(); But in C# - it's diffrent i wan't to know how. :confused::confused::confused:

        1 Reply Last reply
        0
        • E ElCachubrey

          John Simmons / outlaw programmer wrote:

          First, you have to do this: class DervClass : BaseClass

          Yes, it's just misstyping.

          John Simmons / outlaw programmer wrote:

          econd, in your derived class, you have to do this: protected override void Fun2() { base.Fun2(); }

          I can't do this in derived class snice implemented logic.

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #8

          El'Cachubrey wrote:

          I can't do this in derived class snice implemented logic.

          First you tell us you want to do what I told you how to do, and then you tell us you *can't* do it because of logic in your code. Make up your freakin' mind, or stop wasting our time. If your "logic" prevents you from doing what you need to do, then your code is completely screwed up, and you need to start over.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          E 1 Reply Last reply
          0
          • E ElCachubrey

            Hi all. I have OOP Question. Can i invoke base class methods instead overridens; in C# Examp: class BaseClass { public void Fun1() { this.Fun2();//I want write here - base class method //What i should write here for invoke Fun2() of this class (exactly this class, // not derived), i suppose what i shoul write as in C++ ((BaseClass)this).Fun2(); //But this not work, in this case still invoke method of derived class. } protected virtual void Fun2() { WriteLine("base class method"); } } class DervClass : BaseClass { protected overriden void Fun2() { WriteLine("overriden class method"); } } .... DervClass derv = new DervClass(); derv.Fun1();

            modified on Wednesday, December 17, 2008 6:37 AM

            S Offline
            S Offline
            Simon P Stevens
            wrote on last edited by
            #9

            First let me check I understand what you are trying to do

            class Program
            {
            static void Main(string[] args)
            {
            MySub instance = new MySub();

                    instance.CallMe();
            
                    Console.ReadLine();
                }
            }
            
            public class MyBase
            {
                public virtual void Method1()
                {
                    Console.WriteLine("I want this to run");
                }
            
                public void CallMe()
                {
                    Method1();            
                }
            }
            
            public class MySub : MyBase
            {
                public override void Method1()
                {
                    Console.WriteLine("But this is what really gets run");
                }
            }
            

            You create a new instance of the sub class, and call "CallMe" which triggers a method. You want it to trigger the method in the base class, and not the one in the sub class. Well, like others have said, the standard way to do this would be to put base.Method1() in the Method1 in the subclass to call the base method. But for some reason you don't want to do that. I'm assuming you don't want to do that because sometimes, when you call method1 directly on the subclass, you do want it to run. This is the nature of what 'override' means. The method1 in the subclass overrides the method1 in the base. Perhaps the override behaviour isn't what you want. Perhaps you should just consider naming the methods with different names. Then you can call which ever one you want to call. Alternatively, consider using the 'new' keyword.

            public class MySub : MyBase
            {
                public new void Method1()
                {
                    Console.WriteLine("But this is what really gets run");
                }
            }
            

            Using the 'new' keyword makes the method1 in the sub class hide the method1 in the base class, but not override it. This means when you cast back down to a base class (or call from within the base class) it's the base's method that gets run, because nothing is overriding it, instead, when you have an instance of the subclass, the method1 just hides the bases method1. Confusing. Yeah. I can't explain it very clearly. Try running the snippets I've given you and see the difference. Be careful if you use new. This isn't very standard behaviour, and other people may easily be surprised by what happens. You should probably just be naming your methods differently if they have different behaviour. Some links on the subject: htt

            E 1 Reply Last reply
            0
            • realJSOPR realJSOP

              El'Cachubrey wrote:

              I can't do this in derived class snice implemented logic.

              First you tell us you want to do what I told you how to do, and then you tell us you *can't* do it because of logic in your code. Make up your freakin' mind, or stop wasting our time. If your "logic" prevents you from doing what you need to do, then your code is completely screwed up, and you need to start over.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              E Offline
              E Offline
              ElCachubrey
              wrote on last edited by
              #10

              Cool up. I don't beg you to respond my question. I make example just for explanate what i try to do and i mean exactly what i mean how i can invoke base class member from other base class member even if it's one overriden in derived class. Be more attentive in future :laugh: .

              1 Reply Last reply
              0
              • S Simon P Stevens

                First let me check I understand what you are trying to do

                class Program
                {
                static void Main(string[] args)
                {
                MySub instance = new MySub();

                        instance.CallMe();
                
                        Console.ReadLine();
                    }
                }
                
                public class MyBase
                {
                    public virtual void Method1()
                    {
                        Console.WriteLine("I want this to run");
                    }
                
                    public void CallMe()
                    {
                        Method1();            
                    }
                }
                
                public class MySub : MyBase
                {
                    public override void Method1()
                    {
                        Console.WriteLine("But this is what really gets run");
                    }
                }
                

                You create a new instance of the sub class, and call "CallMe" which triggers a method. You want it to trigger the method in the base class, and not the one in the sub class. Well, like others have said, the standard way to do this would be to put base.Method1() in the Method1 in the subclass to call the base method. But for some reason you don't want to do that. I'm assuming you don't want to do that because sometimes, when you call method1 directly on the subclass, you do want it to run. This is the nature of what 'override' means. The method1 in the subclass overrides the method1 in the base. Perhaps the override behaviour isn't what you want. Perhaps you should just consider naming the methods with different names. Then you can call which ever one you want to call. Alternatively, consider using the 'new' keyword.

                public class MySub : MyBase
                {
                    public new void Method1()
                    {
                        Console.WriteLine("But this is what really gets run");
                    }
                }
                

                Using the 'new' keyword makes the method1 in the sub class hide the method1 in the base class, but not override it. This means when you cast back down to a base class (or call from within the base class) it's the base's method that gets run, because nothing is overriding it, instead, when you have an instance of the subclass, the method1 just hides the bases method1. Confusing. Yeah. I can't explain it very clearly. Try running the snippets I've given you and see the difference. Be careful if you use new. This isn't very standard behaviour, and other people may easily be surprised by what happens. You should probably just be naming your methods differently if they have different behaviour. Some links on the subject: htt

                E Offline
                E Offline
                ElCachubrey
                wrote on last edited by
                #11

                Nicely. Instead "Confusing" it's very clear. Let's make a count: 1. There is no exists way to invoke Base Class method if they overriden in derived classes. 2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.

                S realJSOPR 2 Replies Last reply
                0
                • E ElCachubrey

                  Nicely. Instead "Confusing" it's very clear. Let's make a count: 1. There is no exists way to invoke Base Class method if they overriden in derived classes. 2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.

                  S Offline
                  S Offline
                  Simon P Stevens
                  wrote on last edited by
                  #12

                  El'Cachubrey wrote:

                  1. There is no exists way to invoke Base Class method if they overriden in derived classes.

                  You can call base.method1() from the derived class. I don't know of any other way. Although I'm not Anders[^], there could be a way, I just can't think of one. Perhaps someone with more experience can confirm or deny this.

                  El'Cachubrey wrote:

                  2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.

                  Well, like I said, it might not be the only way. It's a way though. Just be aware that the 'new' keyword causes different behaviour to the override keyword. What you are doing is conceptually something slightly different. Make sure you understand all the behaviour differences and consequences and that they are what you want before you do it. (And don't forget, other people who use your code won't be expecting this, so note it heavily in the comments, or consider naming the methods differently)

                  Simon

                  E 1 Reply Last reply
                  0
                  • S Simon P Stevens

                    El'Cachubrey wrote:

                    1. There is no exists way to invoke Base Class method if they overriden in derived classes.

                    You can call base.method1() from the derived class. I don't know of any other way. Although I'm not Anders[^], there could be a way, I just can't think of one. Perhaps someone with more experience can confirm or deny this.

                    El'Cachubrey wrote:

                    2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.

                    Well, like I said, it might not be the only way. It's a way though. Just be aware that the 'new' keyword causes different behaviour to the override keyword. What you are doing is conceptually something slightly different. Make sure you understand all the behaviour differences and consequences and that they are what you want before you do it. (And don't forget, other people who use your code won't be expecting this, so note it heavily in the comments, or consider naming the methods differently)

                    Simon

                    E Offline
                    E Offline
                    ElCachubrey
                    wrote on last edited by
                    #13

                    GRATEFULL

                    1 Reply Last reply
                    0
                    • E ElCachubrey

                      Nicely. Instead "Confusing" it's very clear. Let's make a count: 1. There is no exists way to invoke Base Class method if they overriden in derived classes. 2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #14

                      El'Cachubrey wrote:

                      1. There is no exists way to invoke Base Class method if they overriden in derived classes.

                      Wrong, wrong, wrong. I showed you how to do it.

                      El'Cachubrey wrote:

                      2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class. Quote Selected Text

                      Wrong again. If you make a "new" method, it completely hides the base version from other objects that use your derived class.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      1 Reply Last reply
                      0
                      • E ElCachubrey

                        Hi all. I have OOP Question. Can i invoke base class methods instead overridens; in C# Examp: class BaseClass { public void Fun1() { this.Fun2();//I want write here - base class method //What i should write here for invoke Fun2() of this class (exactly this class, // not derived), i suppose what i shoul write as in C++ ((BaseClass)this).Fun2(); //But this not work, in this case still invoke method of derived class. } protected virtual void Fun2() { WriteLine("base class method"); } } class DervClass : BaseClass { protected overriden void Fun2() { WriteLine("overriden class method"); } } .... DervClass derv = new DervClass(); derv.Fun1();

                        modified on Wednesday, December 17, 2008 6:37 AM

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #15

                        By marking the method in the base class virtual and the method in the derived class override, you are stating that you do not want to be able to call the base class' version directly from an instance of the derived class, and therefore it can't be done. If, you do want to be able to call the base class' version from an instance of the derived class, then do not use virtual and override.

                        realJSOPR 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          By marking the method in the base class virtual and the method in the derived class override, you are stating that you do not want to be able to call the base class' version directly from an instance of the derived class, and therefore it can't be done. If, you do want to be able to call the base class' version from an instance of the derived class, then do not use virtual and override.

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #16

                          PIEBALDconsult wrote:

                          and therefore it can't be done.

                          Well, not *technically*...

                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                          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