views:

38

answers:

1

Hello fellows, I have a need where I have to add some new fields to an existing class along with all its existing fields/attributes.

So whenever my derived class is filled by DAL, I will be filling all fields of base class as well. Currently, I am doing it like this but not sure this is the right way ? Please give me an example. Also I am not sure whether the base class object will be a new one each time a derived class is initialized ?

public class Employee
    {
        private int _id;
        private int _name;
        public int ID
        { 
            set { _id=value;}
            get { return _id;}
        }
        public int Name
        { 
            set { _name=value;}
            get { return _name;}
        }
        protected void SetName ()
        {
            _name=value;
        }
        protected void SetID()
        {
            _id=value;
        }
    }
public class EmployeeWithDepartmentName:Employee
        {
           private string _deptName;
           public string DeptName 
            {
                set { _deptName=value; }
            }
            public setBaseEmpName()
            {
               base.SetName();
            }
        public setBaseID()
            {
               base.SetID();
            }
        }
+1  A: 

Everything in a base class can automagically be accessed from derived classes without doiing anything, just use the property/method name directly.

public class MyBase
{
    public string UserName {get;set;}
}

public class MyClass : MyBase
{
  public void DoSomething()
  {
     Console.WriteLine("UserName: {0}", UserName);
     UserName = "Anders";
  }
}

You can also do this:

MyClass myClass = new MyClass();
myClass.UserName = "Rune";

Protected means that only derived classes can access the property/method. Public means that everyone can access the properties/methods.

Also I am not sure whether the base class object will be a new one each time a derived class is initialized ?

It's not two objects, it's one object created from two different classes (that's how inheritance works).

Read this article about inheritance: http://www.csharp-station.com/Tutorials/lesson08.aspx

jgauffin
sorry I read what you wrote. How would I set the value of UserName in MyBase through derived class? Would that mean I have to create Duplicate get set methods inside MyClass as well ?
Popo
Sounds to have totally missunderstood the object-oriented-programming concept. When MyClass inherits MyBase it will automatically get everything from MyBase into MyClass. You do not have to do anything in MyClass to access the stuff in MyBase. Just pretend that everything in MyBase was declared in MyClass.
jgauffin
Sorry jgauffin, my bad. What I don't understand is how to set value to MyBase fields in MyClass ? Do you mean like this inside MyClass public void SetUserName () { UserName=Value; }
Popo
Check updated code where I set username in DoSomething.
jgauffin
I guess a public method SetBase inside MyClass which would take input parameters from DAL would do the job. Thank you jgauffin =)
Popo
Added another example. Don't know what you need SetBase for?
jgauffin
Thanks, you are right. I won't need any method.
Popo