Reputation: 2441
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. List<Department>
, but it seems that generics don't work well with COM Interop. So, what are my options?
I would like to both iterate over the list and be able to access an item by index. Should I inherit from List<Department>
, implement an IList
, IList<Department>
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 List[index]
even work with COM Interop?
Thanks, Mike
public class Department {
public string Code { get; private set; }
public string Name { get; private set; }
// ...
}
public class MyLibrary {
public List<Department> GetDepartments() {
// return a list of Departments from the database
}
}
<%
Function PrintDepartments(departments)
Dim department
For Each department In departments
Response.Write(department.Code & ": " & department.Name & "<br />")
Next
End Function
Dim myLibrary, departments
Set myLibrary = Server.CreateObject("MyAssembly.MyLibrary")
Set departments = myLibrary.GetDepartments()
%>
<h1>Departments</h1>
<% Call PrintDepartments(departments) %>
<h1>The third department</h1>
<%= departments(2).Name %>
Upvotes: 11
Views: 7358
Reputation: 5001
I've been trying to address this same problem in vb.net (not fluent in C#).
Since List(Of T) supports the IList interface, for the COM interface for my objects I have tried specifying
Public Class MyClass
...
Private _MyList As List(of MyObject)
Public ReadOnly Property MyList As IList Implements IMyClass.MyList
Get
Return _MyList
End Get
End Property
and then specifying in the Public Interface that COM sees
ReadOnly Property MyList As IList
This seems to work fine from a classic ASP client that instantiates the object, calls a generator function and then reads MyList property like an array.
Upvotes: 1
Reputation: 11
In your ASP code, could you not do this?
<h1>The third department</h1>
<%= departments.Item(2).Name %>
I know that in VB .NET, C# indexers are supported via the "Item" property, so the same idea may work in ASP.
Upvotes: 1
Reputation: 31071
Since you are only consuming the data in ASP, I would suggest returning Department[]
. This should map directly to a SAFEARRAY in COM. It supports enumeration and indexed access too.
public Department[] GetDepartments() {
var departments = new List<Department>();
// populate list from database
return departments.ToArray();
}
Upvotes: 5
Reputation: 2441
After some more research and trial-and-error, I think I found a solution by using System.Collections.ArrayList
. However, this does not work with getting a value by index. To do so, I created a new class ComArrayList
that inherits from ArrayList
and adds new methods GetByIndex
and SetByIndex
.
public class ComArrayList : System.Collections.ArrayList {
public virtual object GetByIndex(int index) {
return base[index];
}
public virtual void SetByIndex(int index, object value) {
base[index] = value;
}
}
public ComArrayList GetDepartments() {
// return a list of Departments from the database
}
<h1>The third department</h1>
<%= departments.GetByIndex(2).Name %>
Upvotes: 12