473,769 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# specification omission? Lack of static interface properties

Yes, I think so at least... In C# you *can* have static properties which are
quite useful when used properly. Now imagine the scenario where you need the
ability (sp?) to implement a variety of classes that must implement an
interface. All these classes must have a particular *static* property, and
this in particular is handy not only to be consequent with the fact that you
can do it with classes but also with the fact that you cannot specify member
variables in an interface (which IMHO is good that way, ie no member
variables in an interface definition).

And then... you happily put your static interface property that all the
implementing classes must implement and then "Invalid attribute for an
interface member" !!!

I believe this is an omission in the specification, sadly now I must find
another less straightforward way to achieve the same.
Nov 16 '05 #1
6 5896

"~~~ .NET Ed ~~~" <ti*********@ab olishspam.now> wrote in message
news:Ou******** ******@TK2MSFTN GP14.phx.gbl...
Yes, I think so at least... In C# you *can* have static properties which
are
quite useful when used properly. Now imagine the scenario where you need
the
ability (sp?) to implement a variety of classes that must implement an
interface. All these classes must have a particular *static* property, and
this in particular is handy not only to be consequent with the fact that
you
can do it with classes but also with the fact that you cannot specify
member
variables in an interface (which IMHO is good that way, ie no member
variables in an interface definition).

And then... you happily put your static interface property that all the
implementing classes must implement and then "Invalid attribute for an
interface member" !!!

I believe this is an omission in the specification, sadly now I must find
another less straightforward way to achieve the same.


Actually, this would be the less straight forward way of achieving what you
want(which may or may not be an appropriate pattern to begin with).

Here is the big problem, "How do you call a static interface method?"
It cannot be called on the interface type itself, because the interface has
no ties to a particular type and therefore cannot be used to call a...how
would you put it, virtual static method.

It cannot be called on an interface instance since the instance has no
specific type associated with it at compilation time, thus the compiler has
no way of determining what static method to call(this is, of course,
ignoring the fact that C# doesn't allow static member access through
instances).

Equally, you can call it on the class, but then there is no point in
providing it in an interface as the interface itself cannot be tied to it
and you have to know the exact type to use it, breaking many of the benifits
of interfaces.

This particular functionality should not exist in interfaces. It is actually
probably the domain of a construct a step or two above interfaces, but I do
not know of a langauge that really supports the level of contractual
specification you desire.

Nov 16 '05 #2
Why not create a property in the interface that is non static. Then in the
class definition itself have that property get/set the static member of that
class type. Seems pretty clean to me. You can use some kind of naming
convention to let others know what the plans for this property are if you
are going to be sharing this code with co-workers.

Interfaces don't have types so if you had a an interface that several
classes derived from and tried to call a static method or property of that
interface, how would it know which classes static method to execute? (This
was explained very nicely by Daniel)

jim

"~~~ .NET Ed ~~~" <ti*********@ab olishspam.now> wrote in message
news:Ou******** ******@TK2MSFTN GP14.phx.gbl...
Yes, I think so at least... In C# you *can* have static properties which
are
quite useful when used properly. Now imagine the scenario where you need
the
ability (sp?) to implement a variety of classes that must implement an
interface. All these classes must have a particular *static* property, and
this in particular is handy not only to be consequent with the fact that
you
can do it with classes but also with the fact that you cannot specify
member
variables in an interface (which IMHO is good that way, ie no member
variables in an interface definition).

And then... you happily put your static interface property that all the
implementing classes must implement and then "Invalid attribute for an
interface member" !!!

I believe this is an omission in the specification, sadly now I must find
another less straightforward way to achieve the same.

Nov 16 '05 #3
Good points indeed and yet it is something I have missed on several
ocassions. What I had indeed
was an instance property that accessed a static member, only to use that I
must first create an
instance of the class (and in this case just for that purpose) and that
seemed a bit of an overkill.

Then I considered simply using a static public member, but then the whole
purpose of using an
interface for this particular situation is missed.

Another alternative was to use an abstract class instead of an interface and
use the "is" operator but
again that seemed like an overkill, not to mention that if you need that
class to also inherit for another
then you reached the end of the rope.

Anyway thanks, I will have to resort to some of these workarounds.

"~~~ .NET Ed ~~~" <ti*********@ab olishspam.now> wrote in message
news:Ou******** ******@TK2MSFTN GP14.phx.gbl...
Yes, I think so at least... In C# you *can* have static properties which are quite useful when used properly. Now imagine the scenario where you need the ability (sp?) to implement a variety of classes that must implement an
interface. All these classes must have a particular *static* property, and
this in particular is handy not only to be consequent with the fact that you can do it with classes but also with the fact that you cannot specify member variables in an interface (which IMHO is good that way, ie no member
variables in an interface definition).

And then... you happily put your static interface property that all the
implementing classes must implement and then "Invalid attribute for an
interface member" !!!

I believe this is an omission in the specification, sadly now I must find
another less straightforward way to achieve the same.

Nov 16 '05 #4
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:uk******** ******@TK2MSFTN GP10.phx.gbl...
Here is the big problem, "How do you call a static interface method?"


