Class X Creates Collection of Instances of Class Y. Y's need info from X

  • Thread starter Thread starter Jim Frazer
  • Start date Start date
J

Jim Frazer

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
 
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
 
Tom,

There is one, and only one, instance of the primary class, by design. I
think the latter approach might be best. I'll give that a shot.

Thanks,

Jim
 
Jim Frazer said:
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.

No - don't forget that "this" is just a reference. It would be exactly
the same as in C++. You'd just waste the size of a reference in each
instance of the secondary class.

If there's only one instance of the primary class, you could consider
using the singleton pattern:

http://www.pobox.com/~skeet/csharp/singleton.html
 
Back
Top