Creating a COM object for Classic ASP (Part 2)

Summary

In this blog post I’m going to expand on my last post about COM objects and design a COM “wrapper” for a dictionary. This will demonstrate the use of properties, passing parameters and the indexer.

The MyDictionary Class

The code that I’m going to use in my COM module will be something like this:

public interface IMyDictionary
{
    object this[string key] { get; set; }
}

public class MyDictionary : IMyDictionary
{
    private Dictionary<string, object> Vars = new Dictionary<string, object>();

    public object this[string key]
    {
        get 
        {
            return Vars[key.ToLower()];
        }
        set 
        {
            Vars[key.ToLower()] = value;
        }
    }
}

As I mentioned in the summary, this is nothing more than a wrapper for the basic functionality of the Dictionary object using a string indexer and saving and returning an object data type. Technically, there is already a dictionary COM object available for Classic ASP, but I wanted to demonstrate how this functions.

Next, follow the steps of the last blog post (or download the final version at the bottom of this blog post) and use it in ASP.

Here’s a sample ASP program:

<%
Dim MyComObject
Dim MyText

Set MyComObject = Server.CreateObject("ComDictionary.MyDictionary")

MyComObject("testvar") = "test data"

MyText = MyComObject("testvar")

%>
<html>
<head></head>
<body>
    <%=MyText %>
</body>
</html>

When you execute your program, you’ll see “test data” appear in the browser.

Adding Methods to Your Object

By now you’re probably getting the idea that setting up a COM object is the hardest part. After that, it’s just C# code to add to your object and ASP code to call the object. So let’s add a clear function to clear all the data in our dictionary:

public void Clear()
{
    Vars.Clear();
}

And add the interface definition to your interface section:

void Clear();

Restart your IIS and rebuild your COM solution. Now change your ASP program to look like this:

<%
Dim MyComObject
Dim MyText

Set MyComObject = Server.CreateObject("ComDictionary.MyDictionary")
MyComObject("testvar") = "test data"

MyComObject.Clear
MyText = MyComObject("testvar")

%>
<html>
<head></head>
<body>
    <%=MyText %>
</body>
</html>

If you run it you’ll notice that it blows up. Some of you probably already know what the “issue” is. It’s the fact that an item is not in the dictionary, and it’s crashing at the “get” property. So let’s fix that. Chang your “get” property to look like this:

get 
{
    if (Vars.ContainsKey(key.ToLower()))
    {
        return Vars[key.ToLower()];
    }
    else
    {
        return null;
    }
}

Now reset your IIS and rebuild your COM solution. You’ll notice that there is no output. That’s because you cleared the variable that was set in the dictionary and the subsequent read of that variable produced nothing.

Where to get the Code

You can visit my GitHub account and download this sample by clicking here.  I have included the home.asp file in the root directory.

Leave a Reply