I am betting this is a very hard problem for 99.9995% of programmers. It's supposed to be simple and supposed to be one of the benefits of generics, yet generics will not work with this incredibly simple case. If you can get it to work, I'd love to see it.
Take the class below and convert it to accept generic arguments of <int> or <double> or <long> or whatever. You'll find a big surprise when you you get it coded up and try to call IsDataValid because it's going to say that your generic objects don't support the operation you are attempting.
Good luck.
Public Class X_GreaterThan_Y Private _X As Double
Private _Y As Double
Public Sub New()
End Sub
Public WriteOnly Property X() As Double Set(ByVal value As Double)
_X = value
End Set End PropertyPublic WriteOnly Property Y() As Double Set(ByVal value As Double)
_Y = value
End Set End PropertyPublic Function IsDataValid() As Boolean If _X <= _Y Then Return False Else Return True End Function
End Class

GenericIZE This...
JonPul
Obviously. Your template argument is an object, so it only supports the four basic methods. I presume that IComparable is the interface which your template argument must impliment in order for this to work. You can specify this, but I'm having some trouble finding the syntax online.
GeorgeKumpuckal
class XGreaterThanY<T> where T : System.IComparable<T>
The magic is all in the where clause. Beyond that, the only trick is that IComparable gives you the CompareTo method, it does not offer you <, ==, > operators. If you add the where clause to your generic class and use CompareTo instead of operators, that's all you need to do.
// I guess I'm a little lost on seeing the benefits of genericity outside of creating a typed collection.
I agree, you may as well just carry an instance of the interface as make it a templated variable, the only difference is you'll get back an instance of your type and not just the interface. Generics are significantly dumbed down compared to C++ templates, and I see little use for them right now outside of typed containers, myself.
gppradeep
Here it is in C#. In essence, if you want to use methods on your templated type, you need to specialise T so that those methods are guarenteed to be there. Otherwise, object is a valid parameter for T, and that gives you only the four basic methods.
class XGreaterThanY<T> where T : System.IComparable<T>{
private T _x; private T _y; public T X{
set{
_x =
value;}
}
public T Y{
set{
_y =
value;}
}
public bool IsDataValid(){
return (_x.CompareTo(_y) > 0);}
}
TZetlan
You might be right... I am trying to make a class that's even more generic than this. It would be called something like XCompareToY and it would let you say what kind of comparison you wanted to do, probably in an enumerated type that had options like...
XisGreaterThanY
XisLessThanY
XisLessThanOrEqualY
Xis GreaterThanOrEqualY
XisEqualY
and then it would accept whatever types you passed to it.
I will try and work with your example and convert it to vb (I don't know c# very well), and see if I can acheive this, but last time I tried it I got lost trying to find a way to guarantee that X and Y support some methods. I mean, if you have to do that, then where does your genericity come from, if you have to nail down what methods your type is going to have. I guess I'm a little lost on seeing the benefits of genericity outside of creating a typed collection. When it comes to making generic classes, I have been looking all over my code for places to fit it in, and it seems more like a square peg in a round hole... it falls just short of meeting some criteria to fit in ANYWHERE in my code. I'm just not finding much use for it.
Arik Peltz
Is the correct VB syntax for generic constraints.
For more information on the Not So 2005 Applicable MSDN see:
http://msdn.microsoft.com/vbasic/reference/vbasic/default.aspx pull=/library/en-us/dnvs05/html/vb2005_generics.asp#vb2005_generics_topic4