Reputation: 247
i have 3 classes: class PassengerDetails which is inherited by class FlightDetails which in turn is inherited by class Details. Both, PassengerDetails and FlightDetails have a method Accept which accepts some parameters and assigns it to local variables (declared as protected) of that class. What i need to do is by using a method Show in Details i want to print the values of those local variables of class PassengerDetails and FlightDetails. here are few parts of my entire code:
class PassengerDetails
{
protected string strFirstName;
protected int iAge;
public void Accept(string FirstName, int Age)
{
strFirstName = FirstName;
iAge = Age;
}
}
class FlightDetails:PassengerDetails
{
protected DateTime dt;
protected int iNumPass;
public void Accept_1(DateTime date,int NumPass)
{
dt = date;
iNumPass = NumPass;
}
}
class Details : FlightDetails
{
public void Show(Label lbl)
{
lbl.Text= "" + strFirstName +"\n"+ iAge +"\n"+ dt.ToString() + "\n"+ iNumPass;
}
private void btnSubmit_Click(object sender, EventArgs e)
{
PassengerDetails pass = new PassengerDetails();
pass.Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
}
private void btn2Submit_Click(object sender, EventArgs e)
{
FlightDetails fgt = new FlightDetails();
fgt.Accept_1(Convert.ToDateTime(dtDep.Text),Convert.ToInt32(txtNPass.Text));
Details det = new Details();
det.Show(lblShow);
when i do this, all i get is default value of those local variables. can someone plzz help???
Upvotes: 0
Views: 3198
Reputation: 3467
You can access to values of FlightDetails by
base.fieldname
and for access to values of PassengerDetails, add a property to FlightDetails :
public type property
{
get {return base.fieldname;}
}
and in Details use property.
Upvotes: 1
Reputation: 72840
You are calling th Accept
and Accept_1
methods on separate instances of the classes in question. Your Details
class exposes these directly by inheritance, so you should replace this:
PassengerDetails pass = new PassengerDetails();
pass.Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
with this:
this.Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
or, more simply (arguably):
Accept(txtFName.Text,Convert.ToInt32(txtAge.Text));
The point is, your instance of Details
is already also an instance of FlightDetails
and PassengerDetails
, so you don't need to create separate ones. However, looking at the class names, I strongly suspect that you might be misusing inheritance, and that a composition approach may be more appropriate, with Details
exposing properties of types FlightDetails
and PassengerDetails
.
Upvotes: 2