Re: Rationale for CS0536?
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 12 Sep 2005 10:31:46 -0400
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
>
.
- Follow-Ups:
- Re: Rationale for CS0536?
- From: elvis_the_king@xxxxxx
- Re: Rationale for CS0536?
- References:
- Rationale for CS0536?
- From: elvis_the_king
- Rationale for CS0536?
- Prev by Date: Re: is it possible to create Activex Component using c#.net? If yes then how to create it?<EOM>
- Next by Date: Re: IActiveDesktop
- Previous by thread: Rationale for CS0536?
- Next by thread: Re: Rationale for CS0536?
- Index(es):
Relevant Pages
|