Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat are alternatives to generic collections for COM Interop?
    primarykey
    data
    text
    <p>I am attempting to return a collection of departments from a .NET assembly to be consumed by ASP via COM Interop. Using .NET I would just return a generic collection, e.g. <code>List&lt;Department&gt;</code>, but it seems that generics don't work well with COM Interop. So, what are my options?</p> <p>I would like to both iterate over the list and be able to access an item by index. Should I inherit from <code>List&lt;Department&gt;</code>, implement an <code>IList</code>, <code>IList&lt;Department&gt;</code> or another interface, or is there a better way? Ideally I would prefer not to have to implement a custom collection for every type of list I need. Also, will <code>List[index]</code> even work with COM Interop?</p> <p>Thanks, Mike</p> <h2>Example .NET components (C#):</h2> <pre><code>public class Department { public string Code { get; private set; } public string Name { get; private set; } // ... } public class MyLibrary { public List&lt;Department&gt; GetDepartments() { // return a list of Departments from the database } } </code></pre> <h2>Example ASP code:</h2> <pre><code>&lt;% Function PrintDepartments(departments) Dim department For Each department In departments Response.Write(department.Code &amp; ": " &amp; department.Name &amp; "&lt;br /&gt;") Next End Function Dim myLibrary, departments Set myLibrary = Server.CreateObject("MyAssembly.MyLibrary") Set departments = myLibrary.GetDepartments() %&gt; &lt;h1&gt;Departments&lt;/h1&gt; &lt;% Call PrintDepartments(departments) %&gt; &lt;h1&gt;The third department&lt;/h1&gt; &lt;%= departments(2).Name %&gt; </code></pre> <h2>Related questions:</h2> <ul> <li><a href="https://stackoverflow.com/questions/161704/using-generic-lists-on-serviced-component">Using Generic lists on serviced component</a></li> <li><a href="https://stackoverflow.com/questions/56375/are-non-generic-collections-in-net-obsolete">Are non-generic collections in .NET obsolete?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload