Attribute VB_Name = "Module_demo_4_UDTtool"


'________ some user-defined types for code demonstration _________________________________
'
'   !Types CANNOT conatain Objects or enumerations for UDT SAVE/LOAD tool to work
'
'   -Must be Public
'   -Name Does NOT matter
'       *if the name(s) are changed, the UDT-Tool Class must be re-made using 'makeUDTtool'

Public Type myTYPE1

    num1 As Double
    str1 As String

End Type


Public Type myTYPE2

    type1_arr() As myTYPE1
    
    num2 As Integer
    
    num_arr2() As Integer

End Type


Public Type myTYPE3
    
    type2_arr() As myTYPE2
    
    str3 As String
    
End Type

'_______________________________________________________________________________




'_____ UDT SAVE/LOAD Tool CLass Maker ___________________________________
'
'   only need to be called once
'   will only make code if Class does Not exist yet
'
'   Creates a Class that has functions for Saveing/Load UDT arrays from file
'
'   myUDTmakerTool.makeClass [name of new-Class], [exact name of User-Type], [exact name of User-Type], ...
'
'   After this is run, the 'udtTool_SaveLoad_demo' can run
'
'myTYPE1
'myTYPE2
'myTYPE3
Sub udtTool_maker_demo()

    Dim myUDTmakerTool As New makeUDTtool
    

    ' *attempt to make UDT tool with no type-names passed
    '
    '   *need UDT names, won't make anything
    '
    myUDTmakerTool.makeClass "new1_UDTTools"
    
    
    '- make UDTtool named 'new1_UDTTools' with only 'myTYPE1' functions/properties
    '
    myUDTmakerTool.makeClass "new1_UDTTools", "myTYPE1"
    'SUCCESS
    

    ' *attempt make UDTtool for 'myTYPE2' using the SAME NAME
    '   - will NOT do anything!
    '
    '   code will not overwrite existing Classes
    '       must delete Class manually to remake
    '
    myUDTmakerTool.makeClass "new1_UDTTools", "myTYPE2"
    
    
    
    ' make UDTtool named 'new2_UDTTools' with all user-type functions
    '
    myUDTmakerTool.makeClass "new2_UDTTools", "myTYPE1", "myTYPE2", "myTYPE3"
    'SUCCESS
    
    '   now Classes 'new1_UDTTools' & 'new2_UDTTools' are available
    '
    '   They'll have save/load functions for {myTYPE1} & {myTYPE1,myTYPE2,myTYPE3} (respectivly)
    '

End Sub


