Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Over the past year I have found your site to be EXCELLENT. Never have I been able to find so many answers to such vast problems and it is an excellent service..."

Geography

Where in the world do Tek-Tips members come from?

set value in base class, access from derived

ralphtrent (Programmer)
11 Apr 12 13:04
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
MarkXaml (Programmer)
11 Apr 12 15:38
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



 
ralphtrent (Programmer)
11 Apr 12 16:14
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

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!

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