Solved

cannot convert from 'T' to 'sbyte' using Generics

Posted on 2006-11-06
5
640 Views
Last Modified: 2008-02-01
Here's my function:

static public void SetMin<T>(ref T a, T b) {  // Invalid - Cannot convert from'T' to sbyte
      a = Math.Min(a, b);
}

Visual Studio 2005 gives the errors:
The best overloaded method match for 'System.Math.Min(sbyte, sbyte)' has some invalid arguments
Argument '1': Cannot convert from 'T' to 'sbyte'
Argument '2': Cannot convert from 'T' to 'sbyte'

Why is this?  If I unwrap this method and declare directly with sbyte, the compiler is fine with it:

static public void SetMin(ref sbyte a, sbyte b) {  // Works fine
      a = Math.Min(a, b);
}
0
Comment
Question by:rschaaf
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
joechina earned 125 total points
ID: 17881295
Math.Min has many overloaded methods but all accepts number type parameters.
When compiling your generic function, the compiler doesn't know the type T. There is no way to compile this.

Try this:

static public T GetMin<T>(T a, T b) where T : IComparable
        {
            if (a.CompareTo(b) <= 0)
                return a;
            else
                return b;
        }
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17881297
I don't know way to do this using Math function, but this one can be used as replacement:

        static public void SetMin<T>(ref T a, T b) where T :IComparable<T>
        {
            a = a.CompareTo(b) < 0 ? a : b;
        }
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17881304
Oops, sorry for duplicated post.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17881315
Actually, it is not 100% duplicated, because I use IComparable<T> :)
0
 
LVL 13

Expert Comment

by:joechina
ID: 17881641
True, IComparable<T> is better.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now