Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to use global constants, types and functions inside VBA classes?

Tags:

vba

I have started using VBA classes and I have always tried to write my code such that each class is "independent", that is, it has everything it needs--constants, functions, etc--inside. Lately, though, this approach has resulted in code duplication since, instead of calling public functions in different modules, I copied some code from the "outside world" (in the same project) into a class just to maintain its "self-sufficiency".

I am considering changing a few classes so that they will be able to access functions, constants, types, etc. from other modules just like any other module can, but something in me is telling me this might not be a good practice. Can somebody tell me that what the little voice is saying is wrong? Is there a better approach?

Thanks.

Updates:

My apologies for not providing details earlier. Here's a sample code:

'-------------------------------------
'Module 1
'-------------------------------------

Public Const INITIAL_VALUE As String = "Start"
Public Const FINAL_VALUE As String = "End"

'more constants ...

Public Type SheetLoc
   SheetName As String
   SheetRow As Long
   SheetColumn As Long
End Type

'more types ...

'-------------------------------------
'Module 2
'-------------------------------------

Public Function GetLocation(key As String) As SheetLoc

   Dim myLoc As SheetLoc

   'some codes here...
   '
   With myLoc
      .SheetName = someValue
      .SheetColumn = anotherValue
      .SheetRow = stillAnotherValue
   End With

   GetLocation = myLoc

End Function

'more module functions

'-------------------------------------
'Class Module
'-------------------------------------

'some codes...

Public Sub SaveResults()

   Dim currLoc As SheetLoc      '<==== using a type defined outside of the class

   'more declarations ....
   'some codes here ....

   If currentValue = INITIAL_VALUE Then      '<=== referring to external constant 
      currLoc = GetLocation(someKey)         '<=== calling an external function
   ElseIf currentValue = FINAL_VALUE Then    '<=== referring to an external constant 
      currLoc = GetLocation(anotherKey)
   Else
      currLoc = GetLocation(defaultKey)
   End If

   'populate data ...
   'save results ...

End Sub

Note the codes with "<====" in the comments; the class uses types, functions and constants defined outside of the class, and this is what makes me wonder if it's a good practice or if there's a better option. I guess I just don't fully get the encapsulation concept.

like image 391
bigeyes Avatar asked Feb 29 '12 07:02

bigeyes


1 Answers

I never put public constants in a class module. All, and I mean all, of my public variables and constants are in a standard module called MGlobals. That has two benefits. First, you and I know both know where to find them - they're somewhat dangerous and need to be findable. Second, if that module ever gets more than a few lines, I know I'm being lazy and need to refactor.

It's a good practice to try to keep your class modules modular. But don't go nuts with it. Good programming will never be dropping a selection of modules into a project and having them just work. There is, and should be, integration to do on any project of significance.

like image 64
Dick Kusleika Avatar answered Sep 21 '22 22:09

Dick Kusleika