I figured it would be fun to have a thread on list some various tips or tricks that wasn't quite worth a thread on its own, but could be useful in sharing, even if not particularly relevant to our needs. I like to hear about others' clever tricks as it helps me take a new look at how we can do things.
So anyway:
Creating a read-only "constant" array
VB normally cannot hold a constant array (at least one with values filled in), and some snippets use class modules just to be able to initialize the array. This is how I would create a constant array without having to worry about whether it was already initialized before calling it, or writing values to it. This can be used in a standard module:
EDIT:
I discovered that Constants can be in fact stored in a procedure, so here is a much more robust way, and I assume faster because we aren't swapping in and out with an array and passing a variant anymore:
So anyway:
Creating a read-only "constant" array
VB normally cannot hold a constant array (at least one with values filled in), and some snippets use class modules just to be able to initialize the array. This is how I would create a constant array without having to worry about whether it was already initialized before calling it, or writing values to it. This can be used in a standard module:
Code:
Static Property Get foo() As Variant
Dim init As Boolean
Dim bite(2) As Byte
If Not init Then
bite(0) = &H1
bite(1) = &H10
bite(2) = &H11
init = True
foo = bite()
Else
foo = bite()
End If
End Property
Private Sub bar()
Dim i As Integer
For i = 0 To 2
Debug.Print foo(i)
Next i
End Sub
EDIT:
I discovered that Constants can be in fact stored in a procedure, so here is a much more robust way, and I assume faster because we aren't swapping in and out with an array and passing a variant anymore:
Code:
Static Property Get foo(iSelect As Integer) As String
Select Case iSelect
Case 1
Const foo1 As String = "A"
foo=foo1
Case 2
Const foo2 As String = "B"
foo=foo2
Case 3
Const foo3 As String = "C"
foo=foo3
Case Else
'Shouldn't be here!
foo=""
Err.Raise 0
End Select
End Property
Last edited: