473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

static modifier and interfaces

Why can't I create an interface and make its implementation static/shared?

Is there some pattern that lets me work around this?

Any explanation of why this is would be appreciated.

This is the message I get for the following code:

'staticInterface.Class1' does not implement interface member
'staticInterface.foo.doSomething(int)'.
'staticInterface.Class1.doSomething(int)' is either static, not public, or
has the wrong return type.
interface foo

{

bool doSomething(int y);

}
public class Class1 : foo

{

public bool x() { }

static public bool doSomething(int y)

{

// TODO: Add Class1.doSomething implementation

return false;

}
}
Nov 15 '05 #1
5 3943
The interface you wrote, is a contract. Meaning any class implementing it
has to have doSomething as a public instance method. That way if you have
an instance of the class that implements your interface, you know you can
call doSomething on it.

However, if you had the option of making doSomething static - how can you be
sure that you can call this method on an instance? You can't - you would
never know if it was an instance or static implementation.

"Andy" <ea***@expknow.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Why can't I create an interface and make its implementation static/shared?

Is there some pattern that lets me work around this?

Any explanation of why this is would be appreciated.

This is the message I get for the following code:

'staticInterface.Class1' does not implement interface member
'staticInterface.foo.doSomething(int)'.
'staticInterface.Class1.doSomething(int)' is either static, not public, or
has the wrong return type.
interface foo

{

bool doSomething(int y);

}
public class Class1 : foo

{

public bool x() { }

static public bool doSomething(int y)

{

// TODO: Add Class1.doSomething implementation

return false;

}
}

Nov 15 '05 #2
100
Hi Andy,
Interface members are implicitly abstract (virtual) and have to be
implemented.
You can think of implementing an interface members as overriding them.
Overriding make sense for instance methods only.

HTH
"Andy" <ea***@expknow.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Why can't I create an interface and make its implementation static/shared?

Is there some pattern that lets me work around this?

Any explanation of why this is would be appreciated.

This is the message I get for the following code:

'staticInterface.Class1' does not implement interface member
'staticInterface.foo.doSomething(int)'.
'staticInterface.Class1.doSomething(int)' is either static, not public, or
has the wrong return type.
interface foo

{

bool doSomething(int y);

}
public class Class1 : foo

{

public bool x() { }

static public bool doSomething(int y)

{

// TODO: Add Class1.doSomething implementation

return false;

}
}

Nov 15 '05 #3
Ok, interfaces are contract - yeah. But why limit them to instance methods?

Doesn't it make sense that you might sometime want to create a group of classes that implement specific behavior (contract) that is static?

Is there a pattern that will let me work around this?

"Andy" <ea***@expknow.com> wrote in message news:%2***************@TK2MSFTNGP10.phx.gbl...
Why can't I create an interface and make its implementation static/shared?

Is there some pattern that lets me work around this?

Any explanation of why this is would be appreciated.



This is the message I get for the following code:

'staticInterface.Class1' does not implement interface member
'staticInterface.foo.doSomething(int)'.
'staticInterface.Class1.doSomething(int)' is either static, not public, or
has the wrong return type.


interface foo

{

bool doSomething(int y);

}


public class Class1 : foo

{

public bool x() { }

static public bool doSomething(int y)

{

// TODO: Add Class1.doSomething implementation

return false;

}


}

Nov 15 '05 #4
Andy <ea***@expknow.com> wrote:
Ok, interfaces are contract - yeah. But why limit them to instance methods?

Doesn't it make sense that you might sometime want to create a group of
classes that implement specific behavior (contract) that is static?
It does, but just being able to specify a static method in the
interface is only half the story - the other half is invoking that
method. How would you expect to do that?
Is there a pattern that will let me work around this?


It depends on exactly what you're doing. The factory pattern might be
helpful to you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Andy wrote:
Ok, interfaces are contract - yeah. But why limit them to instance methods?

Doesn't it make sense that you might sometime want to create a group of
classes that implement specific behavior (contract) that is static?

*Is there a pattern that will let me work around this?*

"Andy" <ea***@expknow.com <mailto:ea***@expknow.com>> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
> Why can't I create an interface and make its implementation

static/shared?
>
> Is there some pattern that lets me work around this?
>
> Any explanation of why this is would be appreciated.
>
>
>
> This is the message I get for the following code:
>
> 'staticInterface.Class1' does not implement interface member
> 'staticInterface.foo.doSomething(int)'.
> 'staticInterface.Class1.doSomething(int)' is either static, not

public, or
> has the wrong return type.
>
>
> interface foo
>
> {
>
> bool doSomething(int y);
>
> }
>
>
> public class Class1 : foo
>
> {
>
> public bool x() { }
>
> static public bool doSomething(int y)
>
> {
>
> // TODO: Add Class1.doSomething implementation
>
> return false;
>
> }
>
>
> }
>
>

Here is what I think (1 1/2 c)..
*static* methods are a contract from the "Type" but not from an
"instance". i.e. The mere existence of a particular Type would guarantee
the existence of that *static*. Now, Interface are instance specific.
This has to be the case since interfaces are meant to discover and use
abilities of similar instances of types.

With that in mind, it would not really make sense to declare a interface
method as static since that would mean that the contract being agreed
upon is actually from the *type* of the derived class and not by the
*instance*.

And of course, the real thing to note is that if you consider COM
interfaces (or any implementation of interfaces) they are just runtime
vtbl pointers to concrete instance method, that would just not work with
a static.

Anyway, if you really want a static method to be part of a contract, you
can do that using a simple facade pattern to wrap the underlying
interfaces and use Template method + Factory to generate and use the
real interface implementations.

--
Girish Bharadwaj

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
8
by: Ganesh Kundapur | last post by:
Hi all, struct test { static int x; }X; main() { X.x = 100; }
7
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
5
by: A.M | last post by:
Hi, I have i utility class contains static functions (and also members) I usually use in may applications. Can i have a static like constructor so any time the app starts, The constructor...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.