Comparing objects of different types
-
In my application I have an array of custom objects that I want to search by one of the properties of the custom object (the ID property which is an int), please see the code below to understand what I'm trying to do:
private CustomObject[] _customObjects;
public CustomObject CustomObjects
{
get
{
if(_customObjects == null)
{
CustomObject[] customObjects = CustomObject.GetCustomObjects();
Array.Sort(customObjects); // CustomObject implements IComparable
_customObjects = customObjects;
}
}public IsIDInList(int id)
{
// What do I have to do to make this work?
return Array.BinarySearch(CustomObjects, id) >= 0;
}I could use a dictionary of course but I don't want to for some reasons specific to my application. Any suggestions are highly appreciated...
Waleed Eissa Software Developer Sydney
-
In my application I have an array of custom objects that I want to search by one of the properties of the custom object (the ID property which is an int), please see the code below to understand what I'm trying to do:
private CustomObject[] _customObjects;
public CustomObject CustomObjects
{
get
{
if(_customObjects == null)
{
CustomObject[] customObjects = CustomObject.GetCustomObjects();
Array.Sort(customObjects); // CustomObject implements IComparable
_customObjects = customObjects;
}
}public IsIDInList(int id)
{
// What do I have to do to make this work?
return Array.BinarySearch(CustomObjects, id) >= 0;
}I could use a dictionary of course but I don't want to for some reasons specific to my application. Any suggestions are highly appreciated...
Waleed Eissa Software Developer Sydney
Hi, I suggest you have a look at this overload:
Array.BinarySearch Method (Array, Int32, Int32, Object, IComparer)
and if necessary read up on IComparer. :)Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
Hi, I suggest you have a look at this overload:
Array.BinarySearch Method (Array, Int32, Int32, Object, IComparer)
and if necessary read up on IComparer. :)Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
Hi, thanks for your reply, actually I know about IComparer and IComparable and I use them very often in my code but I never had the need to compare objects of different types, this is why I'm a little confused, I know it's not possible using the generic version of IComparer, are you referring to the non-generic version? A short code sample would very helpful, thanks a lot.
Waleed Eissa Software Developer Sydney
-
Hi, thanks for your reply, actually I know about IComparer and IComparable and I use them very often in my code but I never had the need to compare objects of different types, this is why I'm a little confused, I know it's not possible using the generic version of IComparer, are you referring to the non-generic version? A short code sample would very helpful, thanks a lot.
Waleed Eissa Software Developer Sydney
:confused: All your objects are of type CustomObject, aren't they? (either directly or by inheritance) If you can Sort the Array, why wouldn't you be able to BinarySearch it? :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
:confused: All your objects are of type CustomObject, aren't they? (either directly or by inheritance) If you can Sort the Array, why wouldn't you be able to BinarySearch it? :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
Hmm, seems you didn't read my post very carefully :), I have an array of CustomObject objects, CustomObject has a property named ID which is an int, I want to search the array for a custom object with that ID or in other words I want to be able to know whether there's a CustomObject in the array with that ID, I'm not interested in returning the object, I just want to know whether it's in the array or not. Regards
Waleed Eissa Software Developer Sydney
-
Hmm, seems you didn't read my post very carefully :), I have an array of CustomObject objects, CustomObject has a property named ID which is an int, I want to search the array for a custom object with that ID or in other words I want to be able to know whether there's a CustomObject in the array with that ID, I'm not interested in returning the object, I just want to know whether it's in the array or not. Regards
Waleed Eissa Software Developer Sydney
:confused::confused: Array.BinarySearch will look for an element with a specific Property, provided your IComparer is based on that same Property, hence the Array must be Sorted accordingly. And it will return a positive index when found, or a negative number when not found. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
:confused::confused: Array.BinarySearch will look for an element with a specific Property, provided your IComparer is based on that same Property, hence the Array must be Sorted accordingly. And it will return a positive index when found, or a negative number when not found. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
I only have the value of the ID property (have a look on the code), I don't have the object, can I pass objects of different types to IComparer.Compare()?
Waleed Eissa Software Developer Sydney
-
I only have the value of the ID property (have a look on the code), I don't have the object, can I pass objects of different types to IComparer.Compare()?
Waleed Eissa Software Developer Sydney
Hi, if your IComparer only looks at the object's ID, you could pass it a dummy object with the ID value you are looking for. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
Hi, if your IComparer only looks at the object's ID, you could pass it a dummy object with the ID value you are looking for. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
Hmm, so this means to create an object and assign it the value I want for the ID just for the sake of passing to the IComparer, is my understanding correct? So, as far as I can see, comparing objects of different types is not directly supported by the framework, is this correct?
Waleed Eissa Software Developer Sydney
-
Hmm, so this means to create an object and assign it the value I want for the ID just for the sake of passing to the IComparer, is my understanding correct? So, as far as I can see, comparing objects of different types is not directly supported by the framework, is this correct?
Waleed Eissa Software Developer Sydney
Waleed Eissa wrote:
to create an object and assign it the value I want for the ID
that was the idea yes
Waleed Eissa wrote:
comparing objects of different types is not directly supported
objects of different types are different by definition, no need to compare them. this is not related to any framework, they differ by definition in OO. chairs aren't tables. If your different types have a useful common ancestor (say furniture) you should use that as the basis for your logic. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|