472,993 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

Generics, use mathematical operatos on type parameter

Hi all,

I would like to use mathematical operators (*, +, etc) on a generic
parameter, see the code below:

public class TestGenerics<T> where ???
{
private T d;
public TestGenerics(int a)
{
d = a * 3;
}
}

Is this possible?
If yes, what constraint should I use on the T type argument?

Thanks
Jock
Feb 4 '06 #1
7 1199
On Sat, 04 Feb 2006 10:50:07 GMT, "Jock" <Jo**@nospam.com> wrote:
Is this possible?


No. You'll have to work around this problem with an interface or base
class that implements the required functionality.
--
http://www.kynosarges.de
Feb 4 '06 #2
>>Is this possible?

No. You'll have to work around this problem with an interface or base
class that implements the required functionality.

That what I suspected!
Unfortunately when you deal with native types (int, float, double, etc)
Generics are not so handy!
When will Microsoft provide us a useful Generics implementation similar to
C++ Template?!? :))

Jock
Feb 4 '06 #3
> When will Microsoft provide us a useful Generics implementation similar
to C++ Template?!? :))


For instance would be very handy having something like that:

public class TestGenerics<T> where T : numeric
{
private T d;
public TestGenerics(int a)
{
d = a * 3;
d = (d + 7) *5;
}
}

"where T : numeric" means that you can apply to T every math operator that
is supported by every numeric *primitive* types.
So the code in the constructor above will be valid!

What do you think?

Jock
Feb 4 '06 #4
That would be quite useful. I suggest you submit your idea to the
MSDN Product Feedback Center so that the developers see it. In fact,
there's already a submission for operator restriction:

http://lab.msdn.microsoft.com/produc...c-36f1806d7ea8

You could vote for it so that MS is more likely to implement it for
the next release.
--
http://www.kynosarges.de
Feb 5 '06 #5
Wait, here's another existing suggestion that's a bit closer to your
own proposal:

http://lab.msdn.microsoft.com/produc...1-8ce455606f72
--
http://www.kynosarges.de
Feb 5 '06 #6
You could vote for it so that MS is more likely to implement it for
the next release.


Christoph,

Thanks for your suggestion.
I an going to read and vote for the suggestions on the MSDN Product Feedback
Center you point
(I even didn't know that something like that exists!).

I hope we can achieve more power with Generics very soon.

Jock
Feb 5 '06 #7
Christoph Nahr wrote:
Wait, here's another existing suggestion that's a bit closer to your
own proposal:

http://lab.msdn.microsoft.com/produc...1-8ce455606f72

Here's another:
http://tinyurl.com/8amzd

Here is an article describing the suggestion:
http://www.codeproject.com/csharp/genericnumerics.asp

HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Feb 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
3
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class...
17
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
2
by: Ravi Shekhar | last post by:
Hello, So I'm doing some mathematical modeling and it turns out I need distinct imaginary and complex types to carry out complex contour integration. I want to have it in all three precisions...
6
by: CassioT | last post by:
Hi. I want to create a base form class with a generic parameter. public BaseForm<T: Form {} public MyForm : BaseForm<MyType> {} The problem here is that the inherited form doesn't work in...
5
by: teel | last post by:
Hi there, I'm trying to apply "less than" and "more than" operators on the Generics (class template-like) type objects. Below is the code of my class representing a parameter that can be any type,...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.