By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

static classes

P: n/a
Hi all,
I was wondering: Why isn't it possible to write "static classes". By
this I mean a class that will _never_ get instantiated, but will always
work on and with other static members of its class.

The class would in effect behave like a "classic" win32 dll and its
exported functions, only this time referred by
<classname>.<ExportedFunction>

I know (how) I can achieve the same results with a singleton, but why
not just mark the entire class as "static" instead of marking every
single member as static?

I'm quite new to .NET (digging, gropin and mutilating around for about 8
months now), so, my questions are:
- Is it possible?
- If not, are there better solutions to my "problem"?
- Would it any how be a nice language/platform addition?

Thanks in advance,

Willem





Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Try creating your class with all static members and a private constructor.
No instanciation anymore. Just like the MessageBox class
(http://msdn.microsoft.com/library/de...-us/cpref/html
/frlrfsystemwindowsformsmessageboxclasstopic.asp)

Telmo Sampaio

"Willem van Rumpt" <no***********@no.thelandslide.spam.com> wrote in message
news:#p**************@TK2MSFTNGP11.phx.gbl...
Hi all,
I was wondering: Why isn't it possible to write "static classes". By
this I mean a class that will _never_ get instantiated, but will always
work on and with other static members of its class.

The class would in effect behave like a "classic" win32 dll and its
exported functions, only this time referred by
<classname>.<ExportedFunction>

I know (how) I can achieve the same results with a singleton, but why
not just mark the entire class as "static" instead of marking every
single member as static?

I'm quite new to .NET (digging, gropin and mutilating around for about 8
months now), so, my questions are:
- Is it possible?
- If not, are there better solutions to my "problem"?
- Would it any how be a nice language/platform addition?

Thanks in advance,

Willem




Nov 16 '05 #2

P: n/a
Willem,
I understand that C# 2.0 (part of VS.NET 2005 aka Whidbey) will have static
classes.

http://lab.msdn.microsoft.com/vs2005/

http://msdn.microsoft.com/vcsharp/de...0/default.aspx

Hope this helps
Jay

"Willem van Rumpt" <no***********@no.thelandslide.spam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi all,
I was wondering: Why isn't it possible to write "static classes". By
this I mean a class that will _never_ get instantiated, but will always
work on and with other static members of its class.

The class would in effect behave like a "classic" win32 dll and its
exported functions, only this time referred by
<classname>.<ExportedFunction>

I know (how) I can achieve the same results with a singleton, but why
not just mark the entire class as "static" instead of marking every
single member as static?

I'm quite new to .NET (digging, gropin and mutilating around for about 8
months now), so, my questions are:
- Is it possible?
- If not, are there better solutions to my "problem"?
- Would it any how be a nice language/platform addition?

Thanks in advance,

Willem




Nov 16 '05 #3

P: n/a

"Willem van Rumpt" <no***********@no.thelandslide.spam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi all,
I was wondering: Why isn't it possible to write "static classes". By this
I mean a class that will _never_ get instantiated, but will always work on
and with other static members of its class.


Probably because no one thought it would be as valueable as it has turned
out to be. C# 2.0 will support static classes for pretty much this reason.
You will have to mark every member as static still, but the compiler throws
an error if you don't, and it takes care of the sealing and private
constructor.

Outside of that, what you want is achievable, just mark your class as sealed
and give it a private constructor(with no public ones). That'll stop
initalizations.
Nov 16 '05 #4

P: n/a
Willem van Rumpt wrote:
Hi all,
I was wondering: Why isn't it possible to write "static classes". By
this I mean a class that will _never_ get instantiated, but will always
work on and with other static members of its class.
Excellent idea and as other posters have pointed out -- will be implemented
in 2.0

As far as why it wasn't implemented before, I think the bent was towards
classes as universals that are shared between applications -- rather than
called within a single app ( which, in practice, is actually far more
common ).

The class would in effect behave like a "classic" win32 dll and its
exported functions, only this time referred by
<classname>.<ExportedFunction>

I know (how) I can achieve the same results with a singleton, but why
not just mark the entire class as "static" instead of marking every
single member as static?

I'm quite new to .NET (digging, gropin and mutilating around for about 8
months now), so, my questions are:
- Is it possible?
- If not, are there better solutions to my "problem"?
- Would it any how be a nice language/platform addition?

Thanks in advance,

Willem


Nov 16 '05 #5

P: n/a
thank you all for your replies. I'll make do with the provided
solutions, have a look at the C# 2.0 specs, and wait for Whidbey to
arrive (somewhere late autumn 2008, as I've understood ;) ).

Willem van Rumpt
Nov 16 '05 #6

P: n/a
Hi Willem,

If you want to start playing with it now, you can download the new Visual
Studio 2005 Express Beta 1:

http://lab.msdn.microsoft.com/express/vcsharp/

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com
"Willem van Rumpt" <no***********@no.thelandslide.spam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
thank you all for your replies. I'll make do with the provided
solutions, have a look at the C# 2.0 specs, and wait for Whidbey to
arrive (somewhere late autumn 2008, as I've understood ;) ).

Willem van Rumpt

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.