[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2006-11-06
5
Medium Priority
?
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
joechina earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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