'myTYPE1
'myTYPE2
'myTYPE3
Sub udtTool_SaveLoad_demo()

    Dim demo_type1() As myTYPE1
    Dim demo_type2() As myTYPE2
    Dim demo_type3() As myTYPE3
    
    Dim check_type1() As myTYPE1
    Dim check_type2() As myTYPE2
    Dim check_type3() As myTYPE3
    
    
    Dim myUDTtool As New new2_UDTTools  '<-- made in the above code
    
    '-- populate UDTs with crap
    Call populate_demo_types(demo_type1, demo_type2, demo_type3)
    
    
    '-- retain values (for program validation)
    check_type1 = demo_type1
    check_type2 = demo_type2
    check_type3 = demo_type3


    '____ Program will SAVE UDT arrays to file(s) _____________________________

    
    'STEP (1)
    '-- add UDT arrays to TOOL (type1&3)
    myUDTtool.myTYPE1 = demo_type1
    myUDTtool.myTYPE3 = demo_type3
    
    
    'STEP (2)
    '-- save to computer
    '
    '   saves both 'demo_type1', 'demo_type3'
    '
    '   !!  'demo_type2' is NOT saved
    '        it was not added to the udtTOOL @ STEP (1)
    '
    '   default filename is:
    '       "udt_array_[UDTname].txt"
    '
    myUDTtool.SaveUDT
    
    
    'STEP (3)
    '-- now add 'demo_type2'
    myUDTtool.myTYPE2 = demo_type2
    
    
    'STEP (4)
    '-- save all 3 to 'myFavoriteFile_[UDTname].txt'
    '
    '       "myFavoriteFile_myTYPE1.txt"
    '       "myFavoriteFile_myTYPE2.txt"
    '       "myFavoriteFile_myTYPE3.txt"
    '
    myUDTtool.SaveUDT "myFavoriteFile"
    
    
    'STEP (5)
    '-- save only demo_type2 to 'myThirdFile_[UDTname].txt'
    '
    '       "myThirdFile_myTYPE2.txt"
    '
    '       function will recongnize "myTYPE2" in 'FileName' argument
    '       and only save that type
    '
    myUDTtool.SaveUDT "myThirdFile" & "myTYPE2"
    
    

    '_______ This is what is Should be saved at this Point ________________________
    '
    '* in the same directory as the WorkBook ...
    '
    '   "udt_array_[UDTname].txt"
    '       -type1 & type3 saved there @ STEP (2)
    '           udt_array_myTYPE1.txt
    '           udt_array_myTYPE2.txt
    '
    '   "myFavoriteFile_[UDTname].txt"
    '       -type1 & type2 & type3 saved there @ STEP (4)
    '           myFavoriteFile_myTYPE1.txt
    '           myFavoriteFile_myTYPE2.txt
    '           myFavoriteFile_myTYPE3.txt
    '
    '   "myThirdFile_[UDTname].txt"
    '       -type2 saved there @ STEP (4)
    '           myThirdFile_myTYPE2.txt
    '_______________________________________________________________________________

    
    
    '____ Now, Program will LOAD UDT arrays from file(s) ___________________________
    
    
    'STEP (6)
    '-- load files saved with filename "udt_array_[UDTname].txt"
    '
    '   *remember, only type1&3 were save here @ STEP (2)
    '
    '
    Erase demo_type1    '<-- erase local arrays
    Erase demo_type2
    Erase demo_type3
    
    
    myUDTtool.LoadUDT "udt_array"
    
    demo_type1 = myUDTtool.myTYPE1
    demo_type3 = myUDTtool.myTYPE3
    
    demo_type2 = myUDTtool.myTYPE2  '<-- this will work, !!BUT it is NOT from the "udt_array" file
                                    '       It is from the file set @ STEP (3)
                                    '       Class still retains the type2 array that was set
                                    
    
    
    'STEP (7)
    '-- repeat STEP (6) but also set data in 'myUDTtool' to Nothing
    '
    '
    Erase demo_type1    '<-- erase local arrays
    Erase demo_type2
    Erase demo_type3
    
    myUDTtool.myTYPE1 = demo_type1      '< *Also reset arrays in Class
    myUDTtool.myTYPE2 = demo_type2
    myUDTtool.myTYPE3 = demo_type3
    
    
    myUDTtool.LoadUDT "udt_array"
    
    demo_type1 = myUDTtool.myTYPE1
    demo_type3 = myUDTtool.myTYPE3
    
    demo_type2 = myUDTtool.myTYPE2  '<-- !Now this will NOT work, unlike STEP (6)
                                    '    because the arrays were also reset in the Class
                                    '    -type2 was NOT save @ "udt_array...txt", so it is still set to NOTHING
                                    
                                    
    
    
    'STEP (8)
    '-- get ALL UDT arrays saved in "myFavoriteFile_[UDTname].txt"
    '
    '
    Erase demo_type1    '<-- erase local arrays
    Erase demo_type2
    Erase demo_type3
    
    myUDTtool.myTYPE1 = demo_type1      '< *Also reset arrays in Class
    myUDTtool.myTYPE2 = demo_type2
    myUDTtool.myTYPE3 = demo_type3
    
    
    myUDTtool.LoadUDT "myFavoriteFile"
    
    demo_type1 = myUDTtool.myTYPE1
    demo_type2 = myUDTtool.myTYPE2
    demo_type3 = myUDTtool.myTYPE3
    
    
    
    
    
    'STEP (9)
    '
    '   (A) get ONlY type1 from "udt_array_[UDTname].txt"
    '
    '   (B) get ONLY type2 from "myThirdFile_[UDTname].txt"
    '       * type2 is the only one saved there @ STEP (5)
    '
    '   (C) get ONLY type3 from "myFavoriteFile_[UDTname].txt"
    '
    '
    Erase demo_type1    '<-- erase local arrays
    Erase demo_type2
    Erase demo_type3
    
    myUDTtool.myTYPE1 = demo_type1      '< *Also reset arrays in Class
    myUDTtool.myTYPE2 = demo_type2
    myUDTtool.myTYPE3 = demo_type3
    
    
    '____(A)____________________________________________________________
    
    myUDTtool.LoadUDT "udt_array" & "myTYPE1"    '<-- load only type1 from "udt_array...txt"
    
    Erase demo_type1                    '<-- erase local arrays
    Erase demo_type2
    Erase demo_type3
    '-------------------------
    demo_type1 = myUDTtool.myTYPE1      '<-- should work
    demo_type2 = myUDTtool.myTYPE2      '<-- !should NOT work
    demo_type3 = myUDTtool.myTYPE3      '<-- !should NOT work
                                    
                                    
                                    
    '____(B)____________________________________________________________
      
    myUDTtool.LoadUDT "myThirdFile"     '<-- load only type2 from "myThirdFile...txt"
                                        '    *remember only type2 was saved there @ STEP (5)
                                        '     therefore, 'myTYPE2' does NOT need to be specified in the argument
    
    Erase demo_type1                    '<-- erase local arrays
    Erase demo_type2
    Erase demo_type3
    '-------------------------
    demo_type1 = myUDTtool.myTYPE1      '<-- should work, Class still holds UDT array loaded from "udt_array...txt" (above)
    demo_type2 = myUDTtool.myTYPE2      '<-- should work, Newly loaded from "myThirdFile...txt"
    demo_type3 = myUDTtool.myTYPE3      '<-- !should NOT work
    
    
    
    '____(C)____________________________________________________________
      
    myUDTtool.LoadUDT "myFavoriteFile" & "myTYPE3"    '<-- load only type3 from "myFavoriteFile...txt"
                                                    '    *remember, all types were saved there @ STEP (4)
    
    Erase demo_type1                    '<-- erase local arrays
    Erase demo_type2
    Erase demo_type3
    '-------------------------
    demo_type1 = myUDTtool.myTYPE1      '<-- should work, Class still holds UDT array loaded from "udt_array...txt" (above)
    demo_type2 = myUDTtool.myTYPE2      '<-- should work, Class still holds UDT array loaded from "myThirdFile...txt" (above)
    demo_type3 = myUDTtool.myTYPE3      '<-- should work, Newly loaded from "myThirdFile...txt"
    
                                    
    

