Difference between declaring a userform as Object vs MSForms.Userform?

When you import the components into the project it will name it UserForm1 and probably UserForm2 respectively.

oForm == UserForm1

oForm1 == UserForm2

Now, looking at the MSDN docs for Object we find that:

You can declare an object variable with the Object data type when the specific object type is not known until the procedure runs. Use the Object data type to create a generic reference to any object.

You've declared the variables like so:

Dim oForm           As MSForms.UserForm  
Dim oForm1          As Object  

So what happens when you initialize the objects is oForm gets initialized as a UserForm, while the runtime determines that the Object oForm1 is an instance of UserForm1, which is not the same thing.

Try changing the component name of oForm1 prior to initializing it and you should pretty quickly see the difference.

Now, if you want the type safety of declaring as a generic form and you want to access the Width property, you can cast your UserForm as an Object and access it like so.

Dim FormAsForm As UserForm
Dim FormAsObject As Object

Set FormAsForm = New UserForm1
Set FormAsObject = FormAsForm

FormAsObject.Width = 200

Debug.Print TypeName(FormAsForm)
Debug.Print TypeName(FormAsObject)

This is a trick we use often when implementing multiple interfaces. The compiler will only allow you to use properties that are defined in the particular type the class object is declared as.


So what's the difference? Practically speaking, you get no intellisense when declaring things as Object. You also get no type safety. It's perfectly valid to do this (although not recommended.)

Dim foo As New Baz
Dim bar As New Qux
Dim var As Object

Set var = foo
Set var = bar

Object does come in extremely handy when you're using late binding to avoid adding references to your project though. Without the adding a reference, you're forced into using an unknown type.

Dim xl As Object
Set xl = CreateObject("Excel.Application")

The other big difference is you're leaving it up to the runtime to determine what kind of object the variable will be. As you discovered, it will sometimes (rarely, but sometimes) produce surprising results.

Tags:

Vb6

Vba

Userform