tags:

views:

96

answers:

3

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???

+2  A: 

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.

David M
the thing is that all the above 3 classes are within the main form and hence using this keyword gives the error 'WindowsApplication1.frmMain' does not contain a definition for 'Accept_1'
Pratik Gandhi
Your code shows these methods defined within the `Details` class - I suspect you've lost a bracket somewhere. In that case, you only need to create a single `Details` instance, and call the two `Accept` methods on that instance, not to create instances of each parent class. But I still think you should be looking at composition not inheritance.
David M
Pratik Gandhi
Great - hope the explanation made sense as well.
David M
A: 

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.

Navid Farhadi
A: 

can i use parametrized constructor's instead of methods Accept and Accept_1? if yes, then may i know how to proceed with it?

Pratik Gandhi
please **update** your question by editing it instead of answering yourself with additional info...
marc_s