List of Available OOP Concepts

2020-07-10 06:23发布

I am building some material for OOP (object oriented programming) in VBA. Can anybody list me the OOP concepts which are available in VBA?

For example, from my reading I discovered that:

  1. Inheritance is not available in VBA.
  2. The encapsulation concept is there, as you can use access modifier "private" and build a public property.

标签: oop vba excel
3条回答
甜甜的少女心
2楼-- · 2020-07-10 06:27

VBA supports some OO concepts, and not others.

with VBA you can create your own classes, and you can create objects from those classes. However, VBA does NOT support Inheritance, and does not truly support 'polymorphism' in the classical meaning of the term as used in OO languages such as C++, or .NET.

VBA classes do support encapsulation, and abstraction.

查看更多
beautiful°
3楼-- · 2020-07-10 06:30

One particular shortcoming in VBA is in the encapsulation of arrays of objects.

You can have arrays of objects, but you cannot go more than one level down. Workarounds exists, for example by using the Variant type but then you lose type safety. This makes it cumbersome to work with hierarchical object structures, while possible you will end up with convoluted code.

查看更多
狗以群分
4楼-- · 2020-07-10 06:35

Here are some observations I've made while working with OOP concepts in VBA:

  • You cannot overload methods in VBA. However, you do have optional parameters at your disposable, for better or for worse.
  • You have one parameterless Class_Initialize method that is called when the object is instantiated, but it cannot be overloaded to handle parameters. If you want to force your class to not be "fully functional" without having certain properties set, you'll have to write your own way to do so.
  • The VB6 and VBA editing environment forces you to build "class files" and keep each class in a separate file, which are distinct from modules.
  • Classes and Modules can both have public and private fields. A public field in a module is essentially a global variable.
  • Modules are functionally similar to static classes in C#. Public code can be called from the modules anywhere within your application.

The VB6/VBA paradigm envisions classes as a way to encapsulate an object's functionality and properties. In this sense, VB6/VBA's objects exist just like any other basic OOP environment and their use and design should be encouraged where appropriate.

However, the lack of several key OOP features cause VB6/VBA to fall short in being able to thoroughly implement a complete OOP design pattern.

查看更多
登录 后发表回答