Sign in to follow this  

[.net] Question about classes with static members

This topic is 4066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a class that I do not want to make an instance of. Its member functions and variabes are suited to be static. And yet, I somehow want to force its constructor to be executed (currently, it appears that the constructor never runs). The class I'm referring to is a ColorCycle. It defines a billion brushes, and a member function called getNextBrush(). Every time getNextBrush() is called, a new color brush is returned. The purpose is that I want every shape that a user draws on the screen to be drawn with a different color. The list of brushes is defined in the constructor of ColorCycle. However, since I never instantiate ColorCycle, it never executes (since the list of brushes is empty when the program is run). Like I said, I don't particularly have a will to create an instance of ColorCycle since its purpose is "just static in nature". Is there no other way?

Share this post


Link to post
Share on other sites
Your class could just as well be a namespace at this point, so make it a regular function and call it from main(). If that doesn't suit you, yes, you will have to create an instance. Nothing wrong with that.

Share this post


Link to post
Share on other sites
Can you initialise the list of brushes in a static constructor?

Failing that - you could use lazy initialisation: In getNextBrush() (or a function/property that it calls to retrieve the list of brushes), you can check whether the static brush list has been initialised (is null), and if not, then initialise it.

Cheers,
Gary

Share this post


Link to post
Share on other sites
are you using .net 2.0? You can just make a static constructor which takes no parameters in this case. I don't know if earlier versions of .net had that as well, but it's worth looking into.

Share this post


Link to post
Share on other sites
Quote:
Original post by kanato
are you using .net 2.0? You can just make a static constructor which takes no parameters in this case. I don't know if earlier versions of .net had that as well, but it's worth looking into.
public static class MyStaticClass
{
}
... fails in .NET 1.1, but works in .NET 2.0.

The static constructor is called the first time you make use of ColorCycle:
public static class ColorCycle
{
static ColorCycle()
{
for(int i = 0; i < 1000000000; i++)
m_Brushes[i] = new Brush(i);
}

// Make ONE BILLION brushes!
private static Brush[1000000000] m_Brushes = new Brush[1000000000];

public static Brush getNextBrush()
{
if(m_Counter > m_Brushes.Length)
m_Counter = 0;
return m_Brushes[m_Counter++];
}
}



The first time you call getNextBrush, the static contructor will be called. Personally, I really dislike this solution and would prefer something like this, if ColorCycle has to be static:


public static ColorCycle
{
private static int m_Counter;
public static Brush getNextBrush()
{
if(m_Counter > 1000000000)
m_Counter = 0;
return new Brush(m_Counter++);
}
}




This way you're getting away from initializing one billion Brushes during runtime. The creation of brushes also turns to the Brush class, rather then ColorCycle. I would actually ditch the static class idea all together....

Share this post


Link to post
Share on other sites
once very important thing about static constructors in .NET ... they do NOT run at load time like you might expect if you come from a C++ background. They run prior to the first access of any static or non-static member of the class. An important consequence you need to be aware of is - if noone uses the class, the static constructor will never be run at all.

This can be a very good thing, but you must make sure and take it into account in your code.

For instance in C++ I used to use the constructor of static instances as a time to register plugins for open-ended extensible systems ... so if the classes get compiled into the app, they will register themselves.

In .NET that cannot be used quite the same way, because it will not execute the static constructor unless the class is used, and if it is a plugin system, the class won't be used - kinda a catch 22. My answer of course was to use the other .NET idiom of adding an attribute to the class (specific to the system it plugs in to) and using reflection at runtime to find all classes which have the attribute and call their static register plugin function.

Share this post


Link to post
Share on other sites

This topic is 4066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this