IComparable

  • Thread starter Thread starter sahel
  • Start date Start date
S

sahel

hi all .
i inheritance a class from IComparable but
class employee : IComparable // has error*******
{
public int CompreTo(object obj)
{
}
}
but it has an error : 'employee' does not implement interface member
'System.IComparable.CompareTo(object)'
 
hi all .
i inheritance a class from IComparable  but
class employee : IComparable // has error*******
{
    public int CompreTo(object obj)
    {
    }}

but it has an error : 'employee' does not implement interface member
'System.IComparable.CompareTo(object)'

it was my program :
using System;
using System.Collections.Generic;

class Program
{
static void Main(string[] args)
{
employ e1, e2;
e1 = new employ();
e1.id = 200;
e2 = new employ();
e2.id = 10;
Console.WriteLine(e1.CompreTo(e2));
}
}

class employ : IComparable // here has error **********
{
public int CompreTo(object obj)
{
employ e = (employ)obj;
if (this.id > ((employ)obj).id)
return 1;
if ((this.id )==((employ)obj).id)
return 0 ;
return -1;
}
}
 
hi all .
i inheritance a class from IComparable  but
class employee : IComparable // has error*******
{
    public int CompreTo(object obj)
    {
    }}
but it has an error : 'employee' does not implement interface member
'System.IComparable.CompareTo(object)'

it was my program :
using System;
using System.Collections.Generic;

    class Program
    {
        static void Main(string[] args)
        {
            employ e1, e2;
            e1 = new employ();
            e1.id = 200;
            e2 = new employ();
            e2.id = 10;
            Console.WriteLine(e1.CompreTo(e2));
        }
    }

class employ : IComparable  // here has error **********
{
    public int CompreTo(object obj)
    {
        employ e = (employ)obj;
        if (this.id > ((employ)obj).id)
            return 1;
        if  ((this.id )==((employ)obj).id)
            return 0 ;
        return -1;
    }

}

i think the name id never exist in this context
------->protected int id ;
 
it was my program :
using System;
using System.Collections.Generic;
    class Program
    {
        static void Main(string[] args)
        {
            employ e1, e2;
            e1 = new employ();
            e1.id = 200;
            e2 = new employ();
            e2.id = 10;
            Console.WriteLine(e1.CompreTo(e2));
        }
    }
class employ : IComparable  // here has error **********
{
    public int CompreTo(object obj)
    {
        employ e = (employ)obj;
        if (this.id > ((employ)obj).id)
            return 1;
        if  ((this.id )==((employ)obj).id)
            return 0 ;
        return -1;
    }

i think the name id never exist in this context
------->protected int id ;

i change your program like this was it the result that u wanted ?

using System;
using System.Collections;
class employ : IComparable
{
protected int b;
public int CompareTo(object obj)
{
employ a = obj as employ;
if (a != null)
//return this.b.CompareTo(a.b);
return 1;
else
throw new ArgumentException("!!!!!");
}
public int B
{
get
{
return this.b;
}
set
{
this.b= value;
}
}

}

public class program
{
public static void Main()
{
ArrayList c = new ArrayList();
employ e1, e2;
e1 = new employ();
e1.B = 200;
e2 = new employ();
e2.B= 10;
c.Sort();
foreach (employ temp in c)
Console.WriteLine(e1.B + e2.B);


}
}
 
nicol said:
[...]
class employ : IComparable  // here has error **********
{
    public int CompreTo(object obj)

If you want to implement IComparable, you have to spell the name of the
method "CompareTo" correctly.  You can't implement interfaces with
misspelled method names, such as "CompreTo".

Pete

thanks a lot
 
Back
Top