0

I'm working on a custom login Textbox that has an Auto Complete function. However, When i try to:

public static void SetSelectOnMouseOver(DependencyObject obj, bool value)
    {
        obj.SetValue(ListBoxItemBehavior.SelectOnMouseOverProperty, 
            (object)(bool)(value ? 1 : 0));
    }

I get an error (object)(bool)(value ? 1 : 0)); > cannot convert type 'int' to 'bool', what's wrong?

Enumy
  • 841
  • 2
  • 8
  • 20
  • 2
    You can not use casting for `int` to `bool`, you have an option to use [`Convert.ToBoolean`](http://msdn.microsoft.com/en-us/library/system.convert.toboolean(v=vs.110).aspx) which would **convert 0 to false and for non zero values it will return `true`**. But you can avoid all that and simply use `Value` parameter without any condition. At least that is what it looks like in your code. – Habib Sep 19 '14 at 13:27
  • 3
    I understand its a silly question, but I see no reason to downvote. – DidIReallyWriteThat Sep 19 '14 at 13:30
  • @Habib No reason that's right but i have got to make it visual to others for hard review or even on paper. – Enumy Sep 19 '14 at 13:31
  • @Hossam, not really sure how much readable that would be , I mean you are using a bool -> then returning integer values -> then converting it back to bool. – Habib Sep 19 '14 at 13:33
  • @Habib ohh about that yes, i just stated down there that it was stupid.. didn't realize that until `David` pointed it out. – Enumy Sep 19 '14 at 13:36

1 Answers1

7

value is already a bool. Why not just use (object) value, or even just value?

David Crowell
  • 3,711
  • 21
  • 28
  • Moreover, since all types in CLR are derived from `System.Object`, it should be simply `value` without explicit cast. Compiler will implicitely cast it, if nessesary. – Andrey Korneyev Sep 19 '14 at 13:45
  • 1
    I just answered that up there "No reason that's right but i have got to make it visual to others for hard review or even on paper." – Enumy Sep 19 '14 at 13:48