473,414 Members | 2,019 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,414 software developers and data experts.

Developing generic - wasting time?..

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>
{

//[some statements]

protected void SomeMethod()
{
T A, B, C;

//[some statements]

C = A + B; // CS0019: Operator '+' cannot be applied to operands of type 'T' and 'T'

//[some statements]
}
}
}

Is there available some "tricks" to bypass this error and compile ???
Of course, operator '+' is defined on 'T' type.

Best regards, Bliss.
Oct 29 '07 #1
4 1306
Bliss,

It might be defined on the type, but the compiler doesn't know it, and
can't apply it because the constraint system doesn't allow it.

Rather, you have to define your operations in an interface, and then
mark that as part of the constraint. Then, you can cast to your interface,
and perform operations on that.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bliss" <kd****@lipetsk.ruwrote in message
news:ed**************@TK2MSFTNGP02.phx.gbl...
>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>
{

//[some statements]

protected void SomeMethod()
{
T A, B, C;

//[some statements]

C = A + B; // CS0019: Operator '+' cannot be applied to operands of type
'T' and 'T'

//[some statements]
}
}
}

Is there available some "tricks" to bypass this error and compile ???
Of course, operator '+' is defined on 'T' type.

Best regards, Bliss.

Oct 29 '07 #2
On 2007-10-29 11:13:22 -0700, Bliss <kd****@lipetsk.rusaid:
[...]
Is there available some "tricks" to bypass this error and compile ???
Of course, operator '+' is defined on 'T' type.
Of course. :) But that doesn't mean that the generic class has any
way to represent that.

How much control do you have over the type used as T? If you can
modify the type, then one approach would be to define an interface that
requires the operator+ to be implemented, and then constrain T in the
generic to types that implement that interface.

If you can't create a type that allows for a suitable constraint in the
generic, then I don't believe you'll be able to do what you want.

Caveat: I haven't actually tried doing this. For all I know, operators
are a special case and can't be declared in an interface. If that's
true, then you may have to use a constraint that requires inheritance
of a specific class that implements the operator+ and of course make
sure your T type used inherits that class.

Pete

Oct 29 '07 #3
Rene пишет:
This may help you:

http://blogs.msdn.com/ericgu/archive.../14/52852.aspx

Nice blog. =) I think that this topic may be closed.

Best regards to all people who was seen there.
Oct 30 '07 #5

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

Similar topics

12
by: Laszlo Zsolt Nagy | last post by:
Hello, I would like to develop a new network protocol, where the server and the clients are Python programs. I think to be effective, I need to use TCP_NODELAY, and manually buffered transfers....
11
by: ZenRhapsody | last post by:
Has anyone done any performance testing between new generic Lists and single dimensional arrays? I really like the code flexibility the List provides since I don't know how many items I will...
23
by: Louly | last post by:
Hi everybody, I'm a 22 yrs old girl. I've been developing a Database for 6 months no using Access. A week ago I had this feeling that I'm wasting my time in Access. I don't think it's wise to...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
3
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify...
5
by: hnshashi | last post by:
I have writtem kernel(2.4) module to commu. with user space appl. using netlink socket. I am getting compilation error. kernel module:-> #include <linux/skbuff.h> #include<linux/module.h> ...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
5
by: Marc Gravell | last post by:
If you cannot understand such a simple post, then you don't understand Oh come on, that is a weak strawman even for you! Show me one way in which my post can be interpreted as that; then look at...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.