ArgumentException: Value does not fall within the expected range. - WHY?

I’ve bumped into one of those weirdo C#/Unity things that TOTALLY DOES NOT MAKE SENSE.

Here’s what’s happening… I have a List<> names myAvailableLanguages and want to allow a user to pick a (new) language from a dropdown UI object. Aside from that dropdown UI object, I have a 2nd one that allows the user to pick a language group (like Germanic, Slavic or others - the list of languages is HUGE). There are also some bools connected to myAvailableLanguages and I got those toggles to toggle UI objects. This works perfectly and no problems there.

Now I want to use the toggle UI objects where the selected language plays a role if it’s intractable/toggled or not. For this I’ve written the following code:

Debug.Log(myAvailableLanguages[myAvailableLanguages.FindIndex(x=> x.Language.ToString()==myDropdownLanguages.options[myDropdownLanguages.value].text.ToString())].Language);

I won’t go into too much detail on this line, but in theory it’s 100% okay. Yet the line gives the error of this thread’s topic. When I break down the line into several elements, they all give the right response.

When I use:

Debug.Log(myAvailableLanguages[myAvailableLanguages.FindIndex(x=> x.Language.ToString()=="Japanese")].Language+" "+myDropdownLanguages.options[myDropdownLanguages.value].text.ToString());

and select Japanese from the dropdown UI object, the line debugs as “Japanese Japanese” as it should.
Also look at the above line and the 1st one. Both use identical statements, where in the 1st one I substituted “Japanese” for the 2nd part of the line above here.

When I use:

string tempstring=myDropdownLanguages.options[myDropdownLanguages.value].text.ToString();
Debug.Log(myAvailableLanguages[myAvailableLanguages.FindIndex(x=> x.Language.ToString()==tempstring)].Language);

I get the correct response of the current language selected from the dropdown UI object and once again, these 2 lines are basically the same as the one I’ve listed 1st.

Though it’s clear for me to use the last line examples to get the code I want to work as it should, I just can’t stop to wonder about the WHY the 1st line with an actual correct statement gives this ArgumentException error…

I’m going to bet its something to do with the way that the lambda is being passed the information. In the test and the final version the right side of the comparison is a “set” string in the first it’s all “dynamic”, perhaps the scope within the lambda or something isn’t working quite as you’re expecting?

(I’m still at “lambda functions” are magic though, so it’s a guess :slight_smile: )

Lambdas capture variables, not their values. That’s probably what’s biting you in the ass here.

Without seeing more of the code, it’s hard to tell exactly what’s happening, but that’s the essence. Feel free to post a bigger chunk of the code.

By the way, I’m pretty sure that text.ToString() isn’t necessary. It’s not a problem, just a nitpick.

Think you and LeftyRighty might be right on the variables. Though odd, converting it to string and then use it works like a glance.

In this case I need to. myAvailableLanguages is an ENUM and without the ToString() I get an error that I can’t convert ENUM to string. In other cases I don’t need it, but adding it anyway, just to be sure and remove chances on errors when going from Monodevelop to the Unity engine (project is pretty big already and it takes a couple of seconds before the Unity engine responds)