473,668 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

'staticInterfac e.Class1' does not implement interface member
'staticInterfac e.foo.doSomethi ng(int)'.
'staticInterfac e.Class1.doSome thing(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.doSometh ing implementation

return false;

}
}
Nov 15 '05 #1
5 3964
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******** *******@TK2MSFT NGP10.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:

'staticInterfac e.Class1' does not implement interface member
'staticInterfac e.foo.doSomethi ng(int)'.
'staticInterfac e.Class1.doSome thing(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.doSometh ing 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******** *******@TK2MSFT NGP10.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:

'staticInterfac e.Class1' does not implement interface member
'staticInterfac e.foo.doSomethi ng(int)'.
'staticInterfac e.Class1.doSome thing(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.doSometh ing 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******** *******@TK2MSFT NGP10.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:

'staticInterfac e.Class1' does not implement interface member
'staticInterfac e.foo.doSomethi ng(int)'.
'staticInterfac e.Class1.doSome thing(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.doSometh ing 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.co m>
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***@e xpknow.com>> wrote in message
news:%2******** *******@TK2MSFT NGP10.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:
>
> 'staticInterfac e.Class1' does not implement interface member
> 'staticInterfac e.foo.doSomethi ng(int)'.
> 'staticInterfac e.Class1.doSome thing(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.doSometh ing 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
2199
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 allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason Second question. If a language doesn't support a fairly obvious feature, one has to wonder if...
9
6358
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 keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
8
8433
by: Ganesh Kundapur | last post by:
Hi all, struct test { static int x; }X; main() { X.x = 100; }
7
17190
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
52753
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 it. thanks, Steven
5
5536
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 initializes static class members ? Thanks, Ali
17
2348
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
4
14888
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 strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
9
2159
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) Hide.dll methods and data I want to remain hidden I have a DLL, Hide.dll, that contains methods that I want to handle for
0
8799
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
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
8658
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...
0
7401
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
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
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
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
2026
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.