|| 1000 apologies
| Accepted, although I'm really offended!
Doh! Double Doh!
I meant to say:
Accepted, although I'm really *NOT* offended!
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
message || 1000 apologies
| Accepted, although I'm really offended!
|
|| I was thinking of "class C<T> : T"
|
| Correct:
|
| Although the first time I saw "class C : B<C>" where B is a base class, I
| scratched my head. As I think of the "C<T> : T" case... Which I find odd
as
| I follow the "class C : I<C>" without any problems...
|
|| or "C<A,B> : A<B>".
| This is allowed! ;-)
|
| class A<T>
| {
| }
|
| class B
| {
| }
|
| class C<A,B> : A<B>
| {
| }
|
| C<object, IEquatable<object>> x = new
| C<object,IEquatable<object>>();
|
| Depending on what you use for A & B, they could even be the same type,
such
| as:
|
| class X : IEquatable<X>
| {
| public bool Equals(X other)
| {
| return false;
| }
| }
|
| C<X, X> x = new C<X,X>();
|
|
|
| --
| Hope this helps
| Jay B. Harlow [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley -
http://www.tsbradley.net
|
|
| || 1000 apologies - I was thinking of "class C<T> : T" or "C<A,B> : A<B>".
||
|| Your other post is indeed the way to go and the way that you would do
|| something similar in C++
||
|| message || > Nick,
|| > | eg you cannot inject methods into a derived class with:
|| > | public class DiscDistribution : IAddable<DiscDistribution>
|| > Umm... Yes you can!
|| >
|| > Try:
|| >
|| > public interface IAddable<T>
|| > {
|| > T Add(T other);
|| > }
|| >
|| > public class DiscDistribution : IAddable<DiscDistribution>
|| > {
|| > public DiscDistribution Add(DiscDistribution other)
|| > {
|| > return null;
|| > }
|| > }
|| >
|| > I use the above with IEquatable<T> all the time!
|| >
|| >
|| > --
|| > Hope this helps
|| > Jay B. Harlow [MVP - Outlook]
|| > .NET Application Architect, Enthusiast, & Evangelist
|| > T.S. Bradley -
http://www.tsbradley.net
|| >
|| >
|| > || > |
|| > | || > | > Hello,
|| > | >
|| > | > I probably don't see the obvious, but maybe you can help me out of
|| > this
|| > | > mess.
|| > | >
|| > | > The following is my problem:
|| > | > I created two classes NormDistribution and DiscDistribution. Both
|| > | > classes provide an implemation of the operator +.
|| > | >
|| > | > Now I want to write another generic class Plan<DType>, which can
|| > | > accept NormDistribution and DiscDistribution for DType. And here
| comes
|| > | > my problem. Class Plan must have a function like this:
|| > | >
|| > | > public class Plan<DType>
|| > | > {
|| > | > public DType Demand1;
|| > | > public DType Demand2;
|| > | > public DType Demand3;
|| > | >
|| > | > public void Optimize()
|| > | > {
|| > | > Demand3 = Demand1 + Demand2; // <- error CS0019
|| > | > }
|| > | > }
|| > | >
|| > | > How can I solve the problem?
|| > | > The background for this is, that my class Plan does some
| computations,
|| > | > which are independant of the underling distribution type. The
|| > | > computation differ only in the implementation of the basic
operators
|| > +,
|| > | > - and so on. I wanted to delegate the problem to the
| NormDistribtution
|| > | > and DiscDistribution class, but somehow I can not get it to work.
|| > | >
|| > | > Any ideas?
|| > | >
|| > | > Thanks,
|| > | > Sascha
|| > | >
|| > |
|| > | 1) Either have your distribution classes derive from a common base or
|| > | implement a common interface (Let's call it "Common" either way).
|| > | 2) Give Common an "public Common Add(Common other)" method
|| > | 3) Force DType to be derived from (or implement) Common: "public
class
|| > | Plan<DType> where DType: Common {...."
|| > | 4) Demand3 = Demand1.Add(Demand2);
|| > |
|| > | This isn't really satisfactory because you might as well forget about
|| > | generics and just work with "Common" using plain old polymorphism.
|| > |
|| > | As Chris pointed out - even ignoring the issue of operators or
methods
|| > you
|| > | cannot do the usual C++ stuff.
|| > |
|| > | eg you cannot inject methods into a derived class with:
|| > |
|| > | public class DiscDistribution : IAddable<DiscDistribution>
|| > |
|| > |
|| > |
|| >
|| >
||
||
|
|