471,627 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 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 1273
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 discussion thread is closed

Replies have been disabled for this discussion.

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.