calling a variable from parent class to base class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ranjith Reddy Bandi
    New Member
    • May 2007
    • 6

    calling a variable from parent class to base class

    i want to call a varible from parent class to base using inhertance in c#.net i please give immedate replay......Tha nk You
  • mwalts
    New Member
    • May 2007
    • 38

    #2
    Originally posted by Ranjith Reddy Bandi
    i want to call a varible from parent class to base using inhertance in c#.net i please give immedate replay......Tha nk You
    I think there is a terminology issue here,

    A parent class is a base class.

    If you want to use a method from the class you inherit from, use the base keyword.

    I think your going to have to re-word your question

    -mwalts

    Comment

    • cena
      New Member
      • Jun 2007
      • 1

      #3
      Hi Ranjith,
      by using mybase property call the base class variable into child class

      Comment

      • Munawar
        New Member
        • May 2007
        • 12

        #4
        Hi,

        Yes in C# you can get any public member of parent class by using keyword "base"

        Like here is scenario
        [code=c]
        public class MyParent
        {
        public string parentString;

        public MyParent() {

        Console.WriteLi ne("Parent Constructor.");
        }

        public MyParent(string myString)
        {
        parentString = myString;
        Console.WriteLi ne(parentString );
        }
        public void print()
        {

        Console.WriteLi ne( "Iam parent; " + parentString);

        }
        }

        Class myChild : MyParent
        {
        //Calling parent constructor
        public MyChild() : base("From Derived")
        {

        Console.WriteLi ne("Child Constructor");

        }

        public new void print()
        {
        //here you call parent property /member using base
        base.parentstri ng="munawar"
        base.print();

        Console.WriteLi ne("I'm a Child Class.");

        }

        }


        public static void Main()
        {

        MyChild child = new MyChild();

        child.print();

        ((MyChild)child ).print();

        }
        [/code]


        Thanks
        Munawar Hussain

        Comment

        • Ranjith Reddy Bandi
          New Member
          • May 2007
          • 6

          #5
          Thank you...........

          Comment

          • Ranjith Reddy Bandi
            New Member
            • May 2007
            • 6

            #6
            Hi cena
            Can u give the syntax plzz....

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by Ranjith Reddy Bandi
              Hi cena
              Can u give the syntax plzz....
              Hi Ranjith,
              Welcome to TDSN.

              Have you tried Munawar's example?
              It seems pretty complete to me.

              -Frinny

              Comment

              Working...