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 can i??

How can i??

Scheduled Pinned Locked Moved C#
question
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.
  • H Offline
    H Offline
    hotthoughtguy
    wrote on last edited by
    #1

    How can i build a program that hv two classes (child class and parant class) child class is inheriting from parant class. Now child class has two methods 1.which returns the name of parant class 2.which returns the object of parant class

    S L N 3 Replies Last reply
    0
    • H hotthoughtguy

      How can i build a program that hv two classes (child class and parant class) child class is inheriting from parant class. Now child class has two methods 1.which returns the name of parant class 2.which returns the object of parant class

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Hi you could use reflection. This would be in the child class. public string GetParentClassName() { return this.GetType().BaseType.Name; } Hmm, the child itself is an object of the parent class. Don't know exactly what you mean with this. Regards Sebastian

      It's not a bug, it's a feature! Me in Softwareland.

      1 Reply Last reply
      0
      • H hotthoughtguy

        How can i build a program that hv two classes (child class and parant class) child class is inheriting from parant class. Now child class has two methods 1.which returns the name of parant class 2.which returns the object of parant class

        L Offline
        L Offline
        Lev Danielyan
        wrote on last edited by
        #3

        I am not sure what do you mean by saying the Name and Object of parent class, but you can get the Type of the base class like this: public class Dad { } public class Kid : Dad { public Type GetParentType() { return this.GetType().BaseType; } }

        Regards, Lev

        H 1 Reply Last reply
        0
        • L Lev Danielyan

          I am not sure what do you mean by saying the Name and Object of parent class, but you can get the Type of the base class like this: public class Dad { } public class Kid : Dad { public Type GetParentType() { return this.GetType().BaseType; } }

          Regards, Lev

          H Offline
          H Offline
          hotthoughtguy
          wrote on last edited by
          #4

          thanks... thats what i want... this was my 1st post on this site... i really like this platform...

          1 Reply Last reply
          0
          • H hotthoughtguy

            How can i build a program that hv two classes (child class and parant class) child class is inheriting from parant class. Now child class has two methods 1.which returns the name of parant class 2.which returns the object of parant class

            N Offline
            N Offline
            Nissim Salomon
            wrote on last edited by
            #5

            Hi when you create a class that inherit from another class you must specify the base class type, this is the solution for your question.

            class Base
            {
                public Base()
                {
            
                }    
            }
            
            class Derieved : Base
            {       
                public string GetPerentName()
                {
                    return typeof(Base).Name;
                }
            
                public Base GetPerentObject()
                {
                    return (Base)this;
                }
            }
            
            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