Virtual static member

R

Ralfeus

Hi all.
I have a base class and several classes inherited from this base
class. I need to create a instances counter for each class. I thought
about something like creation of virtual static member in the base
class and increasing it in each derived class constructor. But it's
not possible to create a virtual data member. Which alternative way
can be? Or only way is to create a static data member in each class?
Thank you
 
J

Jon Skeet [C# MVP]

I have a base class and several classes inherited from this base
class. I need to create a instances counter for each class. I thought
about something like creation of virtual static member in the base
class and increasing it in each derived class constructor. But it's
not possible to create a virtual data member. Which alternative way
can be? Or only way is to create a static data member in each class?

You either need a static variable in each class, or a base static
variable which is a Dictionary<Type,int> which keeps a count for each
type. Then in the base class constructor just do (with appropriate
locking):

int count;
counter.TryGetValue(GetType(), out count);
count++;
counter[GetType()] = count;

Jon
 
R

Ralfeus

Oh, that's exactly what I need :)
Thank you very much :)

I have a base class and several classes inherited from this base
class. I need to create a instances counter for each class. I thought
about something like creation of virtual static member in the base
class and increasing it in each derived class constructor. But it's
not possible to create a virtual data member. Which alternative way
can be? Or only way is to create a static data member in each class?

You either need a static variable in each class, or a base static
variable which is a Dictionary<Type,int> which keeps a count for each
type. Then in the base class constructor just do (with appropriate
locking):

int count;
counter.TryGetValue(GetType(), out count);
count++;
counter[GetType()] = count;

Jon
 
P

Peter Duniho

I'm sure you wouldn't do the GetType() twice. ;-)

Why not?

I think there's a chance that the compiler can optimize that anyway
(assuming the method resolves to some simple inspection of the data
structure...I don't really know how it works though), but even if it
doesn't the code is simpler without caching the value. Why bother caching
it before you even know there's a problem calling the method twice?
 
G

Guest

Peter Duniho said:
Why not?

I think there's a chance that the compiler can optimize that anyway
(assuming the method resolves to some simple inspection of the data
structure...I don't really know how it works though), but even if it
doesn't the code is simpler without caching the value. Why bother caching
it before you even know there's a problem calling the method twice?

I would not assume that there is a problem occuring only for this multiple
call, but I would personaly see it as bad style.
But that's maybe a metter of taste and not programming skill. :)

All the best,

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top