Results 1 to 4 of 4

Thread: Reading dynamic array from binary file.

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Angry

    Hey, I've some subscript out of range error in my coding.
    Can anyone help? or explain why?

    Code:
    Option Explicit
    Option Base 1
    
    Private Type Map_UDT1
        Rec As Long
        Pts As Long
    End Type
    Private MapInfo As Map_UDT1
    
    Private Type Map_POINT
        x As Single
        y As Single
    End Type
    
    Private Type Map_UDT2
        Data() As Map_POINT
    End Type
    Private MapPoint As Map_UDT2
    
    Private Sub SaveFile()
        Dim i As Long
        Dim p As Long
    
        If Dir(App.Path & "\map.tbl") <> "" Then Kill App.Path & "\map.tbl"
        If Dir(App.Path & "\map.dat") <> "" Then Kill App.Path & "\map.dat"
    
        For i = 1 to 10
            MapInfo.Rec = i
            MapInfo.Pts = Int(Rnd * 10) + 1
     
            'Save data into the Reference file
            Open App.Path & "\map.tbl" For Binary As #1 Len = Len(MapInfo)
                Put #1, LOF(1) + 1, MapInfo
            Close #1
    
            ReDim MapPoint.Data(MapInfo.Pts)
            Randomize Timer
            For p = 1 To MapInfo.Pts
                MapPoint.Data(p).x = (Rnd * 101) + 100
                MapPoint.Data(p).y = (Rnd * 3.1) + 3
            Next
        
            'Save data into the points data file
            Open App.Path & "\map.dat" For Binary As #1 Len = Len(MapPoint)
                Put #1, LOF(1) + 1, MapPoint
            Close #1
        Next    
    End Sub
    
    Private Sub ReadFile()
    Dim Ptr1 As Long
    Dim Ptr2
    Dim p As Long
    Dim str As String
    
    If Dir(App.Path & "\map.tbl") = "" Then
        MsgBox "Missing table file."
        Exit Sub
    End If
    
    If Dir(App.Path & "\map.dat") = "" Then
        MsgBox "Missing data file."
        Exit Sub
    End If
    
    Ptr1 = 1
    Ptr2 = 1
    Open App.Path & "\map.tbl" For Binary As #1 Len = Len(MapInfo)
        While Not EOF(1)
            Get #1, Ptr1, MapInfo
    
            If MapInfo.Pts > 0 Then
                ReDim MapPoint.Data(MapInfo.Pts)
                Open App.Path & "\map.dat" For Binary As #2 Len = Len(MapPoint)
                    Get #1, Ptr2, MapPoint
                Close #2
                
                'Update File Pointer
                Ptr2 = (Len(MapPoint) * Ptr2) + 1
                
                For p = 1 To MapInfo.Pts
                    'Why I hit the Subscript Out Of Range Error over here?
                    'I did declare the MapPoint.Data array before I open the file.
                    str = str & MapPoint.Data(p).x & ", " & MapPoint.Data(p).y & vbCrLf
                Next
            End If
            
            Debug.Print str
    
            'Update File Pointer
            Ptr1 = (Len(MapInfo) * MapInfo.Rec) + 1
        Wend
    Close #1
    End Sub

    [Edited by Chris on 01-17-2001 at 02:57 AM]

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Hi,

    From the code you've posted, you havent dimensioned the
    data() array.

    Try sticking a Redim MapPoint.Data(0)
    in the Form_Load() event.
    Then whenever you're adding to the array, do
    Redim Preserve MapPoint.Data(MapPoint.Data.Ubound + 1)
    MapPoint.Data(MapPoint.Data.Ubound).x = ...
    MapPoint.Data(MapPoint.Data.Ubound).y = ...

    Thats just off the top of my head so you'll probably need to mess around with the code a bit.

    - jamie.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    plenderj, I did declare before I ad the date into the Mappoint.Data() array.


    ReDim MapPoint.Data(MapInfo.Pts)
    Randomize Timer
    For p = 1 To MapInfo.Pts
    MapPoint.Data(p).x = (Rnd * 101) + 100
    MapPoint.Data(p).y = (Rnd * 3.1) + 3
    Next

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Sorry, missed that bit.

    Well, you using ReDim inside a for loop (for savefile) and inside a do loop (for readfile).
    They could be reducing the bounds of your arrays.
    I think the savefile one does anyway.
    I'd dimension the arrays, and then go into the loops.

    - jamie.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width