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. Finding base class of an object

Finding base class of an object

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

    Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.

    abstract public class MyBaseClass1 : Form
    {
        abstract public void MyMethod1();
    }
    
    abstract public class MyBaseClass2 : Form
    {
        abstract public void MyMethod2();
    }
    

    I have many forms which are inheriting from these base classes. For example

    public partial class Form1 : MyBaseClass1
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        public override void MyMethod1()
        {
            MessageBox.Show("mymethod1");
        }
    }
    

    These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form ActiveMdiChild. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, Samar

    J A realJSOPR 3 Replies Last reply
    0
    • D dashingsidds

      Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.

      abstract public class MyBaseClass1 : Form
      {
          abstract public void MyMethod1();
      }
      
      abstract public class MyBaseClass2 : Form
      {
          abstract public void MyMethod2();
      }
      

      I have many forms which are inheriting from these base classes. For example

      public partial class Form1 : MyBaseClass1
      {
          public Form1()
          {
              InitializeComponent();
          }
      
          public override void MyMethod1()
          {
              MessageBox.Show("mymethod1");
          }
      }
      

      These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form ActiveMdiChild. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, Samar

      J Offline
      J Offline
      JoeSharp
      wrote on last edited by
      #2

      if (ActiveMdiChild is MyBaseClass1) { // your code hier }

      1 Reply Last reply
      0
      • D dashingsidds

        Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.

        abstract public class MyBaseClass1 : Form
        {
            abstract public void MyMethod1();
        }
        
        abstract public class MyBaseClass2 : Form
        {
            abstract public void MyMethod2();
        }
        

        I have many forms which are inheriting from these base classes. For example

        public partial class Form1 : MyBaseClass1
        {
            public Form1()
            {
                InitializeComponent();
            }
        
            public override void MyMethod1()
            {
                MessageBox.Show("mymethod1");
            }
        }
        

        These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form ActiveMdiChild. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, Samar

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        You have a few options, you can as previously stated use the is keyword and see if it's the type you are looking for. An alternative is to actually look at the BaseType:

        Type t = ActiveMdiChild.GetType();
        //You can then look at t.BaseType, I'd like to point out that if you inherit from
        //an interface you will have to look at FindInterfaces

        Type baseClass = t.BaseType;

        //You can then create an instance of this type
        object something = Activator.CreateInstance(baseClass);

        However, unless there is a common base class that you can cast to so that you can use it then you are back to checking the actual type and casting to that. So depending on what you have and what you need you can either have a series of if statements checking for the types or the above method where you will be able to create and use your base class without necessarily knowing exactly what it is.

        My current favourite quote is: Punch them in the face, see what happens!

        -SK Genius

        D 1 Reply Last reply
        0
        • A Anthony Mushrow

          You have a few options, you can as previously stated use the is keyword and see if it's the type you are looking for. An alternative is to actually look at the BaseType:

          Type t = ActiveMdiChild.GetType();
          //You can then look at t.BaseType, I'd like to point out that if you inherit from
          //an interface you will have to look at FindInterfaces

          Type baseClass = t.BaseType;

          //You can then create an instance of this type
          object something = Activator.CreateInstance(baseClass);

          However, unless there is a common base class that you can cast to so that you can use it then you are back to checking the actual type and casting to that. So depending on what you have and what you need you can either have a series of if statements checking for the types or the above method where you will be able to create and use your base class without necessarily knowing exactly what it is.

          My current favourite quote is: Punch them in the face, see what happens!

          -SK Genius

          D Offline
          D Offline
          dashingsidds
          wrote on last edited by
          #4

          Hmm.. This looks good but i wont get the methods of the base class object in the intellisense here. Is there a way of getting that too? Maybe i am asking for too much here. :) Regards, Samar

          1 Reply Last reply
          0
          • D dashingsidds

            Hi Experts, This should be a very trivial question for you guys. I have an object with me. This object is an object of a class who is inherting from an abstract base class. I want to know what is that base class. Let me give you an example Say I have 2 abstract base classes namely MyBaseClass1 and MyBaseClass2 which is inheriting from a Form class so my code would be something like this.

            abstract public class MyBaseClass1 : Form
            {
                abstract public void MyMethod1();
            }
            
            abstract public class MyBaseClass2 : Form
            {
                abstract public void MyMethod2();
            }
            

            I have many forms which are inheriting from these base classes. For example

            public partial class Form1 : MyBaseClass1
            {
                public Form1()
                {
                    InitializeComponent();
                }
            
                public override void MyMethod1()
                {
                    MessageBox.Show("mymethod1");
                }
            }
            

            These forms will be opened in an MDI form. I want to know which is my active form. This i can get from the property of the MDI form ActiveMdiChild. But this property will return me a Form object. I want to know what is the base class of this form object i.e. whether it is MyBaseClass1 or MyBaseClass2. I also want to create an object of this base class if possible. Please do let me know if I hv not explained my query properly. Please help! Thanks in advance! Regards, Samar

            realJSOPR Online
            realJSOPR Online
            realJSOP
            wrote on last edited by
            #5

            Check out this article: The case for a generic C# converter using operators[^]

            .45 ACP - because shooting twice is just silly
            -----
            "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." - J. Jystad, 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