473,804 Members | 3,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1078
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******** *****@TK2MSFTNG P11.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******** ********@TK2MSF TNGP09.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******** *****@TK2MSFTNG P11.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.co m>
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
2963
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 class Foo, which I can't change. Foo holds a reference to a collection of Bar objects and/or objects derived from Bar (which I can't change). My FooExtended class needs to keep extra information for each Bar in that collection. If I could change...
3
1092
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 why do they do this if the user object that was created existed for the life of the object why u would need a user collection? I would think u could just call the user object and get your info from that just as easily then going the extra step to...
19
4922
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 exception of custom Collection Classes. Background: I'm developing an A2K .mdb to be deployed as an .mde at my current job-site. It has several custom controls which utilize custom classes to wrap built-in controls, and add additional functionality....
26
5378
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.