GetType() and Exception is used to compare the objects. This is not a usual format. I couldn't understand this. Anyone knows explain please. Problem is highlighted.
namespace nsForEach
{
using System;
public class ClassArr
{
static public void Main()
{
clsElement[] Arr = new clsElement[]{new clsElement (8), new clsElement (3), new clsElement (12), new clsElement (7)};
Console.WriteLine("Unsorted:");
for (int x = 0; x < Arr.Length; ++x)
Console.WriteLine("The instance number is {0}", Arr[x].getInstance());
Array.Reverse(Arr);
Console.WriteLine("\r\nIn Reverse order:");
for (int x = 0; x < Arr.Length; ++x)
Console.WriteLine("The instance number is {0}", Arr[x].getInstance());
Array.Sort(Arr);
Console.WriteLine("\r\nSorted:");
for (int x = 0; x < Arr.Length; ++x)
Console.WriteLine("The instance number is {0}", Arr[x].getInstance());
Console.ReadKey();
}
}
class clsElement : IComparable
{
int instance;
public clsElement(int instance)
{
this.instance = instance;
}
public int getInstance()
{
return this.instance;
}
public int CompareTo(object o)
{
if (o.GetType() != this.GetType())
throw (new ArgumentException());
clsElement elem = (clsElement)o;
return (this.instance - elem.instance);
}
}
}
/*
Unsorted:
The instance number is 8
The instance number is 3
The instance number is 12
The instance number is 7
In Reverse order:
The instance number is 7
The instance number is 12
The instance number is 3
The instance number is 8
Sorted:
The instance number is 3
The instance number is 7
The instance number is 8
The instance number is 12
*/
Loading
Posted Feb 24, 2012, 11:46 AM
VulpesPosted Feb 24, 2012, 11:27 AM
Posted Feb 24, 2012, 11:04 AM
In my view "o" is an object type and o = "I'm going to force an exception here"; is a string type. Therefore types are not compatible. I wish to know whether my understanding is correct or not.
VulpesPosted Feb 24, 2012, 10:09 AM
If you omit that code, it will be an InvalidcastException rather than an ArgumentException. One advantage of the latter is that it gives you the opportunity to tailor the error message.
If you want to see what the error messages would be in either case, then insert the following as the first line of the CompareTo method:
o = "I'm going to force an exception here";
Posted Feb 24, 2012, 9:56 AM
I wish to know whether the following step is necessary:
if (o.GetType() != this.GetType())
throw (new ArgumentException());
VulpesPosted Feb 24, 2012, 9:27 AM
a positive number if this.instance is greater than elem.instance
a negative number if this.instance is less than elem.instance (**)
zero if they're equal.
The new approach returns 1, -1 and 0 for these cases. These, of course, are positive, negative and zero numbers respectively.
When doing comparisons using the IComparable.CompareTo() method, it doesn't matter how big or small the numbers returned are. All that matters is their sign. See:
http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx
(**) actually it doesn't, it returns zero because there's a bug in the code!
Posted Feb 24, 2012, 8:20 AM
public int CompareTo(object o)
{
int returnVal;
clsElement elem = (clsElement)o;
if (this.instance > elem.instance)
returnVal = 1;
else
if (this.instance > elem.instance)
returnVal = -1;
else
returnVal = 0;
return returnVal;
}
VulpesPosted Feb 24, 2012, 4:58 AM
This is precisely what the code marked in yellow is doing before going on to make the comparison.
Jignesh TrivediPosted Feb 23, 2012, 11:10 PM
try to use Equal
o.GetType().Equals(this.GetType())
if "this.GetType()" is know type then use
(o as type) if you get null than both are differnt type.
hope this help.