locked
Parent class from child class RRS feed

  • Question

  • How to call same function of parent class from child class
    Sunday, March 18, 2018 1:57 PM

All replies

  • Parent class and child class are strange names in .Net

    If you mean base and derived class then you can if the method is not overridden call the method. 

    If it is overridden (or shadowed) then you can cast to the base class. 

    In VB DirectCast(TheDerivedClass, TheBaseClass).TheMethod

    In C#

    (TheBaseClass) TheDerivedClass.TheMethod();


    Success
    Cor

    Sunday, March 18, 2018 5:03 PM
  • Hi HelloBoby11,

    Thank you for posting here.

    If you want to call parent class method in child class, here is a simple code sample for your reference.

     class _0320
        {
            static void Main(string[] args)
            {
                Child.SameFunction();
            }
        }
        public class Parent
        {
            public static void ParentMethod()
            {
                Console.WriteLine("ParentMethod");
            }
        }
        public class Child : Parent
        {
            public static void SameFunction()
            {
                ParentMethod();
            }
        }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 20, 2018 2:50 AM