IComparer issues

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 22
Reputation: sparksterz is an unknown quantity at this point 
Solved Threads: 1
sparksterz sparksterz is offline Offline
Newbie Poster

IComparer issues

 
0
  #1
Oct 19th, 2009
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.

  1.  
  2. internal class FreePtrCompare : IComparer<File>
  3. {
  4. int IComparer<File>.Compare(File x, File y)
  5. {
  6. int a = x.fsPos; //Free space pointer of file 1
  7. int b = y.fsPos; //Free space pointer of file 2
  8. return a.CompareTo(b);
  9. }
  10. }

  1. File [] takenSpace = HardDrive; //HardDrive is also a Array of Files
  2. 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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
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?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
0
  #3
Oct 19th, 2009
Originally Posted by sparksterz View Post
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.
If File y is null, what do you expect to happen? What is y.fsPos supposed to do when y is null? (Hint: it throws an exception.)

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: sparksterz is an unknown quantity at this point 
Solved Threads: 1
sparksterz sparksterz is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
0
  #5
Oct 19th, 2009
The "second" file from the list? Are you saying you're expecting Sort to look at the elements of the list in some particular order?
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: sparksterz is an unknown quantity at this point 
Solved Threads: 1
sparksterz sparksterz is offline Offline
Newbie Poster
 
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.
Attached Files
File Type: zip FileSys.zip (38.2 KB, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
2
  #7
Oct 19th, 2009
Originally Posted by sparksterz View Post
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?
There's no such thing as a 'blank space.' An array with 64 elements will contain 64 elements. Some of them will be null, because that's what they were initialized with -- but that's no different than if you had assigned null to any particular element. Array.Sort will try to sort the entire array.

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.
Yes. You shouldn't preallocate arrays and then put several values in them, you should just use List<>.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: sparksterz is an unknown quantity at this point 
Solved Threads: 1
sparksterz sparksterz is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC