When to use a Class in VBA?

后端 未结 12 1164
情歌与酒
情歌与酒 2020-12-02 07:04

When is it appropriate to use a class in Visual Basic for Applications (VBA)?

I\'m assuming the accelerated development and reduction of introducing bugs is a common

12条回答
  •  一生所求
    2020-12-02 07:49

    As there is a lot code overhead in using classes in VBA I think a class has to provide more benefit than in other languages:

    So this are things to consider before using a class instead of functions:

    • There is no class-inheritance in vba. So prepare to copy some code when you do similar small things in different classes. This happens especially when you want to work with interfaces and want to implement one interfaces in different classes.

    • There are no built in constructors in vba-classes. In my case I create a extra function like below to simulate this. But of curse, this is overhead too and can be ignored by the one how uses the class. Plus: As its not possible to use different functions with the same name but different parameters, you have to use different names for your "constructor"-functions. Also the functions lead to an extra debug-step which can be quite annoying.

    Public Function MyClass(ByVal someInit As Boolean) As MyClassClass Set MyClass = New MyClassClass Call MyClass.Init(someInit) End Function
    • The development environment does not provide a "goto definition" for class-names. This can be quite annoying, especially when using classes with interfaces, because you always have to use the module-explorer to jump to the class code.

    • object-variables are used different to other variable-types in different places. So you have to use a extra "Set" to assign a object

    Set varName = new ClassName

    • if you want to use properties with objects this is done by a different setter. You have to use "set" instead of "let"

    • If you implement an interface in vba the function-name is named "InterfaceName_functionName" and defined as private. So you can use the interface function only when you cast the Variable to the Interface. If you want to use the function with the original class, you have to create an extra "public" function which only calls the interface function (see below). This creates an extra debug-step too.

    'content of class-module: MyClass implements IMyInterface private sub IMyInterface_SomeFunction() 'This can only be called if you got an object of type "IMyInterface" end function private sub IMyInterface_SomeFunction() 'You need this to call the function when having an object of the type "MyClass" Call IMyInterface_SomeFunction() end function

    This means:

    • I !dont! use classes when they would contain no member-variables.
    • I am aware of the overhead and dont use classes as the default to do things. Usually functions-only is the default way to do things in VBA.

    Examples of classes I created which I found to be useful:

    • Collection-Classes: e.g. StringCollection, LongCollection which provide the collection functionality vba is missing
    • DbInserter-Class: Class to create insert-statements

    Examples of classes I created which I dont found to be useful:

    • Converter-class: A class which would have provided the functionality for converting variables to other types (e.g. StringToLong, VariantToString)
    • StringTool-class: A class which would have provided some functionality for strings. e.g. StartsWith

提交回复
热议问题