Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0 architecture , +
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 26-Apr-13 0:55am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
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;
    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.
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[^]
  Permalink  
v2
Comments
eg_Anubhava at 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 at 26-Apr-13 6:38am
   
See updated solution 2
eg_Anubhava at 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 at 30-Apr-13 4:15am
   
My first example showed inheritance. You might find this article useful Introduction to inheritance, polymorphism in C#[^]
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
eg_Anubhava at 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);
patel_vijay at 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)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100