-1

I'm working on a binary search algorithm, which has the following parameters:

Now when I pass these arguments:

It says that type int cannot be used as a parameter F (I was under the impression that the generic types are not concerned with types that are being passed) and that there are no 'boxing' conversion from int to IComparable.

What I was trying to do: Basically, I wanted this method to accept search key's which can be of various numeric types (ints, doubles etc.) and so in the generic method I tried to declare two types.

About this code: The func delegate represents an object's property i.e. car.Name (string), car.Wheels(int) which are of different types. I Sort of want the key data type somehow be inferred based on the propertyFields type that's being passed, but that seems way too complicated, so I tried making it so that the F Key accepts various types and just make sure that I'm passing the correct types to it.

I don't know if this all sounds confusing, but if you have questions about any of my code, feel free to ask.

Edit: The error occurs when I call the BinarySearch Method.

Edit 2: for the propertyField i pass this as an argument: c => c.Longitude (or any other object property).

Vocaloidas
  • 360
  • 2
  • 17
  • Is the error at the line where you declare your method, or where you try to use your `key` parameter? Depending on what you're trying to do, you might need to add a restriction to have `F` implement `IComparable`. – krillgar Apr 12 '17 at 16:59
  • 3
    Is that the actual signature of your `BinarySearch` method? The error suggests you have a constraint on `F`. Could you add the implementation? – Lee Apr 12 '17 at 17:00
  • Yes the error occurs in the line where I call the method and pass the integer in the key parameter. I'll edit the post. – Vocaloidas Apr 12 '17 at 17:02
  • I edited the post; added the constraints which i forgot and clarified where the error exactly occurs. – Vocaloidas Apr 12 '17 at 17:04
  • What's the type of `propertyField`? – Lee Apr 12 '17 at 17:05
  • Why not use one generic and then the `key` and the return type of `targetProperty` would be that type instead? It doesn't make sense to search one type by another. – juharr Apr 12 '17 at 17:06
  • property field contains object properties which are chosen by the user. For example, I want the user to choose which property of the record object that they want to perform the search on i.e. Record.Month or Record.Magnitude. This is what i pass as an argument to that position: c => c.Longitude – Vocaloidas Apr 12 '17 at 17:12

1 Answers1

1

Either your 2nd constraint needs to be where F : IComparable<F> or you should not have a F at all and you should be taking in a T Key instead of a F Key

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • I think it's the latter. – juharr Apr 12 '17 at 17:08
  • That's actually how my initial implementation went, but then I get in trouble because the "type arguments cannot be inferred from the usage. Try specifying parameters explicitly (int IS explicit?)". My guess here is here that the problem of inference occurs when I use T in the func delegate, I can't really grasp how though. – Vocaloidas Apr 12 '17 at 17:09
  • If the T and the return type of the func possibly are different types would cause that error. Check the type of the func you are passing in is `Func` and not something else like `Func` – Scott Chamberlain Apr 12 '17 at 17:11
  • But I really don't want it to be explicit, because user should be able to choose which property of the record object to perform the BinarySearch on i.e. record.Day (int) or record.Magnitude(double). Copying and pasting the method and just changing types is insanity. – Vocaloidas Apr 12 '17 at 17:14