Click here to Skip to main content
15,890,186 members
Home / Discussions / C#
   

C#

 
GeneralRe: Weird crashes Pin
Bekjong31-Oct-06 5:36
Bekjong31-Oct-06 5:36 
Questionreferencing a member of a parent class Pin
grivix31-Oct-06 4:39
grivix31-Oct-06 4:39 
AnswerRe: referencing a member of a parent class Pin
Colin Angus Mackay31-Oct-06 4:56
Colin Angus Mackay31-Oct-06 4:56 
GeneralRe: referencing a member of a parent class Pin
Alaric_31-Oct-06 6:05
professionalAlaric_31-Oct-06 6:05 
GeneralRe: referencing a member of a parent class Pin
Colin Angus Mackay31-Oct-06 6:11
Colin Angus Mackay31-Oct-06 6:11 
GeneralRe: referencing a member of a parent class Pin
grivix31-Oct-06 6:22
grivix31-Oct-06 6:22 
GeneralRe: referencing a member of a parent class Pin
grivix31-Oct-06 6:24
grivix31-Oct-06 6:24 
AnswerRe: referencing a member of a parent class Pin
Colin Angus Mackay31-Oct-06 6:24
Colin Angus Mackay31-Oct-06 6:24 
A bit more of a complete answer to the one I gave before. I've repeated my previous description and annotated the source code. The numbers refer to the description below.

Of course, I may be wrong about what you are trying to achieve as from your description I think you are talking about an association parent/child relationship. But you may, as it has been pointed out, be talking about generalisation (classes deriving from base classes).

public class Program
{
    // (2)
    public static void Main()
    {
        A a = new A();
        a.DoStuff();
    }
}
 
public class A
{
    // (1)
    private B b;
    private C c;
 
    public A()
    {
        // (3)
        this.b = new B(this);
        this.c = new C(this);
    }
 
    public void DoStuff()
    {
        c.mc();
    }
 
    // (6) This allows C to call A without knowing about B 
    // (7) i.e. information hiding, which is a good OO concept
    public void CallMB()
    {
        this.b.mb();
    }
}
 
public class B
{
    // (4)
    private A a;
 
    // (5)
    public B(A a)
    {
        this.a = a;
    }
  
    public void mb()
    {
        Console.WriteLine("I'm in class B");
        Console.Read();
    }
}
 
public class C
{
    // (4)
    private A a;
 
    // (5)
    public C(A a)
    {
        this.a = a;
    }
 
    public void mc()
    {
        // (6) 
        this.a.CallMB();
    }
}


As you have writtent the code, you don't. Instances of class B and C are not fields of class A so there is no parent/child relationship, the reference to B and C will go out of scope at the end of the static Main() method.

(1) Make the b and c fields of class A.

(2) Move the Main() method to its own Program class because it seems out of place in A. (I don't know what A is supposed to do, but I'd say with 99.99% certainty the Main() method does not belong there).

(3) Somewhere in class A you can construct b and c with a reference to this.

(4) Classes B and C have a field of type A.

(5) In the constructors of B and C you can now store the reference to the parent (A) as it was passed in to the constructor.

(6) Now, in the method mc() you can reference A (the parent) which can reference B.

(7) I'll leave the question of encapsulation and information hiding as an exercise to you (because you generally shouldn't be using public fields)


GeneralRe: referencing a member of a parent class Pin
grivix31-Oct-06 6:28
grivix31-Oct-06 6:28 
QuestionNeed XML FAQs Pin
AR Reddy31-Oct-06 4:22
AR Reddy31-Oct-06 4:22 
AnswerRe: Need XML FAQs Pin
led mike31-Oct-06 6:15
led mike31-Oct-06 6:15 
QuestionNewbie: Stupid question about forms & variables... Pin
Phillip Hodges31-Oct-06 4:09
Phillip Hodges31-Oct-06 4:09 
AnswerRe: Newbie: Stupid question about forms & variables... Pin
V.31-Oct-06 4:32
professionalV.31-Oct-06 4:32 
AnswerRe: Newbie: Stupid question about forms & variables... Pin
Guffa31-Oct-06 4:33
Guffa31-Oct-06 4:33 
AnswerRe: Newbie: Stupid question about forms & variables... Pin
Stefan Troschuetz31-Oct-06 4:57
Stefan Troschuetz31-Oct-06 4:57 
GeneralRe: Newbie: Stupid question about forms & variables... Pin
Colin Angus Mackay31-Oct-06 4:58
Colin Angus Mackay31-Oct-06 4:58 
AnswerRe: Newbie: Stupid question about forms & variables... Pin
Colin Angus Mackay31-Oct-06 4:58
Colin Angus Mackay31-Oct-06 4:58 
QuestionHowTo: get version number? Pin
Ariadne31-Oct-06 3:51
Ariadne31-Oct-06 3:51 
AnswerRe: HowTo: get version number? Pin
Russell Jones31-Oct-06 5:04
Russell Jones31-Oct-06 5:04 
AnswerRe: HowTo: get version number? Pin
Ariadne31-Oct-06 5:11
Ariadne31-Oct-06 5:11 
GeneralRe: HowTo: get version number? Pin
ednrgc31-Oct-06 5:39
ednrgc31-Oct-06 5:39 
QuestionPosition of Control on Form Pin
Russell Jones31-Oct-06 2:54
Russell Jones31-Oct-06 2:54 
AnswerRe: Position of Control on Form Pin
Martin#31-Oct-06 3:07
Martin#31-Oct-06 3:07 
GeneralRe: Position of Control on Form Pin
Russell Jones31-Oct-06 4:18
Russell Jones31-Oct-06 4:18 
QuestionFormatting labels Pin
Smon the Vidd31-Oct-06 2:04
Smon the Vidd31-Oct-06 2:04 

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.