arrays again

E

Eric Eggermann

Thanks for the help last week, but I've got a new problem with a different
array now.....sigh.
I want to use an ArrayList to hold unique integer arrays. I've written a
comparer class which works when I call the function explicitly, but when I
use the Sort and BinarySearch functions of my ArrayList object I get
InvalidCast exceptions.

public int Compare(object x, object y)
{
return Compare((Array)x, (Array)y); //InvalidCast exception here
}
public int Compare(Array x, Array y)
{
.... comparison code here is working....
}

Of course, the ArrayList only contains int[] arrays. Also, the above code is
just one of many equally useless tries I've had at this. How do I cast
between an object reference and an array of ints, so that I can use my
comparer with ArrayList.Sort and BinarySearch functions?

All ideas well appreciated,
Eric
 
J

Jon Skeet [C# MVP]

Thanks for the help last week, but I've got a new problem with a different
array now.....sigh.
I want to use an ArrayList to hold unique integer arrays. I've written a
comparer class which works when I call the function explicitly, but when I
use the Sort and BinarySearch functions of my ArrayList object I get
InvalidCast exceptions.

public int Compare(object x, object y)
{
return Compare((Array)x, (Array)y); //InvalidCast exception here
}
public int Compare(Array x, Array y)
{
.... comparison code here is working....
}

Of course, the ArrayList only contains int[] arrays. Also, the above code is
just one of many equally useless tries I've had at this. How do I cast
between an object reference and an array of ints, so that I can use my
comparer with ArrayList.Sort and BinarySearch functions?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
N

Nicholas Paldino [.NET/C# MVP]

Eric,

Place a break in the debugger, and see what the types of the variables x
and y actually are. You might find that it is something you didn't expect.
 
E

Eric Eggermann

Nicholas Paldino said:
Eric,

Place a break in the debugger, and see what the types of the variables x
and y actually are. You might find that it is something you didn't
expect.

I have, and I find that both x, and y in Compare(object x, object y) are
System.Int32. Didn't really expect that. And the GetType().IsArray returns
false. So I'm still confused.


Eric
 
E

Eric Eggermann

Could you post a short but complete program which demonstrates the
problem?

While putting that demo program together, the silly thing seemed to work.
Still doesn't work in the place where I actually need it, but the problem
seems to be somewhere else. Got some work to do.

Thanks for the help, and I'll post that program when I manage to figure out
where the problem lies.

Eric
 
M

Marko Becirevic

Eric Eggermann > said:
message news:[email protected]... variables
expect.

I have, and I find that both x, and y in Compare(object x, object y) are
System.Int32. Didn't really expect that. And the GetType().IsArray returns
false. So I'm still confused.

maybe you send ints or pointers to beginning of an int array, not to the
beginning of an Array class.
Check code from where you call this method.
 
S

Simon Smith

maybe you send ints or pointers to beginning of an int array, not to the
beginning of an Array class.
Check code from where you call this method.

Sounds like you're comparing the contents of the int[] arrays, not the
int[] arrays them selves.
As a tip, I find code like

Compare((Array)x, (Array)y)

very hard to debug. It's no slower than say:

Array ax = (Array)x;

Array ay = (Array)y;

Compare(ax, ay);

and this version of it is more explicit about where the error occurs and
allows you to actually examine the results of the casts before they're
used.


Simon Smith
simon dot s at ghytred dot com
http://www.ghytred.com/NewsLook <http://www.ghytred.com/NewsLook> - Usenet
for Outlook
 
E

Eric Eggermann

Thank for all the replies. They were much help and showed me that the bug
was in a different part of the program. It turned out that the comparer was
working, but I had used it with the wrong sort of object. I'd used it in
sorting an array of ints instead of an array of int arrays as it was
intended. Stupid oversight, but I'd needed your questions to help me find
that.

Cheers,
Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top