473,549 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum and generic IComparable interface

Hi everybody,

when migrating to the .NET 2.0 framework we decided to encourage the use
of generic functions because they ought to run faster than their
equivalent object-implemantations .

Now I have the following problem:
I adapted my common functions to use the generic implementations of the
..NET-classes and interfaces. Now I got problems when calling my
functions with enum-types:

enum TypeOfAction {
Delete = 1,
Copy = 2,
Move = 3,
Cut = 4
}

// Find all entries with specified key and return corresponding values
public static V[] ExtractArrayEnt ries <T, V>
(T[] keys, V[] values, T key)
{
// next line throws with "Unable to cast ... 'TypeOfAction'
// to 'IComparable<Ty peOfAction>'
IComparable<T> comparableValue = (IComparable<T> ) key;
[...]
if (comparableValu e.CompareTo(key s[i]) == 0) retArray[j] = values[i];
[...]
}

When I use normal int's, it works fine. The old version with the
object-typed version of IComparable also worked fine with Enums.

Is there I special reason I don't see why MS didn't fit the Enum-class
with the IComparable<T>-interface?
Is there a workaround to solve this problem?

TIA,
Stefan
Nov 28 '05 #1
2 6261
Stefan L wrote:
Hi everybody,

when migrating to the .NET 2.0 framework we decided to encourage the use
of generic functions because they ought to run faster than their
equivalent object-implemantations .


Try using IEquatable<T> instead of IComparable<T> in this case.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Nov 28 '05 #2
Wasn't appropriate in my case because I was performing a binary search
on a sorted array, so I had to perform a real comparison rather than a
check for equality.
I might have oversimplified the scenario in my example, sorry for that.

I now worked around this issue by using Comparer<T>.Def ault.Compare(),
which automatically switches to use the non-generic
IComparable-interface. No perfomance gain here I guess...

Thanx,
Stefan
Lasse Vågsæther Karlsen schrieb:
Stefan L wrote:
Hi everybody,

when migrating to the .NET 2.0 framework we decided to encourage the
use of generic functions because they ought to run faster than their
equivalent object-implemantations .


Try using IEquatable<T> instead of IComparable<T> in this case.

Nov 28 '05 #3

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

Similar topics

17
3298
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even...
5
5417
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the generic.List.IndexOf function doesn't appear to be invoking the contained class' CompareTo method. My understanding is that it should. The contained class...
1
4716
by: ProjectNut | last post by:
what is the syntax for implementing an interface in a generic class that has a constrained base class example: class myclass<T>: mybase<T> where T: IComparable { } Now, I want myclass<T> to implement IMyInterface. What is the syntax
1
1237
by: Ning Hu | last post by:
Hello, I have a question on why I am getting an error of "Cannot implicitly convert type 'testGenerics.testCollection' to 'testGenerics.IMyCollection<System.IComparable>'. An explicit conversion exists (are you missing a cast?)" for the following code. Since, a Double implements IComparable, It should be implicitly castable no? ...
9
12798
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The...
1
1916
by: Anthony Paul | last post by:
Hello everyone! Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following generic interface : *note : in reality I implement the IComparable and IEquatable generic interfaces, constraints, and associated...
15
7078
by: Anthony Paul | last post by:
Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following generic interface : *note : in reality I implement the IComparable and IEquatable generic interfaces and associated overriden methods, but I've cut...
3
4378
by: =?Utf-8?B?R0I=?= | last post by:
I have created a small program that illustrates the problem. I would know how to address the fields that I want to sort on in the greaterThan comparison. Anybody who knows?? using System; class Program { public List<itemitList, index1;
6
2702
by: Jon | last post by:
Hi, When I try to compile the following generic class, the compiler gives me many errors "Cannot conver type '...' to '...'" on the lines indicated. Besides, the C# compiler gives me errors even if I don't declare and instantiate C_Filter_MA<Ti,Tsin my program. Ti and Ts should be one of {Byte, UInt16, UInt32, UInt64, SByte, Int16, Int32,...
0
7998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7517
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7844
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6098
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5401
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5126
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1978
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1093
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.