×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

set value in base class, access from derived

set value in base class, access from derived

set value in base class, access from derived

(OP)
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();
// When I do this, I get back a blank value
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

RE: set value in base class, access from derived

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



 

RE: set value in base class, access from derived

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close