Well, I guess the one practical use of such a beast, is that it will
allow the compiler to bitch at you, if you forget to implement it.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 16 '05 #5
James Curran <Ja*********@mv ps.org> wrote:
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:uk******** ******@TK2MSFTN GP10.phx.gbl...
Here is the big problem, "How do you call a static interface method?"


Well, I guess the one practical use of such a beast, is that it will
allow the compiler to bitch at you, if you forget to implement it.


Yes, and constructors could be handled in the same way.

I always feel there's practical room for such static methods and
constructors, but there really are theoretical reasons against them.
I'm not sure the theoretical reasons should overpower the practical
reasons, myself, but there we go. We'd need to invent some new syntax,
of course. Something like:

Type t = <dynamically load a type here>;

ISomeInterface( t).StaticMethod (...);
or
ISomeInterface instance = ISomeInterface( t).new(...);

I'm sure someone can come up with much better ideas. They really would
help for plug-in development.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
James Curran <Ja*********@mv ps.org> wrote:
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:uk******** ******@TK2MSFTN GP10.phx.gbl...
> Here is the big problem, "How do you call a static interface method?"
Well, I guess the one practical use of such a beast, is that it will
allow the compiler to bitch at you, if you forget to implement it.


Yes, and constructors could be handled in the same way.

I always feel there's practical room for such static methods and
constructors, but there really are theoretical reasons against them.
I'm not sure the theoretical reasons should overpower the practical
reasons, myself, but there we go. We'd need to invent some new syntax,
of course. Something like:

Type t = <dynamically load a type here>;

ISomeInterface( t).StaticMethod (...);
or
ISomeInterface instance = ISomeInterface( t).new(...);

I'm sure someone can come up with much better ideas. They really would
help for plug-in development.


These mechanisms cetainly would be rather valuable, however I don't think
they are nessecerily appropriatly applied to interfaces. Interfaces have an
established meaning already and should not be significantly extended, IMHO.

I think the nessercy contracts really extend beyond methods and
constructors. A real contracting system like this would ideally allow
everything from static methods and constructors to required interfaces,
attributes, generic type specifications, and potentially Design By Contract
style assertions or constraints that define the contractual domains
input(that is, allow a *contract* to specify that MethodA(int) will only
recieve values between 1 and 4822 while the method itself may assume any set
that contains 1 - 4822), or even maybe a language\runtim e supported factory
system. Atleast, that is my opinion.

The problem really is tying it all together without complicating the
language significantly. As I hinted to in my original response, static
method may well sit two levels above interfaces, where the first level may
be DbC and input\output constraints.


--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

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

Similar topics

2
15945
by: Paul Selormey | last post by:
I have looked through the documents but could not find any information on this. Is anything like static properties in interfaces? If not, how do I define property in interface to be made static property in an abstract base class implementing the property? The problem is, I have a lot of interfaces in a library project and will wish most uses of the library classes should be through
5
24228
by: TruongLapVi | last post by:
Hi, Why C# does not support Interface static member ? Some time I want implement NullObject Pattern: public interface INullObject { public static INullObject Null { get { return NullObject.Instance; } // !!! Wrong, C# not support ? }
6
1704
by: mswlogo | last post by:
There are many threads on the lack of a true unmanaged C++ const like behavior in C# (.Net) and that's not what this topic is about. The topic is, what is the best practical way to live with it. Some developers feel any function that returns an object should copy it. This gets pretty expensive performance wise and development wise and hard to enforce. It also defeats a lot of what C# intentionally did. Other developers feel you should...
15
1895
by: Marshal | last post by:
First... let's deal with Delegates. Comments welcome. 1) Invoking a NULL delegate is annoying. ** (It should just do nothing rather than crash.) 2) It's too easy to accidently attach multiple redundant handlers onto delegates. 3) A delegate might point to handlers on objects that are out of scope or been disposed, causing those objects to crash.
4
2487
by: Mantorok | last post by:
Hi I have an ASP app which references a few static properties in some of the classes. I understand that you should use Session variables, but is it "possible" to have each session "not" reference the same static members. For example if I update the static member on one session this change will be reflected on somebody elses session, which I don't want. I hope there's an easy solution for this as changing to session variables
6
3325
by: David | last post by:
Is there a good workaround the lack of multiple-inheritance? What if some classes expose A's properties and some B's and others expose A's and B's? I have an A class, a B class, an AB class and an AB : C class. The AB class has code copied into it from both A and B. Thanks.
7
1463
by: sherifffruitfly | last post by:
Hi, I'm learning the .net Bloomberg api, and it's main class has all of its stuff static. The help then goes on to say that the class is implemented as a singleton. It's cool I guess to make sure not more than one instance can be made, but with everything in the class static, I don't actually understand why even *one* instance is required. What's the design rationale for this?
3
2310
by: =?Utf-8?B?TkVXMi5ORVQ=?= | last post by:
I have a static event declared in a C++ ref class, that can then be handled in a VB app. I'm trying to expose the static event through the interface that the C++ ref class implements so the VB app can use the interface as preferred. How do you expose a static event through an interface? In the example below as coded, in a VB app I can access and use the general method (MyMethod) if I declare my handle variable as an IMyInterface type....
10
1470
by: =?Utf-8?B?Y2FybG0=?= | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
0
9590
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
9424
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
10051
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...
0
8879
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
7413
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
6675
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
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3571
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.