473,387 Members | 1,721 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,387 software developers and data experts.

Statics & Interfaces

I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

Thanks

Sep 7 '06 #1
10 3328
<hu*******@yahoo.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
>I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?
Maybe you should make an abstract class instead?
>
Thanks

Sep 7 '06 #2
<hu*******@yahoo.comwrote:
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?
The CLR allows static methods to be implemented in interfaces, but even
then you couldn't just specify a static method which has to be
implemented by the type implementing the interface. C# doesn't even let
you put the static method in there in the first place.

I agree it would be useful sometimes, but consider: how would you call
the static method in the first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #3
Hi

First of all we should understand the keyword static; static says that
it would be having its own memory area irrespective of its instance
means this function will be loaded into the memory without having its
instance.

Now think if we have an interface and have one static function declared
in it without body. Now what will be loaded into the memory and if user
will call IInterface.StaticFunction(), what will be executed. So I
think its totally useless having a static function in interface.
-Rohit

Jon wrote:
<hu*******@yahoo.comwrote:
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

The CLR allows static methods to be implemented in interfaces, but even
then you couldn't just specify a static method which has to be
implemented by the type implementing the interface. C# doesn't even let
you put the static method in there in the first place.

I agree it would be useful sometimes, but consider: how would you call
the static method in the first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #4
An interface defines just a contract and not the implementation itself.
So if the static keyword would be allowed for a function then it would
only say that the class implementing the interface must declare this
function as static. Therefore, unless I missed something I think your
argument is not valid.

Rohit wrote:
Hi

First of all we should understand the keyword static; static says that
it would be having its own memory area irrespective of its instance
means this function will be loaded into the memory without having its
instance.

Now think if we have an interface and have one static function declared
in it without body. Now what will be loaded into the memory and if user
will call IInterface.StaticFunction(), what will be executed. So I
think its totally useless having a static function in interface.
-Rohit

Jon wrote:
<hu*******@yahoo.comwrote:
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?
The CLR allows static methods to be implemented in interfaces, but even
then you couldn't just specify a static method which has to be
implemented by the type implementing the interface. C# doesn't even let
you put the static method in there in the first place.

I agree it would be useful sometimes, but consider: how would you call
the static method in the first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #5
Good point. Don't know why I didn't think of this myself.. Thanks for
the tip. I think that might work.

Michael C wrote:
news:11*********************@b28g2000cwb.googlegro ups.com...
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

Maybe you should make an abstract class instead?

Thanks
Sep 7 '06 #6
<hu*******@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
An interface defines just a contract and not the implementation itself.
So if the static keyword would be allowed for a function then it would
only say that the class implementing the interface must declare this
function as static. Therefore, unless I missed something I think your
argument is not valid.
The "interface as a contract" is a useful concept, but not 100% correct.

Really, an interface is a restricted abstract class, with all functions
being pure virtual and no data. So the interface has a v-table layout which
needs to be followed by implementing classes, just like an abstract class
has a v-table layout used by subclasses.

Now, just like the Region class has a static member function Union which
operates on a group of Region objects, MSIL allows an interface to have
static member functions and properties which are generally useful and
applied to the entire tree of class types implementing that interface.

For example, if an interface declared an instance Guid property, it would be
useful to have a static method which returns the instance (of some derived
type) given the Guid. This static method is part of the interface type, and
not a contractual constraint on implementors.

The static keyword consistently means "part of the declaring type, not part
of any object". Thus you can see that an abstract static method is
impossible.

To get something like static methods on derived types, you can borrow from
COM's playbook. In COM implementations in C (not C++), you had to set up
the object's v-table by declaring a structure of function pointers. You can
do the same, by having a class of delegate fields. The interface declares a
property with that class type. Each implementing class needs to initialize
one copy of this class in the type initializer, to point to its set of
static methods, and then the (instance) property just returns the static
field which refers to the delegate group.

Something like:

class RequiredStaticMethods
{
public readonly EventHandler a;
public readonly EventHandler b;
public RequiredStaticMethods(EventHandler a, EventHandler b)
{
if (a == null || b == null) throw new ArgumentNullException();
this.a = a;
this.b = b;
}
}

interface INeedStaticMethods
{
RequiredStaticMethods StaticMethods { get; }
}

class AnImplementation : INeedStaticMethods
{
private static void OnA(object sender, EventArgs e) {}
private static void OnB(object sender, EventArgs e) {}
public static readonly RequiredStaticMethods StaticMethods = new
RequiredStaticMethods(OnA, OnB);
RequiredStaticMethods INeedStaticMethods.StaticMethods { get { return
StaticMethods; } }
}

