Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set value in base class, access from derived

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
I have a base "Family" class. I will derive from this class for my "Father", "Mother" and "Child" class.

I want to set the last name only once in the base class. This is my code
Code:
class Family
{
	private string last_name;
	public virtual string LastName { get { return last_name; } set { last_name = value; } }
}

class Father : Family{}
class Mother : Family{}
class Child : Family{}

Here is how i invoke

Code:
Family f = new Family();
f.LastName = "Smith";
Father fa = new Father();
[green]// When I do this, I get back a blank value[/green]
Console.Write(fa.LastName);

My goal is that fa.LastName will just take the Family.LastName value I just set, but that does not happen. I have to do this

Code:
fa.LastName = f.LastName;

to get my results. I was hoping I would not have to do this for all my derived classes. Now if I set last name in my derived class , then this will work

Code:
class Family
{
private string last_name = "Smith";
}

but obviously I do not want to do that.

I have searched and I could not find anything that talks about this type of implementation.

Is the problem that when Father is called, a new implementation of Family is created, therefore LastName is ""?

Thanks,
RalphTrent
 
The problem is that you are creating an object of type Family and setting its property. The object of type Father you are creating is totally separate from the Family object instance you created earlier.

Ideally, when you inherit a class, you are saying "child class is-a base class," or, in this case, "father is-a family." I'm not sure of the larger picture as to what you're trying to accomplish, so I'll just focus on the inheritance part. Depending on what you're trying to do, there may be better ways to do it.

You can provide an overloaded constructor in which you provide to the object what the family will be. Check this example out (done in a console application):

Code:
class Program
    {
        static void Main(string[] args)
        {
            Family f = new Family();
            f.LastName = "Smith";
            Father fa = new Father();
            //provide the class with the family you want to use
            Father fa2 = new Father(f);
            Mother mom = new Mother(f);
            
            // When I do this, I get back a blank value
            Console.WriteLine(string.Format("father's last name = {0}", fa.LastName));
            // When I do this, I get back the last name
            Console.WriteLine(string.Format("new father's last name = {0}", fa2.LastName));
            Console.WriteLine(string.Format("mother's last name = {0}", mom.LastName));
            Console.ReadLine();

        }
    }

    class Family
    {
        private string last_name;
        public string LastName { get { return last_name; } set { last_name = value; } }
    }

    class Father : Family 
    {
        //base ctor
        public Father() { }

        //overloaded ctor
        public Father(Family incomingFamily)
        {
            base.LastName = incomingFamily.LastName;
        }
    }
    class Mother : Family 
    {
        public Mother() { }

        public Mother(Family incomingFamily)
        {
            base.LastName = incomingFamily.LastName;
        }
    }
    class Child : Family { }

here's a reference web page : Class Inheritance.

I hope this helps you out.

-Mark



 
Thanks Mark for the reply. this example here is just "practice" code to better understand inheritance. I have since renamed Family to FamilyMember because like you said, I am implying the Father is Family when he really is a FamilyMember.

I thought the same thing about having to have another constructor bu I was not sure if there was a more valid way to doing it.

Thanks for replying and confirming my thoughts.

RalphTrent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top