| | |
IComparer issues
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 22
Reputation:
Solved Threads: 1
I've used IComparer before but I seem to be getting an odd issue where I'm not getting the second argument to my Comparer function.
Basically as soon as it gets to the "Array.Sort" line it crashes where "int b" is declared in the "FreePtrCompare" class saying that File y is null object/pointer reference. which it is, but I'm not entirely sure why that isn't being passed through correctly.
Has anyone else run into this issue before?
C# Syntax (Toggle Plain Text)
internal class FreePtrCompare : IComparer<File> { int IComparer<File>.Compare(File x, File y) { int a = x.fsPos; //Free space pointer of file 1 int b = y.fsPos; //Free space pointer of file 2 return a.CompareTo(b); } }
C# Syntax (Toggle Plain Text)
File [] takenSpace = HardDrive; //HardDrive is also a Array of Files Array.Sort(takenSpace, new FreePtrCompare());
Basically as soon as it gets to the "Array.Sort" line it crashes where "int b" is declared in the "FreePtrCompare" class saying that File y is null object/pointer reference. which it is, but I'm not entirely sure why that isn't being passed through correctly.
Has anyone else run into this issue before?
0
#2 Oct 19th, 2009
That code won't compile and i'm not aware of where you are getting the property
fsPos from. Could you post a version of the code I can compile? 0
#3 Oct 19th, 2009
•
•
•
•
Basically as soon as it gets to the "Array.Sort" line it crashes where "int b" is declared in the "FreePtrCompare" class saying that File y is null object/pointer reference. which it is, but I'm not entirely sure why that isn't being passed through correctly.
Apologies for the snark.
You should modify your Compare method to explicitly handle the case where parameters are null.
Edit: or you could remove nulls from the array that you're sorting, if that's the right thing to do.
Last edited by Rashakil Fol; Oct 19th, 2009 at 7:21 am.
All my posts may be redistributed under the GNU Free Documentation License.
•
•
Join Date: Sep 2007
Posts: 22
Reputation:
Solved Threads: 1
0
#4 Oct 19th, 2009
That's the thing though. It crashes getting even the second file from the list. by default there are 5 elements in HardDrive, it gets the very first element when Compare is called, but it doesn't get the second elements for File y. It should just be traversing through the array and comparing them but for some reason it doesn't seem to be getting the next element.
•
•
Join Date: Sep 2007
Posts: 22
Reputation:
Solved Threads: 1
0
#6 Oct 19th, 2009
Here's my project. To run it you'll want to type in a name at the command prompt, then type "free space full" as that's the command that will start the sorting function.
I'm not so sure I expected the Array.Sort to go through any particular order, but I expected it to only compare spots in which a file actually existed. If I declare the array to be 64 spaces large, then only fill 5 of them, and tell it to do an Array.Sort does it sort even blank spaces?
If that's the case I guess I could just as easily push each one to a List<File> and then call Array.Sort on that.
I'm not so sure I expected the Array.Sort to go through any particular order, but I expected it to only compare spots in which a file actually existed. If I declare the array to be 64 spaces large, then only fill 5 of them, and tell it to do an Array.Sort does it sort even blank spaces?
If that's the case I guess I could just as easily push each one to a List<File> and then call Array.Sort on that.
2
#7 Oct 19th, 2009
•
•
•
•
I'm not so sure I expected the Array.Sort to go through any particular order, but I expected it to only compare spots in which a file actually existed. If I declare the array to be 64 spaces large, then only fill 5 of them, and tell it to do an Array.Sort does it sort even blank spaces?
•
•
•
•
If that's the case I guess I could just as easily push each one to a List<File> and then call Array.Sort on that.
All my posts may be redistributed under the GNU Free Documentation License.
•
•
Join Date: Sep 2007
Posts: 22
Reputation:
Solved Threads: 1
0
#8 Oct 19th, 2009
Yep, that sure did it! 
The only reason I was using an Array of pre-defined size was because the HardDrive acting as a real hard drive needed to have a pre-allocated size. I just figured that the compare method wouldn't try to compare null values in an array but it makes sense that it would.
Thanks a lot for the help.

The only reason I was using an Array of pre-defined size was because the HardDrive acting as a real hard drive needed to have a pre-allocated size. I just figured that the compare method wouldn't try to compare null values in an array but it makes sense that it would.
Thanks a lot for the help.
![]() |
Similar Threads
- ASP.NET and Mozilla - Issues! (ASP.NET)
- old issues with AGp 8x on ATI 9800/600Xt models? (Monitors, Displays and Video Cards)
- Counter issues (C)
- Redhat Enterprise and iowait issues (*nix Software)
- SuSE 9.1 - Minor issues (*nix Software)
- Error messages, Taskbar changes in XP + USB connectivity issues. (Windows NT / 2000 / XP)
- Login issues (Web Browsers)
- Mail apps issues - error port 25 (Mac Software)
- Security Issues (*nix Software)
- Graphics Card issues Stop Command??? (Windows NT / 2000 / XP)
Other Threads in the C# Forum
- Previous Thread: Chasing lights, running ants.
- Next Thread: select value from datagrid
| Thread Tools | Search this Thread |
.net access algorithm angle array barchart bitmap box broadcast buttons c# capturing check checkbox client combobox control conversion convert csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate development disappear draganddrop drawing encryption enum event excel file firefox form format forms function gdi+ httpwebrequest image index input install java label leak libraries list listbox mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml






