[CLOSED] Cannot convert from 'Ext.Net.Label' to 'Ext.Net.Component'

  1. #1

    [CLOSED] Cannot convert from 'Ext.Net.Label' to 'Ext.Net.Component'

    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?
    Last edited by Baidaly; Jul 02, 2013 at 12:01 AM. Reason: [CLOSED]
  2. #2
    Hello!

    You should use ComponentBase:

    List<ComponentBase> leftComponents = new List<ComponentBase>();
    List<ComponentBase> rightComponents = new List<ComponentBase>();
  3. #3
    Quote Originally Posted by Baidaly View Post
    Hello!

    You should use ComponentBase:

    List<ComponentBase> leftComponents = new List<ComponentBase>();
    List<ComponentBase> rightComponents = new List<ComponentBase>();
    Or AbstractComponent works as well.
  4. #4
    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.Component Base>' to 'System.Collections.Generic.IEnumerable<Ext.Net.Co mponent>'
    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));
  5. #5
    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.Component Base>' to 'System.Collections.Generic.IEnumerable<Ext.Net.Co mponent>'
    and
    The best overloaded method match for 'control.buildContainer(System.Collections.Generic .IEnumerable<Ext.Net.Component>)' has some invalid arguments
  6. #6
    Try to Cast them to Component using Select:

    leftSide.AddRange(leftComponents.Select(i => (Component)i));
  7. #7
    Quote Originally Posted by Baidaly View Post
    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));
  8. #8
    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.
  9. #9
    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.

Similar Threads

  1. Replies: 8
    Last Post: Feb 06, 2013, 6:27 PM
  2. Replies: 0
    Last Post: Dec 01, 2011, 6:43 AM
  3. [CLOSED] Label Style / Obtain label color based on theme
    By rthiney in forum 1.x Legacy Premium Help
    Replies: 11
    Last Post: Apr 15, 2010, 10:35 AM
  4. Replies: 1
    Last Post: May 22, 2009, 7:38 AM
  5. Replies: 0
    Last Post: Nov 19, 2008, 9:54 PM

Tags for this Thread

Posting Permissions