VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "new1_UDTTools"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Option Explicit

Private UDT1() As myTYPE1
'<-- add additional types here

Private selUDT As Integer
Private namUDT() As String

Private currFileName As String


'========== Initializer ==========
Public Sub Class_Initialize()
    selUDT = 0
    
    Dim i As Integer: i = 1
    ReDim namUDT(1)
    namUDT(i) = "myTYPE1": i = i + 1
    '<-- add additional types here
    currFileName = ""
End Sub


Public Property Let myTYPE1(UDT() As myTYPE1)
    UDT1 = UDT
    selUDT = 1
End Property
'<-- add additional types here


Public Property Get myTYPE1() As myTYPE1()
    Dim s As Integer: s = selUDT
    selUDT = 1
    If UBound_UDT_ < 0 Then MsgBox ("UDT(myTYPE1) not availble")
    selUDT = s
    myTYPE1 = UDT1
End Property
'<-- add additional types here


Private Sub myPut(filenumber, Optional recnumber As Variant)

If selUDT = 0 Then MsgBox ("UDT() is not set, cannot perform operation"): Exit Sub

On selUDT GoSub opt1    '<-- add additional types here
GoTo optFinished

opt1: Put #filenumber, recnumber, UDT1: Return
'<-- add additional types here
optFinished:

End Sub


Private Sub myGet(filenumber, Optional recnumber As Variant)

If selUDT = 0 Then MsgBox ("UDT() is not set, cannot perform operation"): Exit Sub

On selUDT GoSub opt1    '<-- add additional types here
GoTo optFinished

opt1: Get #filenumber, recnumber, UDT1: Return
'<-- add additional types here
optFinished:

End Sub


Private Property Get UBound_UDT_() As Long

If selUDT = 0 Then UBound_UDT_ = -1: Exit Property

On Error GoTo -1
On Error GoTo UDT_NOT_SET

On selUDT GoSub opt1    '<-- add additional types here
GoTo optFinished

opt1: UBound_UDT_ = UBound(UDT1): Return
'<-- add additional types here
optFinished:
    On Error GoTo -1
    On Error GoTo 0
    Exit Property
    
UDT_NOT_SET:
    UBound_UDT_ = -1
    On Error GoTo -1
    On Error GoTo 0
    
End Property


Private Property Let UBound_UDT_(arr_len As Long)

If selUDT = 0 Then MsgBox ("UDT() is not set, cannot perform operation"): Exit Property

On selUDT GoSub opt1    '<-- add additional types here
GoTo optFinished

opt1: ReDim UDT1(arr_len): Return
'<-- add additional types here
optFinished:

End Property


'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'++++++++++ Everything below can stay the same                          ++++++++++++++++++++++++
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Private Property Let Filepath(strFileName As String)

'If selUDT = 0 Then MsgBox ("UDT() is not set, cannot perform operation"): Exit Property

strFileName = VBA.Replace(strFileName, ".txt", "")

Dim i As Integer
For i = 1 To UBound(namUDT)

    If VBA.InStr(strFileName, namUDT(i)) <> 0 Then
        selUDT = i
        strFileName = VBA.Replace(strFileName, "_" & namUDT(i), "")
        strFileName = VBA.Replace(strFileName, namUDT(i), "")
    End If
Next

If strFileName = "" Then Exit Property

currFileName = strFileName

End Property
'----------------------
Private Property Get Filepath() As String
    
If selUDT = 0 Then MsgBox ("UDT() is not set, cannot perform operation"): Exit Sub

If currFileName = "" Then currFileName = "udt_array"

Filepath = ThisWorkbook.Path & "\" & currFileName & "_" & namUDT(selUDT) & ".txt"

End Property



'Save UDT array to FilePath.
Public Sub SaveUDT(Optional FileName As String = "")
    
    Dim i As Integer
    Dim save_occured As Boolean
    
    save_occured = False
    Do
        
        For i = 1 To UBound(namUDT)
            If InStr(FileName, namUDT(i)) <> 0 Then
                privSaveUDT save_occured, FileName
                Exit Do
            End If
        Next
        
        For i = 1 To UBound(namUDT)
            privSaveUDT save_occured, FileName & namUDT(i)
        Next
    
    Loop While (False)

    If save_occured = False Then MsgBox ("failed to save UDT")

End Sub



'Save UDT array to FilePath.
Private Sub privSaveUDT(ByRef save_performed As Boolean, Optional FileName As String = "")
    'You can write a UDT array directly to a file using the Put # statement.
    'However, when loading them back from the file, we need to know how
    'many items there were in the array, so we can re-dimension it appropriately.
    
    'So we will write a short 6 byte header to the beginning of the file.
    'This will be a number telling us how many items were in the array.
    'Then we can ReDim() the array before loading it back from the file.
    
    'The header will always be 6 bytes, so for 3 item arrays, the header would be "000003"
    'This makes the max number of array items "999999" for this example.
    'You can easily modify the code to give you more.
    
    Dim intFF As Integer 'File handle to use.
    
    intFF = FreeFile 'Get available file handle.

    Filepath = FileName    '<-- set full filepath using name
    
    If UBound_UDT_ < 0 Then Exit Sub '<-- check that UDT() is initialized
    
    Open Filepath For Output As #1: Close #1    '<-- clear previous contents
    
    Open Filepath For Binary Access Write As #intFF
        Put #intFF, 1, BuildHeader(6)   'Write header.
        
        myPut intFF, LOF(intFF) + 1     'Write UDT array.
        
    Close #intFF
    
    save_performed = True
End Sub


'Pads a string with 0's until its x bytes long.
Private Function BuildHeader(ByVal Length As Long) As String

    BuildHeader = String$(Abs(Length - Len(CStr(UBound_UDT_))), "0") & CStr(UBound_UDT_)
End Function


'Load the file back into the UDT array (udtTest()).
Public Sub LoadUDT(Optional FileName As String = "")

    Dim i As Integer
    Dim load_occured As Boolean
    
    load_occured = False
    Do
        
        For i = 1 To UBound(namUDT)
            If InStr(FileName, namUDT(i)) <> 0 Then
                privLoadUDT load_occured, FileName
                Exit Do
            End If
        Next
        
        For i = 1 To UBound(namUDT)
            privLoadUDT load_occured, FileName & namUDT(i)
        Next
    
    Loop While (False)

    If load_occured = False Then MsgBox ("failed to load UDT")
    
End Sub




'Load the file back into the UDT array (udtTest()).
Private Sub privLoadUDT(ByRef load_performed As Boolean, Optional FileName As String = "")
    'First thing we'll do is look at the 4 byte header we wrote to the file.
    'This will tell us how many items are in the array.
    'Then we can ReDim udtTest() to the correct dimensions.
    'Then we can use the Get # statement to dump the file right back into the UDT array.
    
    Dim intFF As Integer, strHeader As String
    
    intFF = FreeFile
    
    Filepath = FileName    '<-- set full filepath using name
    
    If VBA.Dir(Filepath) = "" Then Exit Sub  '<-- check if path exist
    
    'Buffer strHeader to 6 bytes.
    strHeader = Space$(6)
    
    'Open file in binary read mode.
    Open Filepath For Binary Access Read As #intFF
        'Get 4 byte header.
        Get #intFF, 1, strHeader
        
        'Re-dimension the array.
        UBound_UDT_ = CLng(strHeader)
        
        'Dump the file back into the UDT.
        myGet intFF, 7
 
    Close #intFF
    
    load_performed = True
    
End Sub


'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

