Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MvxEnumerableExtensions.GetPosition - The operator '==' is not having the expected result (IMHO) #309

Closed
zleao opened this issue Jun 11, 2013 · 5 comments
Labels
t/bug Bug type

Comments

@zleao
Copy link
Contributor

zleao commented Jun 11, 2013

In an Android App, I have a IDictionary binded to the ItemsSource of MvxSpinner and I also have a property binded to the SelectedItem of the spinner.
If I try to set the Selecteditem through the ViewModel, the code reaches the GetPosition method in the MvxEnumerableExtensions.
As the ItemsSource is not a IList, it reaches the following code (Line 58)

if (enumerator.Current == item) { return i; }

As the comparison is beeing made by two object, the operator '==' will use System.Object.ReferenceEquals. This will only return true, if the objects are the same instance. In my case apparently they are not the same instance but they are equal objects( if I use enumerator.Current.Equals(item) it returns true)

So that makes me think that we should use the Equals instead of the '==' operator.
My change sugestion is as follows:

            if (enumerator.Current == null)
            {
                if (enumerator.Current == item)
                {
                    return i;
                }
            }
            else
            {
                if (enumerator.Current.Equals(item))
                {
                    return i;
                }
            }
@slodge
Copy link
Contributor

slodge commented Jun 11, 2013

Thanks. This sounds familiar...

Linked to #135

I know at some point we changed https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target/MvxSpinnerSelectedItemBinding.cs

While in the area we'd also need to check other files and double check our null and ValueType cases:

I believe last time this came up I looked at the Android and the underlying Java default implementations will use things like .indexOf which internally should use .equals - so the aim was to match that...

@slodge
Copy link
Contributor

slodge commented Jun 11, 2013

As a further note, GetPosition is used in two target bindings via code like:

             var index = listView.Adapter.GetPosition(value);

I'm not 100% sure that we should actually be using IAdapter.GetPosition here - need to think carefully here as this GetPosition should really be used for Java objects... This is tied to http://stackoverflow.com/questions/13842864/why-does-the-gref-go-too-high-when-i-put-a-mvxbindablespinner-in-a-mvxbindableli/13995199#comment19319057_13995199 which is not a place I want to touch too closely. It may be better to add our own GetRawPosition method here and to just return -1 for the Java method - need to think and test on this.

@slodge
Copy link
Contributor

slodge commented Jun 11, 2013

Phew.... scratch that last comment - looks like GetPosition is our method. In which case maybe it should just be renamed GetRawPosition to prevent people like me worrying about it

phew...

@zleao
Copy link
Contributor Author

zleao commented Jun 11, 2013

Mini heart attack there... :)
Indeed the #135 issue is very similar. That is exactly the problem I'm having inside GetPosition (soon to be GetRawPosition).

@slodge
Copy link
Contributor

slodge commented Jul 18, 2013

I believe this is working in 3.0.9 - plus I think it should be covered by the ApiExample - https://github.com/slodge/MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Droid/Resources/Layout/Test_Spinner.axml (this binds to Thing)

Thanks again

Closing

Stuart

@slodge slodge closed this as completed Jul 18, 2013
Costo pushed a commit to Costo/MvvmCross that referenced this issue May 30, 2014
martijn00 pushed a commit to martijn00/MvvmCross that referenced this issue Dec 8, 2016
Fix the solution.  Get the build going again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t/bug Bug type
Development

No branches or pull requests

2 participants