Nested Class Function Call - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Nested Class Function Call

manish_dalalmanish_dalal Posts: 2Member
Hello all there,

I am new to C# programming. I am creating a nested class structure in which i need to call a method of outer class from an inner class. Hew, see the code below to get a better understanding of my problem.

//ignore the underscores(_), they are just for indentation of the code.
class A
{
___void Method1()
___{
______.
______.
______.
___}
___class B
___{
______void Method1()
______{
_________.
_________.
_________.
______}
______class C
______{
//How to call the Method1() of class A and class B here.
______}
___}
}

So, the problem is both the outer classes have the same method named Method1(). Is it possible to specifically call the Method1() of one of the outer classes (A or B). I want to call one of the method from the inner class C. How could I call the Method1() of class A, and how that of Class B.
Also tell me if I give only the function call Method1() without any class object name, or so, which one of the Method1() will get called.

Please help....

Thanx in advance

Manish

Comments

  • BaldusariusBaldusarius Posts: 162Member
    This seems like a somewhat dubious use of nested classes. It is usually a bit of a stretch to justify the use of a single level of nesting, much less two or more levels. Nevertheless...

    Nested classes are about scoping. Using them does not set aside any of the rules of OOP or change how C# works, so to call Method1 (as defined in A or B) from C, you need a reference to the class that defines it. As for your last question, any method called by method name alone is prepended by the 'this' pointer, so the method defined in the calling class would run.

    [code]// in class C
    A a = new A();
    B b = new B(); // You're inside the scope, so you don't need to say A.B
    a.Method1(); // A's Method1 gets called.
    b.Method1(); // B's Method1 gets called.
    Method1(); // C's Method1 gets called.[/code]

    Since methods in C# that do not have explicit scope qualifiers are assumed to be private, B can't call C.Method1 and A can't call B.Method1 or C.Method1 unless the method signatures are changed to public or internal.


    : Hello all there,
    :
    : I am new to C# programming. I am creating a nested class structure in which i need to call a method of outer class from an inner class. Hew, see the code below to get a better understanding of my problem.
    :
    : //ignore the underscores(_), they are just for indentation of the code.
    : class A
    : {
    : ___void Method1()
    : ___{
    : ______.
    : ______.
    : ______.
    : ___}
    : ___class B
    : ___{
    : ______void Method1()
    : ______{
    : _________.
    : _________.
    : _________.
    : ______}
    : ______class C
    : ______{
    : //How to call the Method1() of class A and class B here.
    : ______}
    : ___}
    : }
    :
    : So, the problem is both the outer classes have the same method named Method1(). Is it possible to specifically call the Method1() of one of the outer classes (A or B). I want to call one of the method from the inner class C. How could I call the Method1() of class A, and how that of Class B.
    : Also tell me if I give only the function call Method1() without any class object name, or so, which one of the Method1() will get called.
    :
    : Please help....
    :
    : Thanx in advance
    :
    : Manish
    :

Sign In or Register to comment.