Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - Created object to be able to access members of the object that created it

It's not inheritence, it's not nested classes, I dont know what it is and thats what brings me here.

I am writing a program and this is the high-level functionality:

Main() Create an array of 'A()' type objects 'A()'s constructor creates 20 'B()'objects 'B()'s costructor creates 4 'C()' objects

Now 'B()' wants to use some of the public members of 'A()' and 'C()' wants to use some of the public members of 'A()' and 'B()'

In other words, A is Control Record, B is a list of 20 commands and each command has upto 4 conditions. Now C() should be able to validate the conditions, B() should be able to execute the command based on C()'s validation and A() should somehow know when all B's are done

So the idea is that certain objects get spawned off and they should be able to use members from the object who created them. I could pass information in the constructor but that gets tedious. There's gotta be a nice design to be able to do the above.

Can anyone help??

like image 425
Mandeep Ahuja Avatar asked Feb 12 '11 02:02

Mandeep Ahuja


1 Answers

I could pass information in the constructor but that gets tedious.

This is one very valid means of having this behavior, and a very common pattern. The tediousness is minimal, especially if you design your code cleanly. It basically changes one line of code from B bInstance = new B(); to B bInstance = new B(this);, etc.

A() should somehow know when all B's are done

I would recommend using events to handle this. B could include a "Completed" event to which each A subscribes. This will allow it to know exactly when things have completed.

like image 130
Reed Copsey Avatar answered Oct 25 '22 02:10

Reed Copsey