Azurewrath

Hi all,

I wrote this method for my vector3 class:

public int CompareTo ( object obj )
{
Vector3 that = obj as Vector3;
if ( that != null )
{
return this.Length.CompareTo ( that.Length );
}
throw new ArgumentException ( "Object is not a Vector3" );
}

public static bool operator == ( Vector3 v1, Vector3 v2 )
{
return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
}

When I try this on a List, it gives nullreference exception saying "Object reference not set to an instance of an object.". I thought this was because the List wasn't using the full capacity, then I set it to 4, where 4 vectors are added later, same error.

How can I solve this Should I include null statements in the CompareTo method




Thanks,
Aw


Re: Visual C# Language CompareTo method

Kea

Your CompareTo(object) method is broken, because you always throw an exception: Consider enclosing the throw statement in an else-block following the if-block.
For your null-reference problem, can you give some more information on what your code is trying to do before the error occurs If you're using List.Sort(), it will only work if the vector class implements IComparable[<Vector3>]. Or haven't you assigned a reference to a list object to your list variable




Re: Visual C# Language CompareTo method

Azurewrath

Thanks for replying.

Are you sure it's gonna throw and exception everytime Because I thought it will only do so if obj is null.

But when you said I have to implement IComparable<Vector3>, I did it and it worked. Initially I only implemented for IComparable.

So I always have to use
IComparable<Type>





Thanks,
Aw




Re: Visual C# Language CompareTo method

Roland Sommer

Hi,

yes, the exception will always be thrown. You will have to put it into the else part of the if.

To your second question: Depends,

IList<T> will use ICompareable<T>.CompareTo to sort items.

IList will use ICompareable.CompareTo to sort items.

So if you would like to support sorting instances of your class in a generic collection you will have to implement the generic version of the interface. Same thing with the nongeneric collections, implement the nongeneric interface to support sorting your objects stored in that kind of collections.

Of course you can implement both interfaces explicitly and simply implement the generic version by calling the nongeneric version

int IComparable<T>.CompareTo(T other)

{

return ((IComparable)this).CompareTo(other);

}

Roland





Re: Visual C# Language CompareTo method

Azurewrath

Thanks Roland. it makes alot of sense.

But for the first answer, I am still not sure. I have been told return causes an early exit from the method and ignores the rest. Isn't this the case for the code I posted

Because I know people are doing it with multiple returns one after the other without using elses, etc.




Thanks,
Aw




Re: Visual C# Language CompareTo method

Kea

Sorry, I misread your code . With the return statement is seems okay.




Re: Visual C# Language CompareTo method

Roland Sommer

Oh, now that you mention ...

Of course you are right, the exception will only be thrown if that==null.

I don't know why I hadn't seen the return statement or whatever let me post the nonsense about the exception, sorry for the confusion!





Re: Visual C# Language CompareTo method

Azurewrath

Thanks guys! No worries. I am glad that it wasn't doing what I haven't expected (other than the above error of course Smile)




Thanks,
Aw