473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Nov 16 '05 #1
3 1068
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

Nov 16 '05 #2
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

"Tom Dacon" <To**@t-dacons.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Nov 16 '05 #3
Jim Frazer <ji*@nospam.net> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: User N | last post by:
Perhaps this is a stupid question, but here goes anyway. Given an instance of an arbitrary class, is it possible to change its base class at runtime? Here's my problem... I need to extend...
3
by: JJ | last post by:
Hi, I noticed in a sample app source code that the app made use of a class for example a user class and then had the user objects that got created stuffed into a user collection. I was wondering...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.