Search This Blog

Wednesday 14 May 2008

How to consume a .Net ArrayList in classic ASP

I first followed the procedure to register a .Net assembly using regasm on the web server as below (http://weblogs.asp.net/dneimke/archive/2004/01/31/65330.aspx)

  1. Make sure your assembly has a strong name to install it in the GAC
  2. Run regasm /tlb “<path of your assembly>”
  3. Add the assembly to the GAC

This allows your assembly to be accessible in your ASP code to create server side objects

In my example I have a .Net assembly which has a class as below  

using System;                                                     
using System.Collections; 

namespace API 
{ 
      /// <summary> 
      /// Summary description for Versions. 
      /// </summary> 
      public class Versions 
      { 
            ArrayList versions = new ArrayList(); 

            public Versions() 
            { 
                  versions.Add(“1”); 
                  versions.Add(“2”); 
                  versions.Add(“3”); 
            }   

            public ArrayList List 
            { 
                  get { return versions; } 
            } 
      } 
} 

I initially tried to have a method in my .Net class return an ArrayList, Then I tried to create an object of type “System.Collections.ArrayList” in ASP and iterate the object in ASP, as the array list type definition was not supported when the asp code executed. So I created the class above which had a public property having a type of System.Collections.ArrayList  .

So now in ASP I did the following and it printed out the arrray list on my asp page 

<%@ codePage="65001" %> 
<html> 
      <head> 
            <title>Test Page</title>
            <%
   1:     set versions = Server.CreateObject("Versions") 
   2:                   For Each v in versions.List 
   3:                         Response.Write(v & "</br>")
   4:                   Next
   5:             
%> </head> <body></body> </html>

  That can be further enhanced in your ASP code to achieve the functionality you need to

 

 

No comments: