473,799 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static class

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
8 8888
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******** ********@tk2msf tngp13.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
Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Ma th()' 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***@sympatic o.ca> wrote in message
news:ZK******** ************@ne ws04.bloor.is.n et.cable.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******** ********@tk2msf tngp13.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
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
> Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Ma th()' 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
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_******** *************@n ews2.telusplane t.net...
Do you know how the Math class is defined?

If I try:
Math m = new Math();
I get:
'System.Math.Ma th()' 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***@sympatic o.ca> wrote in message
news:ZK******** ************@ne ws04.bloor.is.n et.cable.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******** ********@tk2msf tngp13.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
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******** ********@tk2msf tngp13.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
"Ignacio Machin" <ignacio.mach in 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.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #8
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12864
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
15
6596
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
3
9759
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx mauzi
3
11832
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to add items to this list, but have the base class hold the data and expose the property. Problem is, however, that the derived class' static constructor doesn't get called when DerivedClass.MyList is accessed, so the list has no members. If
9
2665
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database operations on purchase orders and inventory. I have created a PurchaseOrder class and Inventory class to encapsulate operations like creating POs, finding items, etc. These two classes are used extensively from different parts of the app.
11
3846
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
12
3442
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
13
20778
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited & I don't want to copy + paste code. How can I inherit those member variables? Thanks
14
6028
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
5
2669
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or method is not allowed. How do you do this if you really need a static class but also have to use these instance objects in them? If you need a simple example of what I am trying to do, it is below: imports Data.EternityRecordsEntities namespace...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.