Re: Rationale for CS0536?



Matthias,

You could use static methods to implement interfaces, but you would have
to do it in a roundabout way, calling your static methods from the methods
on the instance.

Of course, that isn't what you want. Interfaces are closely tied to
objects (instances) not types (classes). While you could have interfaces
that are implemented with static properties, fields, and methods, it would
be just a roundabout way of encapsulating the functionality in an instance
(you would access static methods, which access local variables and static
fields, etc, etc).

What you should do is make your StdDeviation class a singleton class,
and expose the instance as an implementation of the Distribution interface
(which should be named IDistribution, as recommended by the public naming
conventions) through an Instance property (or GetInstance method). This
way, you have one instance which implements the interface.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx


<elvis_the_king@xxxxxx> wrote in message
news:1126532829.620929.321170@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi all,
>
> I'm still new to C# so maybe someone can explain me the rationale for
>
> CS0536: 'someClass' does not implement interface member
> 'some.Interface.Member()'. 'someClass.Member()' is either static, not
> public, or has the wrong return type.
>
> Why can't I have a class implementing the interface with static
> methods? IIRC, in Java or C++ it's possbile to call a static member
> either through the class name (someClass::Member()) or through an
> object instance.
>
> My motivation is that I have to work with mathematical/stochastical
> distributions. Thus I basically have an
>
> interface Distribution {
> double probability(double x);
> }
>
> and several classes implementing it. Additionally, there are some
> calculation methods that return "Distribution"s, so they give me
> instances of Distributions. Allright so far :)
>
> Now there's the standard normal distribution. As you might remember
> from your math classes, it does not depend on any specific parameters
> and is commonly used. So I would like to call
> StdNormalDistribution::probability() and the like in many places.
> Additionally, StdNormalDistribution is_a Distribution and may be
> returned by the calculation methods mentioned above. So it would be
> nice if StdNormalDistribution could be returned as an instance as well
> as being called statically when needed.
>
> Is there a "nice" way to work around this in C#? Is my oop design
> broken? Am I overlooking a good reason for doing it as-is?
>
> [Of course the "other way round" - having the interface static but the
> instance non-static cannot work; I'm not even sure if "static" makes
> sense in an interface?]
>
> Thanks a lot,
> Matthias
>


.



Relevant Pages

  • Rationale for CS0536?
    ... 'someClass' does not implement interface member ... Why can't I have a class implementing the interface with static ... Now there's the standard normal distribution. ... returned by the calculation methods mentioned above. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Rationale for CS0536?
    ... Is there any other possible distribution that expect something else than ... > Why can't I have a class implementing the interface with static ... > returned by the calculation methods mentioned above. ... > nice if StdNormalDistribution could be returned as an instance as well ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Rationale for CS0536?
    ... > Why can't I have a class implementing the interface with static ... > Now there's the standard normal distribution. ... > returned by the calculation methods mentioned above. ... > nice if StdNormalDistribution could be returned as an instance as well ...
    (microsoft.public.dotnet.languages.csharp)
  • Continous QoS Concept - Access -> Core
    ... The configuration in the Cat 4506 is ok because it is possible to cfg ... Should I cfg queuing in the downstream interface to the distribution ...
    (comp.dcom.sys.cisco)
  • Where to find tcng for debian sarge?
    ... I am quite new to debian but thanks to the user friendly installation ... distribution which was able to install itself on a P90/24mb ram;-) ... as daemon it did not work on my machine. ... interface up" and no to client everything works. ...
    (Debian-User)

Quantcast