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): CODEclass 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 |
|