VBfx / Tile tutorial / Step 2

Save and Load

As already mentioned the Save and Load functions open and close the specified file, checking existance and requesting a file number so we can handle the data. This functions won't change later so you can use them in any other class you like!

SaveData

Public Function SaveData( iFileName as String ) as Boolean
On Error GoTo 1

    Dim FileNumber as Integer
    
    'Open the specified file
    FileNumber = FreeFile
    Open iFileName For Binary as FileNumber
        'Write data
        WriteData FileNumber
    Close FileNumber
    
    'We're done
    SaveData = True
    Exit Function

1  'Catched an error
    SaveData = False
End Function

Not much to explain here... the function gets a file handle (FileNumber), opens the file and forwards the handle to the WriteData function. If everything works right the return value is True.

LoadData

Public Function LoadData( iFileName as String ) as Boolean
On Error GoTo 1

    Dim FileNumber as Integer
    
    'Check existance
    If Dir( iFileName, vbNormal ) = "" Then: GoTo 1
    
    'Open the specified file
    FileNumber = FreeFile
    Open iFileName For Binary as FileNumber
        'Read data
        ReadData FileNumber
    Close FileNumber
    
    'We're done
    LoadData = True
    Exit Function

1  'Catched an error
    LoadData = False
End Function

Nearly the same here, except that we check if the file exists before trying to open it.

As you can see both functions are quite general and don't name any module or class at all. However you still have to copy the code for any Module you need it.

Navigation