-2

im using a class called PriorityQueue and like the name says it should compare elements and ordain them this is the Comparer class inside priority Queue

private class DefaultComparer : IComparer
        {
            #region IComparer Members

            public int Compare(object x, object y)
            {
                #region Require

                if(!(y is IComparable))
                {
                    throw new ArgumentException(
                        "Item does not implement IComparable.");
                }

                #endregion

                IComparable a = x as IComparable;

                Debug.Assert(a != null);

                return a.CompareTo(y);
            }

            #endregion
        }

and this is what im comparing

class Coordenada : IComparable
    {
        public int x;
        public int y;
        public float heuristico;

        int IComparable.CompareTo(object coord1)
        {
            Coordenada c1 = (Coordenada)coord1;
            //Coordenada c2 = (Coordenada)coord2;

            if(c1.heuristico < heuristico)
                return 1;
            if(c1.heuristico > heuristico)
                return -1;

            return 0;
        }
    }

the error as i said in the title is: Cannot cast from source type to destination type i am aware that Coordenada and object are not the same so thats why i tried the cast and well it doesnt work any idea on what should i do?

Edit: this is how im using the priority queue thats suppossed to use the function CompareTo inside Coordenada

    Coordenada c;
    PriorityQueue cola = new PriorityQueue();

    c.x = ax;
    c.y = ay;
    c.heuristico = distancia;
    cola.Enqueue(c)

The priorityQueue is a list,im adding to that list 2-3 different Coordenada objects in a while, because im searching for the lowest number in each cycle and removing it from my list until i get where i want

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Makenshi
  • 993
  • 3
  • 15
  • 28
  • What line is giving the error? Since you haven't shown the code that is using this comparer, it may very well be that objects are being passed that are not of type Coordenada. – Gambit Jul 31 '12 at 21:24
  • the error is in this line if(c1.heuristico < heuristico) when comparing as i said kinda logic because one is the type coordenada and the other is object but IComparable wont let me use other thing than object – Makenshi Jul 31 '12 at 21:26
  • Error is probably line before you said. Debugger just breaks on line after the one that caused exception. – Nikola Radosavljević Jul 31 '12 at 21:40
  • i do believe the error is there tho because it is saying that it cannot compare object with coordenada or so you can see this different you cannot compare object with float or int because they are not the same type of object – Makenshi Jul 31 '12 at 21:44

2 Answers2

1

Change your CompareTo method. It is accepting object as a parameter, so it should be able to handle other things which are not Coordenada also. If object that Coordenada is being compared to, is not of same type, just return appropriate value (may be -1, 1, 0, depends on your logic). You could try like this:

int IComparable.CompareTo(object coord1)
{
    Coordenada c1 = coord1 as Coordenada;
    if (c1 == null)
        return -1;

    if(c1.heuristico < heuristico)
        return 1;
    if(c1.heuristico > heuristico)
        return -1;

    return 0;
}

It would be even better if you did not compare objects which are of completely different nature.

Nikola Radosavljević
  • 6,871
  • 32
  • 44
0

if you are sure coord1 is Coordenada type use the explicit cast as

Coordenada c1 = coord1 as Coordenada;

throwing an InvaildCastException if c1 is null.

Riccardo
  • 1,490
  • 2
  • 12
  • 22
  • Can you explain more detailed the problem? Are you using two different istances of Coordenada class and trying to compare them using CompareTo method you implemented? what do you use DefaultComparer to? – Riccardo Jul 31 '12 at 21:28
  • use IComparable instead of IComparable. In this way you haven't to do any cast inside your class. It's the generic interface IComparable – Riccardo Jul 31 '12 at 21:34
  • Your answer is strangely more confusing than his question. – Nikola Radosavljević Jul 31 '12 at 21:36
  • i tried doing that but i get 2 errors Coordenada.IComparable.CompareTo(object): containing type does not implement interface 'System.IComparable' and 'System.IComparable.CompareTo' in explicit interace declaration is not a member of interface – Makenshi Jul 31 '12 at 21:42
  • if you implement IComparable you must implement the method that has this signature: int CompareTo(Coordenada other); and not int CompareTo(object other), did you? – Riccardo Jul 31 '12 at 21:47