Consider using the 'class factory' design pattern to manage this. The
secondary classes do not expose any constructors whatsoever to the
application. The only way to get an instance of one of the secondary classes
is through a method on the primary class which internally creates an
instance of a secondary class, using a constructor to which it passes a
reference to itself. The instance of the secondary class stores the
reference in its instance memory and thus has access to the primary class
instance. The primary class's factory method returns the newly-created
instance to the application. This makes it impossible for instances of the
secondary class to be created without the crucial access to the instance of
the primary class.
This works best if the primary and secondary classes are in a separate
assembly, so that the constructors for the secondary classes can be marked
'internal' and thus invisible to the application. If they're in the same
project as the app code they can't be completely hidden from the app code.
Another approach, altogether different from the above, can be used if there
is exactly one and only one instance of the primary class, forever and ever
amen. The primary class, in its constructor, can store a reference to itself
in a public static property of the primary class. Any secondary class can
refer to that static property and get the reference, without having to store
it within its own instance memory. Think carefully about this before using
this technique, because future unanticipated developments of the application
may break the uniqueness of the primary class instance, and then the code in
the secondary classes would have to be changed to something like the first
approach.
HTH,
Tom Dacon
Dacon Software Consultingh
"Jim Frazer" <ji*@nospam.net> wrote in message
news:uV*************@TK2MSFTNGP11.phx.gbl...
Hi,
I'm new to C#, but experienced in C++. I'm converting an ActiveX object
to C#. There is a single instance of a "primary" class which creates two
collections of other "secondary" classes. Each of these other classes
needs to access the information in the primary class - items like a mutex, an
IntPtr to a shared memory map, etc. In C++, I'd probably pass a pointer
to the primary class to each of the secondary classes. What would be the
most efficient method to accomplish this in C#?
Something like:
SecClass mySec = new SecClass(this);
seems like it would be inefficient in use of memory, and the secondary
classes wouldn't see changes in any of the members of the primary class.
TIA,
Jim Frazer