Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public   class clsb
   {
       public string subjectName = "";
       public string mainsubject()
       {
          return subjectName;

       }
   }

 public  class clsA : clsb
   {
       public string subjectName = "";
       public string Allsubject()
       {
               return subjectName;
       }
   }

   private void getdata()
       {
                clsA obj = new clsA();
               obj.subjectName = "English";
               string A2 = obj.mainsubject();
               MessageBox.Show(A2);
  }


What I have tried:

How to fill the "clsb.subjectName" variable with a value in getdata
Posted
Updated 31-May-23 2:16am

1 solution

You don't!

The subjectName in the derived class will hide the one in the base class, making it inaccessible. You should be getting an error if you don't add the "new" keyword to the subjectName definition in the derived class. Basically, you're not defining a second subjectName in your derived class but redefining (or replacing) subjectName in the base class.

Both methods in your classes, mainsubject and Allsubject, will return the exact same variable value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900