Inheritance instances
-
Base ------------------------- | public int iNo; | | | ------------------------- Class1 : Base Class2 : Base ---------------------------- -------------------------- | Class2 c2; | | private int iMyNo; | | void Class1() | | public void SetMyNo()| | { iNo = 10; | | { iMyNo = iNo; | | c2 = new Class2(); | | } | | c2.SetMyNo(); | -------------------------- | } | ---------------------------- This is my srtucture, I begin with Class1, which then init Base, and then sets iNo = 10. Next I create a new instance of Class2, which in turn again init Base. Now when I call c2.SetMyNo, it sets is to nothing, since there are two instances of Base. So how do I inherit Class2 from the same instance of Base, the super class of Class1? There's someone in my head but it's not me - Pink Floyd gunigugu
-
Base ------------------------- | public int iNo; | | | ------------------------- Class1 : Base Class2 : Base ---------------------------- -------------------------- | Class2 c2; | | private int iMyNo; | | void Class1() | | public void SetMyNo()| | { iNo = 10; | | { iMyNo = iNo; | | c2 = new Class2(); | | } | | c2.SetMyNo(); | -------------------------- | } | ---------------------------- This is my srtucture, I begin with Class1, which then init Base, and then sets iNo = 10. Next I create a new instance of Class2, which in turn again init Base. Now when I call c2.SetMyNo, it sets is to nothing, since there are two instances of Base. So how do I inherit Class2 from the same instance of Base, the super class of Class1? There's someone in my head but it's not me - Pink Floyd gunigugu
Your question is not very clear but if I understand it you have to two classes Class1 and Class2 both inheriting from Base. Then you create two objects, c1 from Class1 and c2 from Class2. Both objects will have its own copy of the iNo, inhereted from Base. They are not the same iNo int variable just because they are defined in the same class, instead they refer to seperate variables in memory. You don't want to use inheritance in this case, you want to use aggregation
class Base
{
public int iNo;
}class Class1
{
public Base myBase;
}class Class2
{
public Base myBase;
}class Test
{
[STAThread]
static void Main()
{
Base b = new Base();
b.iNo = 10;Class1 c1 = new Class1(); c1.myBase = b; Class2 c2 = new Class2(); c2.myBase = b; Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo); Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo); c2.myBase.iNo = 5; Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo); Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo); }
}
This will result in the following outpu c1.myBase.iNo = 10 c2.myBase.iNo = 10 c1.myBase.iNo = 5 c2.myBase.iNo = 5 /Patric My C# blog: C# Coach
-
Your question is not very clear but if I understand it you have to two classes Class1 and Class2 both inheriting from Base. Then you create two objects, c1 from Class1 and c2 from Class2. Both objects will have its own copy of the iNo, inhereted from Base. They are not the same iNo int variable just because they are defined in the same class, instead they refer to seperate variables in memory. You don't want to use inheritance in this case, you want to use aggregation
class Base
{
public int iNo;
}class Class1
{
public Base myBase;
}class Class2
{
public Base myBase;
}class Test
{
[STAThread]
static void Main()
{
Base b = new Base();
b.iNo = 10;Class1 c1 = new Class1(); c1.myBase = b; Class2 c2 = new Class2(); c2.myBase = b; Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo); Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo); c2.myBase.iNo = 5; Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo); Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo); }
}
This will result in the following outpu c1.myBase.iNo = 10 c2.myBase.iNo = 10 c1.myBase.iNo = 5 c2.myBase.iNo = 5 /Patric My C# blog: C# Coach