now you can call:
AnImplementation.StaticMethods.a(...);
INeedStaticMethods x = new AnImplementation(); // call static method of
fixed type
x.StaticMethods.b(...); // dynamic dispatch to static method based on
runtime type of x
Sep 7 '06 #7
<hu*******@yahoo.comwrote:
An interface defines just a contract and not the implementation itself.
So if the static keyword would be allowed for a function then it would
only say that the class implementing the interface must declare this
function as static. Therefore, unless I missed something I think your
argument is not valid.
Except that in the CLR, an interface *can* have a static method - which
must have an implementation. (i.e. it's not the same type of beast as
the rest of an interface).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #8
Thanks for the detailed explanation
Ben Voigt wrote:
news:11**********************@b28g2000cwb.googlegr oups.com...
An interface defines just a contract and not the implementation itself.
So if the static keyword would be allowed for a function then it would
only say that the class implementing the interface must declare this
function as static. Therefore, unless I missed something I think your
argument is not valid.

The "interface as a contract" is a useful concept, but not 100% correct.

Really, an interface is a restricted abstract class, with all functions
being pure virtual and no data. So the interface has a v-table layout which
needs to be followed by implementing classes, just like an abstract class
has a v-table layout used by subclasses.

Now, just like the Region class has a static member function Union which
operates on a group of Region objects, MSIL allows an interface to have
static member functions and properties which are generally useful and
applied to the entire tree of class types implementing that interface.

For example, if an interface declared an instance Guid property, it would be
useful to have a static method which returns the instance (of some derived
type) given the Guid. This static method is part of the interface type, and
not a contractual constraint on implementors.

The static keyword consistently means "part of the declaring type, not part
of any object". Thus you can see that an abstract static method is
impossible.

To get something like static methods on derived types, you can borrow from
COM's playbook. In COM implementations in C (not C++), you had to set up
the object's v-table by declaring a structure of function pointers. You can
do the same, by having a class of delegate fields. The interface declares a
property with that class type. Each implementing class needs to initialize
one copy of this class in the type initializer, to point to its set of
static methods, and then the (instance) property just returns the static
field which refers to the delegate group.

Something like:

class RequiredStaticMethods
{
public readonly EventHandler a;
public readonly EventHandler b;
public RequiredStaticMethods(EventHandler a, EventHandler b)
{
if (a == null || b == null) throw new ArgumentNullException();
this.a = a;
this.b = b;
}
}

interface INeedStaticMethods
{
RequiredStaticMethods StaticMethods { get; }
}

class AnImplementation : INeedStaticMethods
{
private static void OnA(object sender, EventArgs e) {}
private static void OnB(object sender, EventArgs e) {}
public static readonly RequiredStaticMethods StaticMethods = new
RequiredStaticMethods(OnA, OnB);
RequiredStaticMethods INeedStaticMethods.StaticMethods { get { return
StaticMethods; } }
}

now you can call:
AnImplementation.StaticMethods.a(...);
INeedStaticMethods x = new AnImplementation(); // call static method of
fixed type
x.StaticMethods.b(...); // dynamic dispatch to static method based on
runtime type of x
Sep 7 '06 #9
<hu*******@yahoo.comwrote:
>I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?
I just whining abou this the other day in a blog entry. It's a feature I
would quite like to see, as well as some other realated stuff.

http://www.coversant.net/dotnetnuke/...=88&EntryID=14

Unfortunatly, all I have is questions and suggestions - no good solutions.

--
Chris Mullins
Sep 8 '06 #10
Chris,

Actually, after the previous posts I thought abstract classes would be
the solution. It turns out they are not, at least not for my
intentions. What I would have liked is an abstract static method, just
as you suggested in your blog. Unfortunately, that is not possible.

Chris Mullins wrote:
I have an interface where I would like to have some static functions.
When I add "static" to any of the interface functions I get the
following error: "The modifier 'static' is not valid for this item".
Why is it not possible to define static functions in an interface?

I just whining abou this the other day in a blog entry. It's a feature I
would quite like to see, as well as some other realated stuff.

http://www.coversant.net/dotnetnuke/...=88&EntryID=14

Unfortunatly, all I have is questions and suggestions - no good solutions.

--
Chris Mullins
Sep 8 '06 #11

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

Similar topics

8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
5
by: Stuart MacMartin | last post by:
I have a problem with static lifetime (order of destruction of statics within different cpp files). I have a workaround that happens to work in my case. I'd like to know if this is luck or...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
1
by: R Reyes | last post by:
Hello All, I'm always looking for ways to improve my code. Most of the time (whenever I'm working on a project) I write a bunch of functions. Then after the project is finished, I put all the...
8
by: Dave | last post by:
I have a set of developers who have gone off and implemented an interface for nearly all classes in a project\solution, now some of these classes will need interfaces as they implement the provider...
0
by: Vijay | last post by:
Prep Courses for International Certifications, CSTE & CSQA & ISEB & ISTQB &Business Analyst & SOA Certifications in HYDERABAD. After receiving overwhelming response to our last 50+ batches, ...
9
by: Mark Rae | last post by:
Hi, Now that the VS.NET 2005 SP1 update patch is with us, I'm in the process of moving my main development environment onto 64-bit Vista Business Edition - so far, so good... However, there...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
3
by: DR | last post by:
I heard there is some trick to referencing statics in C# CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.