IList[string] instead of List[string]

Why, when I set a variable to a new instance of List[string], do I get an IList[string]? They aren't interchangeable and by giving me an IList[string] I have no access to any properties or methods not part of the IList interface such as AddRange which is a method of the List class but not part of the IList interface.

  

This seems to be a bug in your .NET implementation. What do I need to do to get a fully functional List[string] object?

Thanks.

Tags:

  • Verified Answer

    Hi Randy, in the syntax:

    declare TableNew as List[string]

    ... the word 'List' is interpreted as a reserved word indicating a predefined LIST type, which as you have discovered is mapped in .NET onto the generic IList interface.  This is typically used in conjunction with the verb CREATE (which will in fact create an instance of a generic List class), and the I-O verbs WRITE, READ etc.

    When compiling for JVM, this predefined LIST type is mapped onto the inerface type java.util.List, with the CREATE verb creating an instance of java.util.ArrayList.

    The idea of this predefined LIST type (and the similar predefined DICTIONARY type) is that it is possible to write portable code using lists and dictionaries which will function equally on .NET and on JVM.

    If however you really want an instance of the List class in .NET (i.e. System.Collections.Generic.List), then you should refer to that type using the TYPE keyword., e.g. something like:

    declare TableNew as type System.Collections.Generic.List[string]

    ...or, provided the namespace System.Collections.Generic has been specified in project properties:

    declare TableNew as type List[string]

    This will enable you to use the full range of methods/properties from the List class, but obviously means that the code will be .NET specific, and not portable to JVM.

    Regards, Robert.

  • Thanks, Robert.

    I'm always forgetting to add the TYPE keyword (C# of course doesn't have that) just like I keep trying to  terminate lines with semi-colons. But usually, I get an error if I don't have it. So when I didn't get an error I assumed I was doing it correctly. That's what I get for assuming. :)

  • Thanks, Robert.

    I'm always forgetting to add the TYPE keyword (C# of course doesn't have that) just like I keep trying to  terminate lines with semi-colons. But usually, I get an error if I don't have it. So when I didn't get an error I assumed I was doing it correctly. That's what I get for assuming. :)

  • Yes, I know a lot of people have problems with the TYPE keyword (also internally here within Micro Focus) - problems with dealing with a language that has so much syntax and so many reserved words.

    Terminating lines with semi-colons is actually allowable, since COBOL treats semi-colons (and commas) as white space.  However, for some reason the IDE paints the semi-colons a nasty pink colour...

    Robert.