PC Review


Reply
Thread Tools Rate Thread

Can't create a static class!

 
 
pigeonrandle
Guest
Posts: n/a
 
      22nd Jan 2007
Hi,
Can anyone tell me why i cant create the following class as 'static':

//Start

using System;

namespace WinAppSQL.windows
{
public static class WinAPI
{
public static int go() { return 1; }
}
}

//End

The compiler refuses! I'm using VS2003 framework 1.1.4322 SP1.

Cheers,
James.

 
Reply With Quote
 
 
 
 
Larry Lard
Guest
Posts: n/a
 
      22nd Jan 2007
pigeonrandle wrote:
> Hi,
> Can anyone tell me why i cant create the following class as 'static':
>
> //Start
>
> using System;
>
> namespace WinAppSQL.windows
> {
> public static class WinAPI
> {
> public static int go() { return 1; }
> }
> }
>
> //End
>
> The compiler refuses! I'm using VS2003 framework 1.1.4322 SP1.


Because classes can't be marked static?

If you want a class to be non-creatable, give it a private default
constructor:

class ICantBeInstantiated
{
private ICantBeInstantiated()
{ }
}

It's up to you as the class implementer to not create any instances
within the class itself...

--
Larry Lard
(E-Mail Removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
 
Reply With Quote
 
pigeonrandle
Guest
Posts: n/a
 
      22nd Jan 2007
That's because you don't have framework 2.0.

http://www.dotnetfun.com/Articles/cs...icClasses.aspx

Cheers,
Me.

pigeonrandle wrote:
> Hi,
> Can anyone tell me why i cant create the following class as 'static':
>
> //Start
>
> using System;
>
> namespace WinAppSQL.windows
> {
> public static class WinAPI
> {
> public static int go() { return 1; }
> }
> }
>
> //End
>
> The compiler refuses! I'm using VS2003 framework 1.1.4322 SP1.
>
> Cheers,
> James.


 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      22nd Jan 2007
Static classes only apply to framework 2 and above, not 1.1 I'm afraid.


pigeonrandle wrote:
> Hi,
> Can anyone tell me why i cant create the following class as 'static':
>
> //Start
>
> using System;
>
> namespace WinAppSQL.windows
> {
> public static class WinAPI
> {
> public static int go() { return 1; }
> }
> }
>
> //End
>
> The compiler refuses! I'm using VS2003 framework 1.1.4322 SP1.
>
> Cheers,
> James.


 
Reply With Quote
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      22nd Jan 2007
pigeonrandle,

Static class was introduced in .NET 2.0. You can simulate this behavior in
..NET 1.x, but not completely.


--
Stoitcho Goutsev (100)

"pigeonrandle" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> Can anyone tell me why i cant create the following class as 'static':
>
> //Start
>
> using System;
>
> namespace WinAppSQL.windows
> {
> public static class WinAPI
> {
> public static int go() { return 1; }
> }
> }
>
> //End
>
> The compiler refuses! I'm using VS2003 framework 1.1.4322 SP1.
>
> Cheers,
> James.
>



 
Reply With Quote
 
pigeonrandle
Guest
Posts: n/a
 
      22nd Jan 2007
All,
Thankyou for you words of wisdom. I will use a private constructor.

Cheers,
James.

Stoitcho Goutsev (100) wrote:
> pigeonrandle,
>
> Static class was introduced in .NET 2.0. You can simulate this behavior in
> .NET 1.x, but not completely.
>
>
> --
> Stoitcho Goutsev (100)
>
> "pigeonrandle" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi,
> > Can anyone tell me why i cant create the following class as 'static':
> >
> > //Start
> >
> > using System;
> >
> > namespace WinAppSQL.windows
> > {
> > public static class WinAPI
> > {
> > public static int go() { return 1; }
> > }
> > }
> >
> > //End
> >
> > The compiler refuses! I'm using VS2003 framework 1.1.4322 SP1.
> >
> > Cheers,
> > James.
> >


 
Reply With Quote
 
sherifffruitfly
Guest
Posts: n/a
 
      22nd Jan 2007

Stoitcho Goutsev (100) wrote:
> pigeonrandle,
>
> Static class was introduced in .NET 2.0. You can simulate this behavior in
> .NET 1.x, but not completely.


What else is there to a static class beyond (a) non-instantiability,
and (b) some static methods?

(not being sarcastic - I just don't know)

Thanks,

cdj

 
Reply With Quote
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      22nd Jan 2007
I knew that someone will ask this

You cannot declare instance members (inlcuding constructor) for a class
declared as *static* this restriction cannot be enforced in .NET 1.x. This
doesn't affect functionality, but it may help discovering errors at compile
time. That is what I had in mind.


--
Stoitcho Goutsev (100)

"sherifffruitfly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Stoitcho Goutsev (100) wrote:
>> pigeonrandle,
>>
>> Static class was introduced in .NET 2.0. You can simulate this behavior
>> in
>> .NET 1.x, but not completely.

>
> What else is there to a static class beyond (a) non-instantiability,
> and (b) some static methods?
>
> (not being sarcastic - I just don't know)
>
> Thanks,
>
> cdj
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jan 2007
Stoitcho Goutsev (100) wrote:
> I knew that someone will ask this
>
> You cannot declare instance members (inlcuding constructor) for a class
> declared as *static* this restriction cannot be enforced in .NET 1.x. This
> doesn't affect functionality, but it may help discovering errors at compile
> time. That is what I had in mind.


There's slightly more, actually.

In C# 1.1 there's no way of creating a type without an instance
constructor *at all* - if you don't specify one yourself, a default one
will be provided.

A static class not only doesn't allow you to declare an instance
constructor, but the class will not have one, so you can't instantiate
it even with reflection.

Jon

 
Reply With Quote
 
Dave Herron
Guest
Posts: n/a
 
      23rd Jan 2007
Methods and constructors can be static, not the class itself in 1.1.
You do not need to instantiate the static class to access static
members.

namespace WinAppSQL.windows
{
public class WinAPI
{
static WinAPI()
{
// static constructor login here
}

public static int go() { return 1; }
}
}

pigeonrandle wrote:
> Hi,
> Can anyone tell me why i cant create the following class as 'static':
>
> //Start
>
> using System;
>
> namespace WinAppSQL.windows
> {
> public static class WinAPI
> {
> public static int go() { return 1; }
> }
> }
>
> //End
>
> The compiler refuses! I'm using VS2003 framework 1.1.4322 SP1.
>
> Cheers,
> James.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking a Static Method in a Static Class =?Utf-8?B?SmltSGVhdmV5?= Microsoft C# .NET 3 7th Feb 2007 03:17 PM
Confusion over Static class and Static method! Anup Daware Microsoft C# .NET 2 5th Feb 2007 12:41 PM
Confusion over Static class and Static method! Anup Daware Microsoft ASP .NET 2 2nd Feb 2007 12:12 PM
Is This Safe? Static Class Reference In Static Function Amy L. Microsoft C# .NET 3 23rd Feb 2005 05:10 PM
Static class method return non-static object? Paschalis Pagonidis Microsoft C# .NET 8 10th Nov 2004 10:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:15 AM.