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. calling a super class method from a derived class

calling a super class method from a derived class

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 4 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.
  • K Offline
    K Offline
    Kanjinghat
    wrote on last edited by
    #1

    Dear All, i have a class A. Class B derives class A and class C derives from class B and class D derives from class c. Now i need to call a method of class A from class D. is it possible? if so how can i acheive this. Is there any facility in C# that helps me to call the methods any of the classes A, B, C from class D? very urgent Thanks in advance

    Ramesh.Kanjinghat

    S P 2 Replies Last reply
    0
    • K Kanjinghat

      Dear All, i have a class A. Class B derives class A and class C derives from class B and class D derives from class c. Now i need to call a method of class A from class D. is it possible? if so how can i acheive this. Is there any facility in C# that helps me to call the methods any of the classes A, B, C from class D? very urgent Thanks in advance

      Ramesh.Kanjinghat

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      Have you heard About Inheritance. this will help you. Inheritance[^]

      Regards, Satips.:rose:

      K 1 Reply Last reply
      0
      • S Sathesh Sakthivel

        Have you heard About Inheritance. this will help you. Inheritance[^]

        Regards, Satips.:rose:

        K Offline
        K Offline
        Kanjinghat
        wrote on last edited by
        #3

        Dear Satips, I know very well what inheritance is. Here my problem class A has a method Method1 and Class B inherits class A and Class C inherits class B where Class C overides the Method1 and class D inherits class C. know when i call Methodi from class D the Method1 of class C will be called. but i want to call the Method1 of class A. We can use super keyword in C++. is there any thing in C# that helps us to acheive this? got this time you got it? thanks in advance

        Ramesh.Kanjinghat

        M 1 Reply Last reply
        0
        • K Kanjinghat

          Dear Satips, I know very well what inheritance is. Here my problem class A has a method Method1 and Class B inherits class A and Class C inherits class B where Class C overides the Method1 and class D inherits class C. know when i call Methodi from class D the Method1 of class C will be called. but i want to call the Method1 of class A. We can use super keyword in C++. is there any thing in C# that helps us to acheive this? got this time you got it? thanks in advance

          Ramesh.Kanjinghat

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, I think you have to do a little trick here. In class C you need a additional method, which calls the base.Method (method from class A).

          	public override void MethodFromA()
          	{
                           //Class C stuff
          	}
          
          	public void BaseMethodFromA()
          	{
          		base.MethodFromA();
          	}
          

          In class D you have to override the base method again and call the additional method in class C.

          	public override void MethodFromA()
          	{
          		BaseMethodFromA();
          	}
          

          Maybe there is some cleaner solution, which I'm not aware of, but I think it will work! All the best, Martin

          K 1 Reply Last reply
          0
          • K Kanjinghat

            Dear All, i have a class A. Class B derives class A and class C derives from class B and class D derives from class c. Now i need to call a method of class A from class D. is it possible? if so how can i acheive this. Is there any facility in C# that helps me to call the methods any of the classes A, B, C from class D? very urgent Thanks in advance

            Ramesh.Kanjinghat

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

            No, not once the method has been hidden (with override or new).

            1 Reply Last reply
            0
            • M Martin 0

              Hello, I think you have to do a little trick here. In class C you need a additional method, which calls the base.Method (method from class A).

              	public override void MethodFromA()
              	{
                               //Class C stuff
              	}
              
              	public void BaseMethodFromA()
              	{
              		base.MethodFromA();
              	}
              

              In class D you have to override the base method again and call the additional method in class C.

              	public override void MethodFromA()
              	{
              		BaseMethodFromA();
              	}
              

              Maybe there is some cleaner solution, which I'm not aware of, but I think it will work! All the best, Martin

              K Offline
              K Offline
              Kanjinghat
              wrote on last edited by
              #6

              Thanks

              Ramesh.Kanjinghat

              M 1 Reply Last reply
              0
              • K Kanjinghat

                Thanks

                Ramesh.Kanjinghat

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                You are wellcome!

                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