dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: May 20th, 2006 03:02 AM Post subject: |
|
|
Hi,
Try this:
| Code: |
Option Explicit
'
' Define PX and PY as Dynamic Arrays
'
Private PX() As Single
Private PY() As Single
Private strFile As String
Private Function GetRandom() As Integer
GetRandom = Int(50 * Rnd + 50)
End Function
Private Sub FillArray(sngArray() As Single)
'
' For testing Purposes
' Fill the array with random numbers
'
Dim intI As Integer
Dim intJ As Integer
For intJ = LBound(sngArray, 1) To UBound(sngArray, 1)
For intI = LBound(sngArray, 2) To UBound(sngArray, 2)
sngArray(intJ, intI) = 50 * Rnd + 50
Next intI
Next intJ
End Sub
Private Sub SaveArray(sngArray() As Single, intFile As Integer)
'
' Save each element of the array
'
Dim intI As Integer
Dim intJ As Integer
For intJ = LBound(sngArray, 1) To UBound(sngArray, 1)
For intI = LBound(sngArray, 2) To UBound(sngArray, 2)
'
' The Format() statement is optional
' makes the file look 'pretty'
'
Print #1, Format(sngArray(intJ, intI), "####.## ");
Next intI
Print #1,
Next intJ
End Sub
Private Sub LoadArray(sngArray() As Single, intFile As Integer)
'
' Read each element in the same order they were saved
'
Dim intI As Integer
Dim intJ As Integer
For intJ = LBound(sngArray, 1) To UBound(sngArray, 1)
For intI = LBound(sngArray, 2) To UBound(sngArray, 2)
Input #intFile, sngArray(intJ, intI)
Next intI
Next intJ
End Sub
Private Sub cmdLoad_Click()
'
' Load the arrays from a file
'
Dim intFile As Integer
Dim intLx As Integer
Dim intLy As Integer
Dim intUx As Integer
Dim intUy As Integer
Dim intI As Integer
Dim intJ As Integer
intFile = FreeFile
Open strFile For Input As intFile
'
' The first 2 records are the dimensions of the arrays
'
Input #intFile, intLx, intLy, intUx, intUy
ReDim PX(intLx To intUx, intLy To intUy)
Input #intFile, intLx, intLy, intUx, intUy
ReDim PY(intLx To intUx, intLy To intUy)
'
' Load the arrays
'
Call LoadArray(PX, intFile)
Call LoadArray(PY, intFile)
Cloas intFile
MsgBox "Arrays Loaded"
End Sub
Private Sub cmdSave_Click()
'
' Save contents of arrays to a file
'
Dim intFile As Integer
intFile = FreeFile
Open strFile For Output As intFile
'
' Start off by saving the Dimensions of each array
' as the first 2 records so we know how to dimension the
' arrays when we want to load them
'
Print #intFile, LBound(PX, 1), LBound(PX, 2), UBound(PX, 1), UBound(PX, 2)
Print #intFile, LBound(PY, 1), LBound(PY, 2), UBound(PY, 1), UBound(PY, 2)
'
' Now save each array
'
Call SaveArray(PX, intFile)
Call SaveArray(PY, intFile)
Close intFile
MsgBox "Data Saved"
End Sub
Private Sub Form_Load()
Dim intRndX As Integer
Dim intRndY As Integer
Dim intI As Integer
'
' Set up some data in the Arrays
' for testing purposes
'
Randomize
'
' Create two random numbers to dimension
' each array between 50 and 100 elements
'
intRndX = GetRandom
intRndY = GetRandom
ReDim PX(intRndX, intRndY)
intRndX = GetRandom
intRndY = GetRandom
ReDim PY(intRndX, intRndY)
'
' Now generate some values for each array
'
Call FillArray(PX)
Call FillArray(PY)
strFile = "C:\TestArray.txt"
End Sub
|
Obviously, if the arrays static (ie.always have the same dimensions) you don't need to save the dimensions to the file and the ReDims will have to be removed.
Regards
Doug _________________ If you can see the light at the end of the tunnel, it probably means there's a Train coming. |
|