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

static class

P: n/a
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain only
static members.

Thanks.
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
You can't make a class static. Just make a regular class and don't
instantiate it.

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain only static members.

Thanks.

Nov 13 '05 #2

P: n/a
Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Math()' is inaccessible due to its protection level

But of course I can do this:
double d = Math.PI;

That's the type of functionality I want.

Thanks.

"Alien" <al***@sympatico.ca> wrote in message
news:ZK********************@news04.bloor.is.net.ca ble.rogers.com...
You can't make a class static. Just make a regular class and don't
instantiate it.

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain

only
static members.

Thanks.


Nov 13 '05 #3

P: n/a
I'm pretty new to C# so please someone correct me if I'm wrong but, if I
understand correctly, you can use double d = Math.PI; because Math makes
available a public static property, method, field, etc titled PI (or
whatever). Create your class as normal and create your
methods/properties/fields as static.

Nov 13 '05 #4

P: n/a
> Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Math()' is inaccessible due to its protection level Make constructor private (if you don't want to instantiate objects of your
class, you don't realy nead constructor, but if you do so, you will get
warning if you try Myclass m = new Myclass())
But of course I can do this:
double d = Math.PI; Make all members static

That's the type of functionality I want.

You will get what you want ;)))
Greetings
Nov 13 '05 #5

P: n/a
Someone,
Make the default constructor of your class private, so no one can create an
instance of the class.

Make the class itself sealed, so no one can derive from it.

Only create static members in the class.

Hope this helps
Jay
"someone" <a@a.com> wrote in message
news:R_*********************@news2.telusplanet.net ...
Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Math()' is inaccessible due to its protection level

But of course I can do this:
double d = Math.PI;

That's the type of functionality I want.

Thanks.

"Alien" <al***@sympatico.ca> wrote in message
news:ZK********************@news04.bloor.is.net.ca ble.rogers.com...
You can't make a class static. Just make a regular class and don't
instantiate it.

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain

only
static members.

Thanks.



Nov 13 '05 #6

P: n/a
Hi,

You CANNOT make a class static, you can make methods, properties and
variables of that class static , though.

If you don't want an instance of that classes to be created you can declare
the constructor as private therefore nobody can create it. Also remember
that if you are going to have only static method then this constructor will
never be called, therefore you will need to have a constructor marked as
static

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"someone" <a@a.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Is it possible to declare a class as static? I can't seem to do that.

What if I need a class that I don't want instantiated? It will contain only static members.

Thanks.

Nov 13 '05 #7

P: n/a
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote:
You CANNOT make a class static, you can make methods, properties and
variables of that class static , though.
Agreed...
If you don't want an instance of that classes to be created you can declare
the constructor as private therefore nobody can create it.
Agreed.
Also remember
that if you are going to have only static method then this constructor will
never be called, therefore you will need to have a constructor marked as
static


Not agreed. You *may* need a static constructor, but in many cases you
don't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #8

P: n/a
Hi Jon,

Also remember
that if you are going to have only static method then this constructor will never be called, therefore you will need to have a constructor marked as
static


Not agreed. You *may* need a static constructor, but in many cases you
don't.


Ok, you are right, the correct word is "may" and no "need" , the english is
not my first language and sometimes I mix the words :)
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.