When to use a Class in VBA?

后端 未结 12 1163
情歌与酒
情歌与酒 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:51

    I think the criteria is the same as other languages

    If you need to tie together several pieces of data and some methods and also specifically handle what happens when the object is created/terminated, classes are ideal

    say if you have a few procedures which fire when you open a form and one of them is taking a long time, you might decide you want to time each stage......

    You could create a stopwatch class with methods for the obvious functions for starting and stopping, you could then add a function to retrieve the time so far and report it in a text file, using an argument representing the name of the process being timed. You could write logic to log only the slowest performances for investigation.

    You could then add a progress bar object with methods to open and close it and to display the names of the current action, along with times in ms and probable time remaining based on previous stored reports etc

    Another example might be if you dont like Access's user group rubbish, you can create your own User class with methods for loging in and out and features for group-level user access control/auditing/logging certain actions/tracking errors etc

    Of course you could do this using a set of unrelated methods and lots of variable passing, but to have it all encapsulated in a class just seems better to me.

    You do sooner or later come near to the limits of VBA, but its quite a powerful language and if your company ties you to it you can actually get some good, complex solutions out of it.

提交回复
热议问题