Aarghl! Error #1034 Type Coercion failed: Cannot convert something to something

The error “Type Coercion failed: cannot convert [#78F6AA, #CAF2DA] to Array” has stared me in the face for the past few days or so.

The error actually has useful information in it (but at the moment I got it, ofcourse I never properly read it…). It states that whatever you are doing, you are passing the wrong type of variable to the function/variable. It expects it to be in another type.

This blog entry helped me to understand the error message.

I wanted to implement a function so the user can select his own gradient colors and save them as a preference for my AIR app. cp1 and cp2 are 2 flex colorpicker functions.

[Bindable] private var colors_selected:Array = [cp1.selectedColor, cp2.selectedColor];

However, everytime I ran the program I got the Type Coercion error.

Finally I found out what’s wrong. I made an array out of both colorpicker selected colors, and then passed it to backgroundGradientColors in another array.
Duh (Smacks head) !
Of course it doesn’t like it ! Below is the corrected code. The mx:SetStyle now does not specify an array, as this is constructed in the variable colors_selected.

[Bindable] private var colors_selected:Array = [cp1.selectedColor, cp2.selectedColor];