-2

Possible Duplicate:
Is there a way to force a C# class to implement certain static functions?

Consider a class and consider that, for some reasons, you need to force this class to have a constant inside it. This is to be applied to a whatever set of classes. In my specific case I need some classes of mine to have a constant inside them to hold the corresponding null value.

Those classes that, in my case, implement a certain interface because they must behave in a certain way, must also have three special values that are needed in my context (context that is too long to explain and quite useless here).

So basically I need a way to force my classes to have a certain constant inside them (accessible from outside). I know that I could simply put the constant in those classes, but what if I am looking for a way to tell the compiler: "If this class implements this interface (for example), that you should not compile it if there is not that constant!".

In this way there is the compiler checking, and not just the programmer who might forget to put the constant.

I hope I could explain my problem. Thanks in advance.

Community
  • 1
  • 1
Andry
  • 16,172
  • 27
  • 138
  • 246
  • 2
    Please provide context info. *Why* do you want that these classes have a specific static method. – ulrichb Oct 10 '12 at 17:27
  • If the class is used as an interface then its not possible. Why don't you just implement the static method in the class that inherits the interface? – Security Hound Oct 10 '12 at 17:27
  • 3
    I recomend you rethink your problem. Static methods cannot be accessed from a class instance, and interfaces are only useful for defining what an instance can do. – Trisped Oct 10 '12 at 17:27
  • static methods aren't class level methods – Sleiman Jneidi Oct 10 '12 at 17:28
  • you can always use generics and an abstract class / interface with a method which returns T. no need for static methods though – YavgenyP Oct 10 '12 at 17:31
  • Why don't you just write the static method in your class? Why do you need to force it? – Bob. Oct 10 '12 at 17:33
  • _In this way there is the compiler checking, and not just the programmer who might forget to put the constant._ Can you explain that? How do you forget to put a constant that is already there? Last time I checked, variables don't randomly go poof after you declare them? – Bob. Oct 10 '12 at 17:40
  • @Bob: You use interfaces to force some behavior ok? Well, this said, try to extend your mind to this problem: there are some behaviors that that class should show, but they happen to be not at instance level. Variables do not go poof, simply I need that a certain class that implements a certain interface, MUST also have a constant inside it. Your question would be the same if formulated again like this: Why do not you simply put methods in the class? Why should you forget them??? As far as I know they do not go poof... – Andry Oct 10 '12 at 17:46
  • 1
    alternatively, may be you can write a custom code analysis rule? – prashanth Oct 10 '12 at 17:56
  • @prashanth: That might be a good idea but isn't it a little against confucio's principle: "Do not use cannons to kill mosquitos"? :) – Andry Oct 10 '12 at 17:58
  • 1
    Just implement a public property and the value can only be read. This would mean that any class that did not implement the property would cause a compile error. – Security Hound Oct 10 '12 at 18:31

3 Answers3

1

So basically I need a way to force my classes to have a certain constant inside them (accessible from outside). I know that I could simply put the constant in those classes, but what if I am looking for a way to tell the compiler: "If this class implements this interface (for example), that you should not compile it if there is not that constant!".

You can achieve something like this creating a class containing an unique public constructor which requires all the information/values you need. So for example you could use something like:

class myClass{
   public readonly int Value1;
   public readonly string Value2;
   public myClass(int value1, string value2){
         Value1 = value1;
         Value2 = value2;
   }
}

Infact in this case the programmer is forced to instantiate the object giving the right values to the unique constructor available, and can't change the value of the two public fields (due to the readonly keyword).

Omar
  • 16,329
  • 10
  • 48
  • 66
0

Explanation from a previous question on the same topic:

A call to a static method is done through the class name, not through an object reference, and the IL code to call it will call the abstract method through the name of the class that defined it, not necessarily the name of the class you used.

Previous post: Why can't I have abstract static methods in C#?

Community
  • 1
  • 1
jheddings
  • 26,717
  • 8
  • 52
  • 65
0

What is wrong with just adding the static method into the class? I don't see why you need another layer when its perfectly logical to code in the static method directly into the class.

 public class MyClass {
      public static MyClass TheMethod(int i){
           // TODO:
      }
 }
Bob.
  • 3,894
  • 4
  • 44
  • 76