PC Review


Reply
Thread Tools Rate Thread

Dictionary of classes

 
 
Holger
Guest
Posts: n/a
 
      30th Sep 2011
I have built some custom classes that are built on the same base
class, but the have all a different method called Calc.
I would like to put them in one dictionary and use the calc method.

Is there a way to add them in one dictionary without using a generic
object and boxing/unboxing them.
I'm looking for something more elegant.

Holger
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      30th Sep 2011
Holger wrote:
> I have built some custom classes that are built on the same base
> class, but the have all a different method called Calc.
> I would like to put them in one dictionary and use the calc method.
>
> Is there a way to add them in one dictionary without using a generic
> object and boxing/unboxing them.
> I'm looking for something more elegant.


Look for the multiton pattern. It's like a singleton, but it adds some
key to the type and for each key no more than one object may exist in
memory.

Technically you can add a static repository in the base class.
public static readonly IList<CalcBase> AllInstances;
With an abstract function
public abstract SomeResult Calc(Parameter);
you will be able to invoke Calc without ugly downcasts.

You may update the list by the constructor of Base.
protected CalcBase()
{ AllInstances.Add(this);
}
Note that mutitons like singletons are objects with infinite lifetime.

If your instances are not equivalent you might want to use a
IDictionary<Key,CalcBase> instead of IList<CalcBase> with some
reasonable key to identify your calculators.

So far so good. But how get your instances build? Whatever you do, you
need to initialize your active CalcBase implementations. Be aware to do
this early enough in your program, to build the repository before it is
accessed.

If all of your implementations of CalcBase are default constructable and
singletons, i.e. there are no two instances in your repository that
share the same type, then you might build your repository in the static
constructor of CalcBase by using reflections in the way that you seek
for all classes that derive from CalcBase. For this to be a reasonable
solution, all classes should be defined in the same assembly and the
same namespace.


Marcel
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      30th Sep 2011
"Holger" <(E-Mail Removed)> wrote in message
news:a9901075-859b-4638-a896-(E-Mail Removed)...

>I have built some custom classes that are built on the same base
> class, but the have all a different method called Calc.
> I would like to put them in one dictionary and use the calc method.
>
> Is there a way to add them in one dictionary without using a generic
> object and boxing/unboxing them.
> I'm looking for something more elegant.


Pardon me if this is a n00B assumption, but couldn't you just put the Calc
method in an interface and then make a Dictionary<string, ICalculator>?


 
Reply With Quote
 
Holger
Guest
Posts: n/a
 
      30th Sep 2011
On Sep 30, 1:40*pm, "Jeff Johnson" <i....@enough.spam> wrote:
> "Holger" <holgeram...@gmail.com> wrote in message
>
> news:a9901075-859b-4638-a896-(E-Mail Removed)...
>
> >I have built some custom classes that are built on the same base
> > class, but the have all a different method called Calc.
> > I would like to put them in one dictionary and use the calc method.

>
> > Is there a way to add them in one dictionary without using a generic
> > object and boxing/unboxing them.
> > I'm looking for something more elegant.

>
> Pardon me if this is a n00B assumption, but couldn't you just put the Calc
> method in an interface and then make a Dictionary<string, ICalculator>?


Marcel/Jeff, thank you for your help.

Jeff, that is what I was looking for. I just didn't know that you
could do that. Excellent!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What should I name my business classes given that LINQ data classes takes all table names? Ronald S. Cook Microsoft C# .NET 2 24th Jun 2008 02:57 AM
How i list or enumerate classes (names of classes in string format) from a certain namespace? please help. Jose Michael Meo R. Barrido Microsoft VB .NET 1 31st Dec 2004 12:13 PM
How i list or enumerate classes (names of classes in string format) from a certain namespace? please help. Jose Michael Meo R. Barrido Microsoft Dot NET 1 31st Dec 2004 12:13 PM
hot to differentiate outer classes and nested classes in late binding Daniel Microsoft Dot NET 0 20th Dec 2004 09:53 PM
Managed __value type enums and classes inside __nogc classes Edward Diener Microsoft VC .NET 0 20th Jan 2004 04:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:08 AM.