Public Not Creatable in VB6

Have you seen this in any VB6 projects you’ve ever worked on?

If you haven’t, or have but didn’t understand what this is, read on cause I’ll tell you what it is and why and when you want to use this.

PublicNotCreatable is one of the values that you can set for the class Instancing property.

This is only available when the class is inside an ActiveX project (DLL, EXE, OCX) so if you only ever deal with Standard EXE then you likely have never encountered this before.

When instancing is set to this, that class cannot be instantiated — meaning, you cannot use “New” on it.
If you do, VB6 will throw an error. As the name implies, it is “not creatable”.

Wait, why would anyone want to create a class that cannot be instantiated or created?

The answer is to create Interfaces. If you’ve read my previous blog, I’ve mentioned that Interfaces enables us to implement Abstraction in our codes. And on this blog, I’ll show you how to do this on an ActiveX DLL and how you can extend it outside of the DLL (for example, from a client app like a Standard EXE).

Acme DLL

We’re going to create an ActiveX DLL from a fictional company called “Acme”

Search Engine Interface

In this project, we have the search engine interface (named iSearchEngine).
The prefix “i” is used to remind us that it is an “interface” class so we don’t try and create it.

Here’s the code for that class:


We then set the “Instancing” property to “PublicNotCreatable” so this class won’t get created.

The reason we set this is because interface classes are NOT intended to be created. It simply act as a template for concrete classes. We don’t have to set this (just like from the previous blog about Abstraction) but setting this “enforces” that intention.

Web Browser Class

Next, we have a Web Browser class that will utilize the search engine instance when it performs the search routine.

Here’s the code of the Web Browser class:

Default (dummy) Search Engine Class

We’re also going to provide a default search engine to our browser class so it would run out of the box.

Here’s the code for our dummy search engine:

The Client Application

We’re now moving on to the application that will consume (or use) the Acme library.

Custom Search Engine

But before we write the tester module, let’s implement a search engine so we can test it too:

Test Module

Finally, we’ll write the codes to test both the default and custom search engines:

And here’s the output of the test:

Summary

As you can see, we easily used the default but also easily implement a new version of the search engine.

This flexibility gives us power to create very extendable or extensible systems, and if you’re writing libraries, you should know how to use this feature.

In case you’re wondering how I tested this, I placed both the Standard EXE and ActiveX DLL in a single solution (see image below to see what it looks like):

If you want to test the codes I used in this blog, here’s the link to the GitHub repository:

https://github.com/vegitz/codes/tree/master/0016%20Public%20Not%20Createable%20in%20VB6

Leave a Comment