End Sub






Sub populate_demo_types(ByRef demo_type1() As myTYPE1, _
                        ByRef demo_type2() As myTYPE2, _
                        ByRef demo_type3() As myTYPE3)

    Dim i, j As Integer
    Dim str As String
    
    ReDim demo_type1(9)
    For i = 0 To UBound(demo_type1)
    With demo_type1(i)
        .num1 = i
        .str1 = CStr(i)
    End With
    Next

    ReDim demo_type2(9)
    For i = 0 To UBound(demo_type2)
    With demo_type2(i)
        
        .type1_arr = demo_type1
        For j = 0 To UBound(.type1_arr)
        With .type1_arr(j)
            .num1 = .num1 + i * 10
            .str1 = CStr(.num1)
        End With
        Next
    
        .num2 = i * 10
        
        ReDim .num_arr2(5)
        For j = 0 To UBound(.num_arr2)
            .num_arr2(j) = .num2 + j
        Next
   
 
    End With
    Next

    ReDim demo_type3(7)
    For i = 0 To UBound(demo_type3)
    With demo_type3(i)
        
        .type2_arr = demo_type2
        For j = 0 To UBound(.type2_arr)
        With .type2_arr(j)
            .num2 = .num2 * 10
        End With
        Next
        
        .str3 = CStr(i * 100)

    End With
    Next


End Sub




