Skip to main content

Valid Sort constructor call throws an ArgumentException

mdevol
Original Poster

In GeoTab.Checkmate.ObjectModel ver. 11.118.421, the following valid Sort constructor call throws an ArgumentException:

Sort sortOptions = new Sort(SortField.IdSortField, SortDirection.Asc, null, null);

This is because of a logic bug in the following Sort constructor code

 

public Sort(string sortBy, SortDirection? direction = global::Geotab.Checkmate.ObjectModel.SortDirection.Asc, string? offset = null, Id? lastId = null) { if (SortField.ImplementedSortFields.Contains<string>(sortBy, StringComparer.InvariantCultureIgnoreCase)) { throw new ArgumentException("Use existing implementation of ISort for this field"); }   SortBy = sortBy; SortDirection = direction; Offset = offset; LastId = lastId; }

In the if statement, SortField.ImplementedSortFields is a string array that contains all the valid sortBy field names, which means that the exception should only be thrown when Contains returns false. So, for this to work correctly, the return value of Contains should have been inverted, meanting the if statement should look lie this:

 

if (!SortField.ImplementedSortFields.Contains<string>(sortBy, StringComparer.InvariantCultureIgnoreCase)) { throw new ArgumentException("Use existing implementation of ISort for this field"); }

I've been able to work around this by passing an invalid sortBy value and then changing the SortBy property value

 

Sort sortOptions = new Sort("invalid", SortDirection.Asc, null, null);   sortOptions.SortBy = SortField.IdSortField;

A possible better implementation would have been to use an enumeration of valid sortable field names. That way, invalid values can't be passed in, and it would be caught at compile time instead of run time.

Join the conversation

You need to be logged in to reply to this post and participate in the Geotab Community discussions.

Top Answers

Hi @mdevol​,

 

I think the Exception being thrown there actually means what it says. There are existing implementations of `ISort` for supported fields like Id, and it expects you to use those instead:

var dateSort = new SortByDate(); var idSort = new SortById(); var nameSort = new SortByName(); var startSort = new SortByStart(); var stopSort = new SortByStop(); var versionSort = new SortByVersion();

In your case, you'd want to use SortById. The sort parameters you're passing are equivalent to the defaults, so you can use it as-is:

// Asc direction, null offset and null lastId are all default constructor parameters var sortOptions = new SortById();

 

3 Replies

Hi @mdevol​,

 

I think the Exception being thrown there actually means what it says. There are existing implementations of `ISort` for supported fields like Id, and it expects you to use those instead:

var dateSort = new SortByDate(); var idSort = new SortById(); var nameSort = new SortByName(); var startSort = new SortByStart(); var stopSort = new SortByStop(); var versionSort = new SortByVersion();

In your case, you'd want to use SortById. The sort parameters you're passing are equivalent to the defaults, so you can use it as-is:

// Asc direction, null offset and null lastId are all default constructor parameters var sortOptions = new SortById();

 

mdevol
Original Poster

Thanks for straightening me out. Apparently I didn't read down far enough in the documentation to find the C# example. Thanks for the help!

No problem! For future readers, mdevol is referring to the Sorting section of the Concepts guide, which covers some C# specifics:

 

image

Still have questions?