PDA

View Full Version : [CLOSED] Cannot convert from 'Ext.Net.Label' to 'Ext.Net.Component'



zfreeman
Jun 25, 2013, 5:39 PM
In a 1.X version of a project I had some code where I create two lists of components:


List<Component> leftComponents = new List<Component>();
List<Component> rightComponents = new List<Component>();

Then I go through and add various components to these lists.
For example:

leftComponents.Add(new Label(setting.name));
leftComponents.Add(new TextField(tconfig));

After converting to 2.2, I am getting the following error when trying to add these to the lists:
"Cannot convert from 'Ext.Net.Label' to 'Ext.Net.Component'"

Is there a different type other than Component that I could use to still be able to do something like this?

Baidaly
Jun 25, 2013, 11:59 PM
Hello!

You should use ComponentBase:


List<ComponentBase> leftComponents = new List<ComponentBase>();
List<ComponentBase> rightComponents = new List<ComponentBase>();

ViDom
Jun 26, 2013, 8:09 AM
Hello!

You should use ComponentBase:


List<ComponentBase> leftComponents = new List<ComponentBase>();
List<ComponentBase> rightComponents = new List<ComponentBase>();

Or AbstractComponent works as well.

zfreeman
Jun 26, 2013, 8:49 PM
Thanks! This takes care of my posted issue, however further down I then add these Component (now ComponentBase) lists to two form panels (named, fittingly "leftSide" and "rightSide"):


leftSide.Add(buildContainer(leftComponents));
rightSide.Add(buildContainer(rightComponents));


When I attempt to do this I get the following errors:
Cannot convert from 'System.Collections.Generic.List<Ext.Net.ComponentBase>' to 'System.Collections.Generic.IEnumerable<Ext.Net.Component>'
and
The best overloaded method match for 'control.buildContainer(System.Collections.Generic .IEnumerable<Ext.Net.Component>)' has some invalid arguments

Here's how I ended up addressing this, on the off chance that someone else runs into the same issues...

List<Component> leftComponentConvert = new List<Component>(leftComponents.Cast<Component>());
List<Component> rightComponentConvert = new List<Component>(rightComponents.Cast<Component>());
leftSide.Add(buildContainer(leftComponentConvert)) ;
rightSide.Add(buildContainer(rightComponentConvert ));

zfreeman
Jun 26, 2013, 10:26 PM
Actually that doesn't do the trick... I still get that same original error when I convert this back to a Component list and try to add it to the FormPanel:
"Cannot convert from 'Ext.Net.Label' to 'Ext.Net.Component'"

Any ideas on how to do this with ComponentBase lists?



leftSide.Add(buildContainer(leftComponents));
rightSide.Add(buildContainer(rightComponents));

I get these two errors right now:
Cannot convert from 'System.Collections.Generic.List<Ext.Net.ComponentBase>' to 'System.Collections.Generic.IEnumerable<Ext.Net.Component>'
and
The best overloaded method match for 'control.buildContainer(System.Collections.Generic .IEnumerable<Ext.Net.Component>)' has some invalid arguments

Baidaly
Jun 27, 2013, 2:37 AM
Try to Cast them to Component using Select:


leftSide.AddRange(leftComponents.Select(i => (Component)i));

zfreeman
Jun 27, 2013, 2:57 PM
Try to Cast them to Component using Select:


leftSide.AddRange(leftComponents.Select(i => (Component)i));

This allows the project to build (using Add rather than AddRange - I don't think AddRange exists for FormPanel) but then when I actually run the page I get the same error:

Unable to cast object of type 'Ext.Net.Label' to type 'Ext.Net.Component'.

And the error's being thrown right here:


leftSide.Add(leftComponents.Select(i => (Component)i));

Daniil
Jun 27, 2013, 4:41 PM
Hi,

I do not understand the problem well.

A FormPanel's Add method takes AbstractComponents. Ext.Net.Label inherits AbstractComponent in its chain. So, it is possible to pass a Label into a FormPanel's Add method.

Ext.Net.Label should be cast to Component, because it doesn't inherit it.

If the problem persists, please provide a simplified test case.

zfreeman
Jul 01, 2013, 10:48 PM
Turns out I didn't need to convert. It was the "buildContainer" part of the code that I no longer needed after using AbstractComponent instead of Component:



leftSide.Add(leftComponents);
rightSide.Add(rightComponents);

Thanks to everyone for the help.