Click here to Skip to main content
16,014,568 members
Home / Discussions / C#
   

C#

 
GeneralRe: omg, what a cry baby! Pin
Guffa27-Jul-05 23:59
Guffa27-Jul-05 23:59 
QuestionHow can I.. Pin
KORCARI27-Jul-05 9:54
KORCARI27-Jul-05 9:54 
AnswerRe: How can I.. Pin
Alomgir Miah27-Jul-05 10:32
Alomgir Miah27-Jul-05 10:32 
GeneralRegular Expression Pin
eggie527-Jul-05 9:47
eggie527-Jul-05 9:47 
GeneralRe: Regular Expression Pin
Dan Neely27-Jul-05 10:38
Dan Neely27-Jul-05 10:38 
GeneralRe: Regular Expression Pin
Werdna27-Jul-05 11:13
Werdna27-Jul-05 11:13 
GeneralInheritance instances Pin
zapap27-Jul-05 9:31
zapap27-Jul-05 9:31 
GeneralRe: Inheritance instances Pin
Patric_J28-Jul-05 5:10
Patric_J28-Jul-05 5:10 
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
GeneralRe: Inheritance instances Pin
zapap28-Jul-05 8:56
zapap28-Jul-05 8:56 
Generalcreating new desktop Pin
27-Jul-05 9:08
suss27-Jul-05 9:08 
GeneralRe: creating new desktop Pin
Sam 200627-Jul-05 14:20
Sam 200627-Jul-05 14:20 
Generalpass value from modal form Pin
Newbie_Toy27-Jul-05 8:56
Newbie_Toy27-Jul-05 8:56 
GeneralRe: pass value from modal form Pin
Libor Tinka27-Jul-05 9:11
Libor Tinka27-Jul-05 9:11 
GeneralColor Creation Pin
MarkMokris27-Jul-05 8:54
MarkMokris27-Jul-05 8:54 
GeneralRe: Color Creation Pin
Peter Vertes27-Jul-05 9:11
Peter Vertes27-Jul-05 9:11 
GeneralRe: Color Creation Pin
Libor Tinka27-Jul-05 9:15
Libor Tinka27-Jul-05 9:15 
GeneralScrolling with bitmap Pin
Yigal Agam27-Jul-05 8:45
Yigal Agam27-Jul-05 8:45 
GeneralRe: Scrolling with bitmap Pin
Libor Tinka27-Jul-05 9:19
Libor Tinka27-Jul-05 9:19 
GeneralRe: Scrolling with bitmap Pin
Yigal Agam27-Jul-05 9:28
Yigal Agam27-Jul-05 9:28 
GeneralRe: Scrolling with bitmap Pin
Libor Tinka27-Jul-05 9:53
Libor Tinka27-Jul-05 9:53 
QuestionGiant memory leaks or what? Pin
Libor Tinka27-Jul-05 8:37
Libor Tinka27-Jul-05 8:37 
AnswerRe: Giant memory leaks or what? Pin
Dave Kreskowiak27-Jul-05 8:41
mveDave Kreskowiak27-Jul-05 8:41 
GeneralRe: Giant memory leaks or what? Pin
Libor Tinka27-Jul-05 9:00
Libor Tinka27-Jul-05 9:00 
GeneralRe: Giant memory leaks or what? Pin
Libor Tinka27-Jul-05 9:04
Libor Tinka27-Jul-05 9:04 
AnswerRe: Giant memory leaks or what? Pin
Judah Gabriel Himango27-Jul-05 9:11
sponsorJudah Gabriel Himango27-Jul-05 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.