In<T>(T x, params T[] y) where T:struct, IComparable<T>

M

Michel Walsh

Question:

public static Boolean In<T>(T x, param T[ ] y) where T : struct,
IComparable<T>
{
for(int i=0; i<y.Length; i++)
{
if(x==y) return true;
}
return false;
}


generates the compilation error that == cannot be applied to operands of
type 'T' and 'T'...


Can it be corrected? (Cannot use Contains and a list, at that point, the
snippet is just a simplified illustration)



Vanderghast, Access MVP
 
T

Tom Shelton

Michel said:
Question:

public static Boolean In<T>(T x, param T[ ] y) where T : struct,
IComparable<T>
{
for(int i=0; i<y.Length; i++)
{ if (((IComparable
}
return false;
}
static bool In<T> (T x, params T[] args) where T : struct
{
for (int i = 0; i < args.Length; i++)
{
if (((IComparable<T>)x).CompareTo (args) == 0)
return true;
}

return false;
}
 
W

William Stacey [MVP]

o... x.Equals(y)

--
William Stacey [MVP]

|
| Michel Walsh wrote:
| > Question:
| >
| > public static Boolean In<T>(T x, param T[ ] y) where T : struct,
| > IComparable<T>
| > {
| > for(int i=0; i<y.Length; i++)
| > {
| if (((IComparable
| > }
| > return false;
| > }
| >
| >
| static bool In<T> (T x, params T[] args) where T : struct
| {
| for (int i = 0; i < args.Length; i++)
| {
| if (((IComparable<T>)x).CompareTo (args) == 0)
| return true;
| }
|
| return false;
| }
|
| --
| Tom Shelton
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top