473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

generic problem with operator. compile error: operator * is not allowedon type T.

Hi,

I am trying to use Generics to create a general method which accepts a
number as a parameter (integer, decimal, ...) and then performs some
calculations.

Below is a simplified example:

private static T test(T somenumber)
{
return ( somenumber * 2);
}

The problem is that this does not compile because the operator * is not
allowed on type T.

How can I solve this without using reference types/objects to do the
calculation ?
I tried adding the :

where T: struct

but it did not help.

I know c# 2.0 operators can not be defined in an interface so I also am
not able to define a different WHERE clause accepting the operator *.

Is there anybody that can help me with this ?

Thanks VERY MUCH in advance,

Regards,
Serge
Feb 20 '06 #1
2 1346
You can not assume that T will have certain operator defined, and there is
no constraint you could apply.

A good article on the subject with solution is here:
http://www.codeproject.com/csharp/genericnumerics.asp

"SharpKnight" <no****@hotmail.com> wrote in message
news:43***********************@news.skynet.be...
Hi,

I am trying to use Generics to create a general method which accepts a
number as a parameter (integer, decimal, ...) and then performs some
calculations.

Below is a simplified example:

private static T test(T somenumber)
{
return ( somenumber * 2);
}

The problem is that this does not compile because the operator * is not
allowed on type T.

How can I solve this without using reference types/objects to do the
calculation ?
I tried adding the :

where T: struct

but it did not help.

I know c# 2.0 operators can not be defined in an interface so I also am
not able to define a different WHERE clause accepting the operator *.

Is there anybody that can help me with this ?

Thanks VERY MUCH in advance,

Regards,
Serge

Feb 20 '06 #2
Thanks very much! it helped me solve this puzzle

Lebesgue schreef:
You can not assume that T will have certain operator defined, and there is
no constraint you could apply.

A good article on the subject with solution is here:
http://www.codeproject.com/csharp/genericnumerics.asp

"SharpKnight" <no****@hotmail.com> wrote in message
news:43***********************@news.skynet.be...
Hi,

I am trying to use Generics to create a general method which accepts a
number as a parameter (integer, decimal, ...) and then performs some
calculations.

Below is a simplified example:

private static T test(T somenumber)
{
return ( somenumber * 2);
}

The problem is that this does not compile because the operator * is not
allowed on type T.

How can I solve this without using reference types/objects to do the
calculation ?
I tried adding the :

where T: struct

but it did not help.

I know c# 2.0 operators can not be defined in an interface so I also am
not able to define a different WHERE clause accepting the operator *.

Is there anybody that can help me with this ?

Thanks VERY MUCH in advance,

Regards,
Serge


Feb 20 '06 #3

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

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
6
by: Andrus | last post by:
I need to create generic table field level cache. Table primary key (PrimaryKeyStructType) can be int, string or struct containing int and string fields. FieldName contains table field name to be...
4
by: Bliss | last post by:
I have some problems with my generic. Part of code: using System; using System.Collections.Generic; using System.Text; namespace Gnrc { public class Gnrc<T>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.