TLD
TLD

Reputation: 8135

Call reference of an object as a parameter in another method

For example:

A a = new A(b);
B b = new B(a);

How can I use b as a parameter in A constructor when it's not existed, how can the compiler will know that b will be created later?

Thank you.

Upvotes: 2

Views: 82

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283614

Something along these lines:

class StrongReference<T> { public T Target; }
var rb = new StrongReference<B>;
A a = new A(rb);
rb.Target = new B(a);

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66389

You can't.

In A class add public method like Init(B b) and call it after you create the instance.

Upvotes: 7

Related Questions