Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
A newbie question, I got the following C# code where there is a inner-class B that need to call a method on class A.
Please advise how.
class A { void MethodA() { } class B { void MethodB { // Now method B need to call Method A above } } }
Nested types don't automatically have an instance of their parent type; you would need something like:
class B { private readonly A a; public B(A a) { this.a = a; } void MethodB() { a.MethodA(); } }
and instead of new B(), you would use new B(this).
new B()
new B(this)
Add a comment
Make object of A inside class B. And use it inside MethodB.
class B { private A objectA; void MethodB() { objectA.MethodA(); } }
Initialize objectA before using. You can do this in constructor.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.