Question

Consider the Class Module named: MyClass

It can be instantiated with early binding using:

Dim cls As MyClass
Set cls = New MyClass

But one could instantiated with:

Dim MyClass As MyClass
Set MyClass = New MyClass

Thus you work with an object with the same name.

What are the issues with this?

Was it helpful?

Solution

VBA is not case sensitive like C# so class1 and Class1 are the same things..

There is nothing seriously wrong (meaning: since the compiler allows that...) with using the same name for an object instance as its class except in general it would be considered a really bad programming practice to use the same name for class instance as its class.

The only issue is that for the next dev who will touch your code or even yourself in the future you may be really confused looking at this. You can always right click the name of the variable and select definition but in a long and buggy run, this would be a real pain in the back...

Suppose somone gave you 20K+ lines of code with 50 classes and 150 instances of it. He says: something doesn't work and you're the one to debug and find what's wrong... ugr I wouldn't want to touch that...

The reason you can do that in VBA is because you can't create your own static classes in VBA, therefore a class doesn't expose any properties and can't be static so when calling the Class1 instance the intelli-sense will only show you the instance's available properties - not classes'. VBA also doesn't support class polymorphism (so no accessors like internal protected etc) so there isn't possible to derive from classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top