Disadvantage 1. UDT can be public just like any other variable. You just need to scope them in the correct place.
Public Type Car
Make As String
Model As String
Price As Currency
Km As Long
End Type
Public g_udtMyCar As Car
Sub Example3()
'use UDTs (defined at top of module)
'e.g.
g_udtMyCar.Make = "Unknown"
' do something
End Sub
Disadvantage 2. Again depends on your scope definition
Disadvantage 3. Incorrect, you can redim and redim preserve
Option Explicit
Public Type Car
Make As String
Model As String
Price As Currency
Km As Long
End Type
Public g_udtMyCars() As Car
Sub Example4()
ReDim g_udtMyCars(1 To 3)
g_udtMyCars(1).Make = "Ford"
g_udtMyCars(2).Make = "Audi"
g_udtMyCars(3).Make = "BMW"
End Sub
Sub Example5()
ReDim Preserve g_udtMyCars(1 To 5)
g_udtMyCars(4).Make = "Mercedes"
g_udtMyCars(5).Make = "VW"
End Sub
Sub Test()
Dim lngIndex As Long
Example4
Example5
For lngIndex = LBound(g_udtMyCars) To UBound(g_udtMyCars)
Debug.Print lngIndex, g_udtMyCars(lngIndex).Make
Next
End Sub
Disadvantage 4. I don't see the use of udt against normal variables having a performance hit. Do you have any evidence of this?
Disadvantage 5. Don't see why, you need to clarify your statement about why they are hard to use in addin.
Disadvantage 6. No they are not.
extends previous code example.
Sub Test()
Dim lngIndex As Long
Example4
Example5
For lngIndex = LBound(g_udtMyCars) To UBound(g_udtMyCars)
Debug.Print lngIndex, g_udtMyCars(lngIndex).Make
Next
Example6 g_udtMyCars(2)
Example7 g_udtMyCars
Debug.Print GetCar(g_udtMyCars, "Ford").Price
End Sub
Sub Example6(MyCar As Car)
Debug.Print MyCar.Make
End Sub
Sub Example7(Cars() As Car)
Dim lngIndex As Long
For lngIndex = LBound(Cars) To UBound(Cars)
Cars(lngIndex).Price = Int((50000 - 15000 + 1) * Rnd + 15000)
Debug.Print Cars(lngIndex).Make, Cars(lngIndex).Price
Next
End Sub
Function GetCar(Cars() As Car, Name As String) As Car
Dim lngIndex As Long
For lngIndex = LBound(Cars) To UBound(Cars)
If StrComp(Cars(lngIndex).Make, Name, vbTextCompare) = 0 Then
GetCar = Cars(lngIndex)
Exit Function
End If
Next
End Function
Disadvantage 7. no more than stanadard variables
Bookmarks