2

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 } } } 
0

2 Answers 2

4

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.