Error casting Integer()-typed arrays into Object()-typed parameters

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
This may be a silly question, but if I can send an Integer value into a sub/function whose parameter is typed Object, why can't I send an Integer() array into a sub/function whose parameter is typed Object() ? This throws this exception: "Unable to cast object of type 'System.Int32[]' to type 'System.Object[]'." Thank you very much for any help.
 
Let me explain my problem better, maybe it helps more.
I have this DialogBox, it works for me as a generic list selector for large lists of items (it has two listboxes side by side and buttons like "Add >>" and "<< Remove", and allows some smart text search with Regex).
It takes as arguments: a datatable which is the datasource, two strings that tell which fields are DisplayMember and ValueMember, and an array of Object() which represents those items which were previously selected. So each time the user presses a button that loads a given instance of this DialogBox, it sends this array in order to have the previous selections marked, so the DialogBox shows exactly how it did in the last time it was open for setup of those specific properties. So far, so good.
However, in spite of most of my datatables have Integer-typed primary key fields (the ValueMember), some few are Decimal-typed (because of big numbers involved) or even String-typed (because they are acronyms). Redesigning the database is out of question, so what to do? My first and only clue was assuming that an Object() parameter would hold seamlessly any underlying types whichsoever, but that doesn't seem to work this way. Is there anything I can do which is simpler that creating a battery of type-testing and iterative casting of elements? Thank you very much!
 
Last edited:
Argument type must match the parameter type, which means Object array and only that. The items in such an array can be any type.

One option is to convert the different type of arrays to Object array using Cast(of T) extension.
Another is to make it a generic method with a T array parameter. Generic Procedures in Visual Basic
 
Back
Top