views:

306

answers:

4

Hi,

I am having trouble exposing a C# collection to Classic ASP. I've tried to use IEnumerable and Array. but I get the "object not a collection" error.

my method looks like:

public IEnumerable<MyObj> GetMyObj() { ... }

and on the Classic ASP side:

Dim obj, x 
Set obj = Server.CreateObject("Namespace.class")

For Each x in obj.GetMyObj
...

So how can I pass a collection to Classic ASP?

UPDATE:

may be this is a progress, the solution I found was to use a new class that inherits IEnumerable instead of using IEnumerable<MyObj>:

public class MyEnumerable : IEnumerable
{
   private IEnumerable<MyObj> _myObj;

   .
   .
   .

    [DispId(-4)]
    public IEnumerator GetEnumerator()
    {
      _myObj.GetEnumerator();
    }
}

But now when I try to access a property of MyObj I get an error: Object required.

Any idea?

A: 

I know this may sound bizarre but I had a similar issue and solved it by wrapping collection with brackets

For Each x in (obj).GetMyObj

Don't know why this would make any difference but it worked for me...

Jakub Konecki
I've just tried adding brackets - same error. :-(
CD
Sorry - worked for me...
Jakub Konecki
A: 

If you get an Object required error try this:

For Each key in obj.GetMyObj
    Set x = obj.GetMyObj(key)
    Response.Write x.YOURPROPERTY
Next
Yots
I get a `Wrong number of arguments or invalid property assignment: 'GetMyObj'` error.
CD
has this got something to do with generics here? could you redesign your class so that MyObj Derives from IEnumerable? MyObj : IEnumerable
Dai Bok
A: 

Reverse-engineering may solve the problem. Try to pass an ASP classic collection to ASP.NET and then see what the type of object is. Then you can pass that type from ASP.NET to classic ASP instead of a trial-error approach. If there's no relevant type in ASP.NET then you should do more reverse-engineering and explore the binary data for finding the pattern and writing your own casting method.

Xaqron
+1  A: 

I think you found the answer; Classic ASP with VB does not have generics built into it. So, you try to pass an IEnumerable<MyObj> to ASP and it comes out as a nightmarish mashup name which classic ASP has no clue how to work with.

The solution is to pass a non-generic IEnumerable. The problem then is that the sequence is treated as containing basic Object instances. You must revert to the pre-2.0 methods of casting objects you get out of a list. In VB this isn't difficult; just explicitly specify the element type and VB will implicitly cast for you while iterating through the For Each:

Dim obj, x 
Set obj = Server.CreateObject("Namespace.class")

For Each x As MyObj in obj.GetMyObj //casts each element in turn
   ...
KeithS