PDA

View Full Version : Solved: Comparison type mismatch



ben.oates
03-25-2008, 11:16 AM
Hello all,

I come here in desperation (after 2 hours on what should be a very simple bit of code!). I'm sure I must be missing something simple, but I really can't see what it is.

I have a custom type "orderHardware" and I want to find the location of an item in array of that custom type, but I get a Type Mismatch on the equals operator in the If statement below. Please see if you can offer any suggestions on this. I searched relentlessly for information on comparing custom types and came up empty.



Public Function hardwareExists(arr() As orderHardware, ordHardware As orderHardware) As Integer
For hardwareExists = LBound(arr) To UBound(arr)
'This is where the error occurs
If arr(hardwareExists) = ordHardware Then Exit Function
Next hardwareExists
End Function


Thanks for your help guys.

OTWarrior
03-26-2008, 02:24 AM
What type of field is orderHardware? is it a string or a numerical value?

ben.oates
03-26-2008, 02:42 AM
Hi OTWarrior,

orderHardware is my custom type:


Type orderHardware
HID As Integer
HType As String
HQuantity As Integer
PO As String
End Type

Entities in the arr() are all of this type, as is ordHardware so I don't understand why I can't compare them. I don't know if it is possible to compare custom types. Am I going to have to compare each component of the type individually do you think?

ben.oates
03-26-2008, 04:52 AM
Never mind guys... HID should be unique. I'll just compare that. Can't believe I didn't think of it earlier!

:bonk:

I haven't closed the thread because if anyone does know what I did wrong in comparing the custom type I would still be interested to know.

akn112
03-28-2008, 07:07 AM
i'm gonna venture a guess that vba is expecting known types of variables when comparing with the = operator. I say this because when working in C# (or C++) if you want to compare two linked lists (commonly used custom objects) you have to create an override function of the = operator that is used in place of the standard = function. Within this function you have to compare all aspects of the linked lists (ie: nodes, values, pointers, or in this case, HID, Htype, etc...) I'm not sure if there is a way to create an override function in vba though.

You could always just create an IsEqual() boolean function which compares each property of your object

ben.oates
03-28-2008, 07:15 AM
Yeah, I came to a similar conclusion - but without polymorphism I would have to create an xxxIsEqual for each of my custom types (that's not the only one) and it all starts to get more messy. I only need to run the check once so it's ok.

Fear not. I have fudged it! Thanks for your reply though.