Click here to Skip to main content
15,867,568 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..
Posted

With apologies to patel_vijay for expanding on his solution ...
Quote:
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
C#
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...
C#
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;
    MessageBox.Show(nc.showValues());
}

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

[Edit]
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.
C#
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 http://msdn.microsoft.com/en-us/library/ms173149.aspx[^]
 
Share this answer
 
v2
Comments
[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;
MessageBox.Show(nc.showValues());
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.
C#
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
 
Comments
[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