473,394 Members | 1,693 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,394 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 1351
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.