Click here to Skip to main content
15,746,534 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi Experts,

I have a class OLDCar in which have some properties and methods and i have not access to change and create methods here.
So i need to create a NEWCAR Class there base class is OLDCar. then how can i set the value of base class in clild class.

class OLDCAR

class NewCar :OLDCAR
public isAirbag {get;set;}

Old car having some values then how can i pass the value in new car object..

With apologies to patel_vijay for expanding on his solution ...
Or you can acess the base class properties in deried class using this or base keyword.
here is an example...
Say you have something set up like
public class OldCar
    protected int aHiddenProp;
    public int AHiddenProp
      get { return aHiddenProp; }
      set { aHiddenProp = value; }
public class NewCar : OldCar
    // This class adds some stuff to OldCar e.g.
    private bool _isAirBag;
    public bool IsAirBag
        get { return _isAirBag; }
        set { _isAirBag = value; }
    public string showValues()
        base.AHiddenProp = 4; // change the value in the base class
        return _isAirBag.ToString() + ' ' + base.AHiddenProp.ToString();

then anything creating an instance of the NewCar class can use the underlying base class properties as well...
private void button1_Click(object sender, EventArgs e)
    NewCar nc = new NewCar();
    nc.AHiddenProp = 3;         // this is actually in the base class
    nc.IsAirBag = true;

The messageBox will display "True 4" because I've adjusted the value in the showValues function

If you (really) want to pass an instance of OldCar to NewCar then you will have to write an appropriate constructor for NewCar e.g.
public NewCar() { }
public NewCar(OldCar oc)
    this.AHiddenProp = oc.AHiddenProp;
    // copy all of the values passed in oc to this instance

but the better route is just to use NewCar in the first place.

See also[^]
Share this answer
[no name] 26-Apr-13 6:30am    
First Thanks for the Example writing ...But you are not passing the OLDCAR Value to new Car Object.

OldCar oc = new OldCar();
oc.AHiddenProp = 200;

///I need to place the old car object to new car object by using constructor

NewCar nc = new NewCar(oc); ??????????
nc.IsAirBag = true;
CHill60 26-Apr-13 6:38am    
See updated solution 2
[no name] 29-Apr-13 12:00pm    
yes you have done this by using constructor but my question is that can we do this by using inheritance.
CHill60 30-Apr-13 4:15am    
My first example showed inheritance. You might find this article useful Introduction to inheritance, polymorphism in C#[^]
if you OLDCLASS class accepting parameters in contructor then you can pass values as below.
public NewCar(int param1)
            : base(param1)
        { }

Or you can acess the base class properties in deried class using this or base keyword.
Share this answer
[no name] 26-Apr-13 6:09am    
How can you send the object of base class to the derived class object.
please have a look for that with example.

OldCar oldcar = oldccarbal.getoldcar();
NewCar newcr = new NewCar(oldcar);
Console.WriteLine (newcr.Speed);
vijay__p 26-Apr-13 6:18am    
If you create an overloaded constructor/method in derived class which accept BaseClass object as paramer then you can
You can access base class properties in derived class and initialize them if required.

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