Objective:
Simulate namespaces in VBA and access functions like if you had a Framework setup or at least that was what I understood...
Background:
I was inspired by this piece of code: Framework.Strings.StartsWith in this post
Questions:
- As I'm basically coding against the predeclared instance, is there a risk of a memory leak?
- I see benefits while coding and accessing the functions, do you see downsides?
Code / File structure:
Code:
Class: Framework (simplified version)
'@Version(1)
'@Folder("Framework")
Option Explicit
'@PredeclaredId
' Copywrite (C) 2019 Ricardo Diaz
' This file is distributed under the GPL-3.0 license
' Obtain a copy of the GPL-3.0 license <http://opensource.org/licenses/GPL-3.0>
Private Type TFramework
Collection As CollectionUtilities
End Type
Private this As TFramework
Public Property Get Collection() As CollectionUtilities
Set Collection = this.Collection
End Property
Public Property Set Collection(ByVal Value As CollectionUtilities)
Set this.Collection = Value
End Property
Private Sub Class_Initialize()
Set Collection = New CollectionUtilities
End Sub
Class: CollectionUtilities
'@Version(1)
'@Folder("Framework.Utilities")
Option Explicit
'@PredeclaredId
'Credits: https://jkp-ads.com/Articles/buildexceladdin02.asp
'@Ignore ProcedureNotUsed
Public Function IsIn(ByVal Collection As Variant, ByVal Name As String) As Boolean
'-------------------------------------------------------------------------
' Procedure : IsIn Created by Jan Karel Pieterse
' Company : JKP Application Development Services (c) 2005
' Author : Jan Karel Pieterse
' Created : 28-12-2005
' Purpose : Determines if object is in collection
'-------------------------------------------------------------------------
Dim newObject As Object
Set newObject = Collection(Name)
IsIn = (newObject Is Nothing)
If IsIn = False Then
Set newObject = Collection(Application.WorksheetFunction.Substitute(Name, "'", vbNullString))
IsIn = (newObject Is Nothing)
End If
End Function
'@Ignore ProcedureNotUsed
Public Sub ClearCollection(ByRef Container As Collection)
Dim counter As Long
For counter = 1 To Container.Count
Container.Remove counter
Next
End Sub
'@Ignore ProcedureNotUsed
Public Function HasItem(ByVal Container As Collection, ByVal ItemKeyOrNum As Variant) As Boolean
Dim temp As Variant
On Error Resume Next
temp = IsObject(Container.Item(ItemKeyOrNum))
On Error GoTo 0
HasItem = Not IsEmpty(temp)
End Function
'@Ignore ProcedureNotUsed
Public Function ArrayToCollection(ByVal evalArray As Variant) As Collection
' Credits: https://stackoverflow.com/a/12258926/1521579
Dim tempCollection As Collection
Dim Item As Variant
Set tempCollection = New Collection
For Each Item In evalArray
tempCollection.Add Item
Next Item
Set ArrayToCollection = tempCollection
End Function
'@Ignore ProcedureNotUsed
Public Sub AddArrayItemsToCollection(ByVal evalCollection As Collection, ByVal evalArray As Variant)
Dim Item As Variant
For Each Item In evalArray
evalCollection.Add Item
Next Item
End Sub
'@Ignore ProcedureNotUsed
Public Sub DebugCollectionValues(ByVal evalCol As Collection)
Dim counter As Long
For counter = 1 To evalCol.Count
Debug.Print evalCol(counter).Name, evalCol(counter).Value
Next counter
End Sub
'@Ignore ProcedureNotUsed
Public Function TableRowToCollection(ByVal sourceCell As Range) As Collection
Dim EvalCell As Range
Dim evalTable As ListObject
Dim evalListRow As ListRow
Dim evalCollection As Collection
Dim evalRow As Long
Set evalTable = sourceCell.ListObject
evalRow = sourceCell.Row - evalTable.HeaderRowRange.Row
Set evalListRow = evalTable.ListRows(evalRow)
Set evalCollection = New Collection
For Each EvalCell In evalListRow.Range.Cells
evalCollection.Add EvalCell.Value2, evalTable.HeaderRowRange.Cells(EvalCell.Column - evalTable.HeaderRowRange.Column + 1).Value2
Next EvalCell
Set TableRowToCollection = evalCollection
End Function
And call it like this:
Code has annotations from Rubberduck add-in

