Highlighted
Absent Member.
Absent Member.
1952 views

IList[string] instead of List[string]

Jump to solution

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.

Randy Hodge

 

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: IList[string] instead of List[string]

Jump to solution

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.

View solution in original post

0 Likes
3 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: IList[string] instead of List[string]

Jump to solution

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.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: IList[string] instead of List[string]

Jump to solution

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. 🙂

Randy Hodge

 

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: IList[string] instead of List[string]

Jump to solution

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.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.