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 4 1103
<"Eric Eggermann" <<none>>> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Eric Eggermann >" <<none> wrote in message
news:uL****************@tk2msftngp13.phx.gbl... 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
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eb**************@tk2msftngp13.phx.gbl... 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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Canonical Latin |
last post by:
"Leor Zolman" <leor@bdsoft.com> wrote
> "Canonical Latin" <javaplus@hotmail.com> wrote:
>
> > ...
> >But I'm still curious as to the rational of having type
>...
|
by: Matteo Settenvini |
last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to...
|
by: Charles Banas |
last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to
get some opinions and comments on it, hopefully so i can gain some
sort of insight as to why this works.
at the top of the...
|
by: masood.iqbal |
last post by:
All this time I was under the illusion that I understand the concept of
multi-dimensional arrays well ---- however the following code snippet
belies my understanding.
I had assumed all along...
|
by: SK |
last post by:
Hi
I appreciate all of the feedback I have recieved. I am enjoying
working on my program and I hope that it can be appreciated. I have my
program compiling now and I am continuing to work out...
|
by: Peter Seaman |
last post by:
I understand that structures are value types and arrays and classes are
reference types. But what about arrays as members of structures i.e. as in
C
struct x
{
int n;
int a;
}
|
by: Joseph Lee |
last post by:
Hi All,
I am having problem when i am using hashtable to keep an array of bytes
value as keys.
Take a look at the code snippet below
---------------------------------------------------
...
|
by: Rene Nyffenegger |
last post by:
Hello everyone.
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in...
|
by: Christian Maier |
last post by:
Hi
After surfing a while I have still trouble with this array thing. I
have the following function and recive a Segmentation fault, how must
I code this right??
Thanks
Christian Maier
|
by: sanjay.vasudevan |
last post by:
Why are the following declarations invalid in C?
int f();
int f();
It would be great if anyone could also explain the design decision
for such a language restricton.
Regards,
Sanjay
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |