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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With