|
-
August 21st, 2009, 02:36 AM
#1
Numeric Based Generics Problem
Hey,
Ive got a problem with an object that im trying to keep generic. It is pretty much the same as a typical system.drawing.Point class but instead is a generic, so it could be of a type int, float, double, uint whatever.
Now making the class is easy enough, but now im writing the static methods which are causing problems as i dont know of any way to constrict the type to always be anything from byte to double, and when i try to write the subtract method:
Code:
public static void Sub(Point<T> a, Point<T> b)
{ return new Point<T>(a.X-b.X, a.Y-b.Y); }
i get the error that generic T cannot be subtracted from T, which is fair enough as it could be anything in its current state, so is there any way to get around this problem or restrict it to numeric based types, even if its just int or float. Im using .net 2.0 at the moment but have access to .net 3.5 if needed. Havent got 4 installed at the moment as im sure that has an INumeric interface which may be another solution...
Any help would be great!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|