Page 1 of 2 12 LastLast
Results 1 to 40 of 44

Thread: ubound; subscript out of range

  1. #1

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Resolved ubound; subscript out of range

    i made my own class module. and when i use ubound(cMy) i get subscript out of range. how can i fix this
    Last edited by nareth; Oct 11th, 2004 at 02:45 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    UBound() function returns upper bound for an array and if array is ampty error occurs. You have to have an error handler to trap that error.

  3. #3

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    well its not empty i declared it as Dim My() As New cMy then way before using UBound i do ReDim My(0) As New cMy then i use UBound and it gives that error while LBound works and UBound always gives the error no matter the real UBound

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You'll have copy/paste your code related to problem described.

  5. #5

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    VB Code:
    1. Public Maps() As cMap
    2.  
    3. Public Sub dbConnect()
    4.     Set DB = New ADODB.Connection
    5.     Let DB.CursorLocation = adUseClient
    6.     Call DB.Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DBHost & ";DATABASE=" & DBName, DBUser, DBPass)
    7.    
    8.     ReDim Players(0) As cPlayer
    9.     ReDim Maps(0) As cMap
    10. End Sub
    11.  
    12. Public Function dbLoadMap(Index As Long) As Boolean
    13.     Dim Path As String
    14.     Dim File As String
    15.    
    16.     Let Path = App.Path & "\maps\"
    17.     Let File = Path & Index & ".DEM"
    18.    
    19.     If Index < LBound(Maps) Then
    20.         Exit Function
    21.     End If
    22.    
    23.     If Index > UBound(Maps) Then
    24.         ReDim Preserve Maps(Index) As cMap
    25.     End If
    26.    
    27.     Set Maps(Index) = New cMap
    28.     Let dbLoadMap = Maps(Index).Load(File)
    29. End Function
    30.  
    31. Public Sub dbLoadMaps()
    32.     Dim Path As String
    33.     Dim File As String
    34.     Dim Count As Long
    35.     Dim I As Long
    36.    
    37.     Let Path = App.Path & "\maps\"
    38.     Let File = Path & "count.txt"
    39.    
    40.     Open File For Binary As #1
    41.         Get #1, , Count
    42.     Close #1
    43.    
    44.     ReDim Maps(Count) As cMap
    45.     For I = 0 To Count
    46.         Call dbLoadMap(I)
    47.     Next
    48. End Sub
    49.  
    50. Public Function dbSaveMap(Index As Long) As Boolean
    51.     Dim Path As String
    52.     Dim File As String
    53.    
    54.     Let Path = App.Path & "\maps\"
    55.     Let File = Path & Index & ".DEM"
    56.    
    57.     If Index < LBound(Maps) Or Index > UBound(Maps) Then
    58.         Exit Function
    59.     End If
    60.    
    61.     Let dbSaveMap = Maps(Index).Save(File)
    62. End Function
    63.  
    64. Public Sub dbSaveMaps()
    65.     Dim Path As String
    66.     Dim File As String
    67.     Dim Count As Long
    68.     Dim I As Long
    69.    
    70.     Let Path = App.Path & "\maps\"
    71.     Let File = Path & "count.txt"
    72.     [COLOR=purple][B]Let Count = UBound(Maps)[/B][/COLOR]
    73.    
    74.     Open File For Binary As #1
    75.         Put #1, , Count
    76.     Close #1
    77.    
    78.     For I = 0 To Count
    79.         Call dbSaveMap(I)
    80.     Next
    81. End Sub
    Last edited by nareth; Oct 11th, 2004 at 12:40 PM.

  6. #6
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    guys - shouldn't it be

    VB Code:
    1. ubound(My,1)
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  7. #7

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    1 stands for what?

  8. #8
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    you have to give the ubound a column to work on. On a single dimention array it will be a 1.

    On a 2 dimentional array it may be

    ubound(My,1)

    or

    ubound(My,2)
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  9. #9

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    ug.... why does it work without 1 on a difrent array?

  10. #10
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Never tried it as a default. Always used a 1 myself.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  11. #11

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    thanks for the sugestion but it dint work

  12. #12
    Addicted Member DigitalMyth's Avatar
    Join Date
    Nov 2002
    Location
    England..
    Posts
    169
    To Correct this i added;

    VB Code:
    1. On Local Error Resume Next
    2.  
    3. If Err.Number = 9 then
    4. Redim Maps(0) as CMap
    5. End if

  13. #13

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    ... i know how you would think that would help. but the array is suposed to be already dimensioned...

  14. #14

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    its already dimensioned... so that code wont work

  15. #15
    Addicted Member DigitalMyth's Avatar
    Join Date
    Nov 2002
    Location
    England..
    Posts
    169
    Ubound

    If i think this script does what i think it does; the Ubound is empty, this is what's making it error, have your try!!

    VB Code:
    1. Let Count = UBound(Maps) - 1

  16. #16

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    so what you mean is when the ubound is the same or lower as lbound it is "empty"

  17. #17

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    i dont get it

  18. #18
    Addicted Member DigitalMyth's Avatar
    Join Date
    Nov 2002
    Location
    England..
    Posts
    169
    from this code i can't see whats up with your code....

    I take it that this is a game, that runs from a SQL database.

  19. #19

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    yeah your right. but it loads maps from files.

  20. #20
    Addicted Member DigitalMyth's Avatar
    Join Date
    Nov 2002
    Location
    England..
    Posts
    169
    what about


    VB Code:
    1. Global CMapCount as Integer
    2.  
    3.  
    4. Class CMap
    5.  
    6.  
    7. Sub Load(file as string)
    8. CMapCount = CMapCount + 1
    9. 'blah
    10. End Sub
    11.  
    12.  
    13. Sub Save(file as string
    14. CMapCount = CMapCount - 1
    15. 'blah
    16. End Sub
    17.  
    18.  
    19. End Class
    20.  
    21. Count = CMapCount

  21. #21

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    i dont get it im kinda new to this

  22. #22
    Addicted Member DigitalMyth's Avatar
    Join Date
    Nov 2002
    Location
    England..
    Posts
    169
    i know it's a bit of a cop out but it will do the same job

  23. #23

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    oh i think i know where you are getting to. but i jsut want ubound to work -.-

  24. #24
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I don't think the UBound returns an error of "subscript out of range"

    You will get a subscript out of range" error when you do something like this:
    VB Code:
    1. Dim Maps() As cMap
    2. ReDim Maps(5)
    3.  
    4. Debug.Print Maps(100)

  25. #25

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    belive it or not but it does

  26. #26
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    At what line exactly do you get the error ?

  27. #27

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    i highlighted it with bold and purple

  28. #28
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    And you call dbSaveMaps sub after you call dbConnect sub ?

  29. #29

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    lol yeah dbconnect is called before savemaps is called

  30. #30
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    VB Code:
    1. If (Not ArrayName) = True Then
    2.     'the array is empty, you get an error if you use UBound or LBound
    3. Else
    4.     'the array has items, you can use LBound and UBound
    5. End If


  31. #31

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    merry this is the strange thing; i can use lbound jsut fine...

  32. #32

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    and the ubound should be 5 because that is what count.tt contains

  33. #33
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    VB Code:
    1. Public Sub dbSaveMaps()
    2.     Dim Path As String
    3.     Dim File As String
    4.     Dim Count As Long
    5.     Dim I As Long
    6.         Let Path = App.Path & "\maps\"
    7.     Let File = Path & "count.txt"
    8.     'this line added
    9.     If (Not Maps) = True Then MsgBox "Can't save - no maps!": Exit Sub
    10.     Let Count = UBound(Maps)
    11.  
    12.     Open File For Binary As #1
    13.         Put #1, , Count
    14.     Close #1
    15.    
    16.     For I = 0 To Count
    17.         Call dbSaveMap(I)
    18.     Next
    19. End Sub

    What happens?

  34. #34

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    ---------------------------
    Dark Eternity Server
    ---------------------------
    Can't save - no maps!
    ---------------------------
    OK
    ---------------------------

  35. #35
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    So, you hadn't loaded maps before trying to save them. Quite simple

  36. #36

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    they are loaded...-.-

  37. #37
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    VB Code:
    1. Public Maps() As cMap
    2.  
    3. Public Sub dbConnect()
    4.     Set DB = New ADODB.Connection
    5.     Let DB.CursorLocation = adUseClient
    6.     Call DB.Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DBHost & ";DATABASE=" & DBName, DBUser, DBPass)
    7.    
    8.     ReDim Players(0) As cPlayer
    9.     ReDim Maps(0) As cMap
    10.     Debug.Print "Maps initialized"
    11. End Sub
    12.  
    13. Public Function dbLoadMap(Index As Long) As Boolean
    14.     Dim Path As String
    15.     Dim File As String
    16.         Let Path = App.Path & "\maps\"
    17.     Let File = Path & Index & ".DEM"
    18.    
    19.     If Index < LBound(Maps) Then
    20.         Exit Function
    21.     End If
    22.         If Index > UBound(Maps) Then
    23.         ReDim Preserve Maps(Index) As cMap
    24.     End If
    25.         Set Maps(Index) = New cMap
    26.     Let dbLoadMap = Maps(Index).Load(File)
    27.     Debug.Print "Loaded map " & Index
    28. End Function
    29.  
    30. Public Sub dbLoadMaps()
    31.     Dim Path As String
    32.     Dim File As String
    33.     Dim Count As Long
    34.     Dim I As Long
    35.         Let Path = App.Path & "\maps\"
    36.     Let File = Path & "count.txt"
    37.    
    38.     Open File For Binary As #1
    39.         Get #1, , Count
    40.     Close #1
    41.    
    42.     Debug.Print "Ready to load " & Count + 1 & " maps"
    43.     ReDim Maps(Count) As cMap
    44.     For I = 0 To Count
    45.         Call dbLoadMap(I)
    46.     Next
    47. End Sub
    48.  
    49. Public Function dbSaveMap(Index As Long) As Boolean
    50.     Dim Path As String
    51.     Dim File As String
    52.         Let Path = App.Path & "\maps\"
    53.     Let File = Path & Index & ".DEM"
    54.    
    55.     If Index < LBound(Maps) Or Index > UBound(Maps) Then
    56.         Exit Function
    57.     End If
    58.  
    59.     Let dbSaveMap = Maps(Index).Save(File)
    60.     Debug.Print "Saved map " & Index
    61. End Function
    62.  
    63. Public Sub dbSaveMaps()
    64.     Dim Path As String
    65.     Dim File As String
    66.     Dim Count As Long
    67.     Dim I As Long
    68.         Let Path = App.Path & "\maps\"
    69.     Let File = Path & "count.txt"
    70.     If (Not Maps) = True Then Debug.Print "No maps to save": Exit Sub
    71.     Let Count = UBound(Maps)
    72.         Open File For Binary As #1
    73.         Put #1, , Count
    74.     Close #1
    75.    
    76.     For I = 0 To Count
    77.         Call dbSaveMap(I)
    78.     Next
    79. End Sub

    What reads in the debug?

  38. #38

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    jup i stepped trough and they ARE loaded

  39. #39

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    NOW THIS IS WIERD

    Maps initialized
    Ready to load 6 maps
    Loaded map 0
    Loaded map 1
    Loaded map 2
    Loaded map 3
    Loaded map 4
    Loaded map 5
    Saved map 0
    Saved map 1
    Saved map 2
    Saved map 3
    Saved map 4
    Saved map 5
    No maps to save

  40. #40

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    VB Code:
    1. Public Sub Destroy()
    2.     Dim oForm As Form
    3.     Dim I As Long
    4.    
    5.     Call sckStop
    6.     For I = 0 To fServer.sClient.UBound
    7.         Call sckClose(CInt(I))
    8.     Next
    9.    
    10.     Call dbSaveMaps
    11.     Erase Maps
    12.     Call dbSavePlayers
    13.     Erase Players
    14.     Call dbClose
    15.    
    16.     Call RmSysTray
    17.    
    18.     For Each oForm In Forms
    19.         Call Unload(oForm)
    20.         Set oForm = Nothing
    21.     Next
    22. End Sub
    23.  
    24. '*******************************************************
    25. Public Players() As cPlayer
    26. Public Maps() As cMap
    27.  
    28. Public Sub dbConnect()
    29.     Set DB = New ADODB.Connection
    30.     Let DB.CursorLocation = adUseClient
    31.     Call DB.Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DBHost & ";DATABASE=" & DBName, DBUser, DBPass)
    32.    
    33.     ReDim Players(0) As cPlayer
    34.     ReDim Maps(0) As cMap
    35.     Debug.Print "Maps initialized"
    36. End Sub
    37.  
    38. Public Sub dbClose()
    39.     Set DB = Nothing
    40. End Sub
    41.  
    42. Public Function dbLoadPlayer(Index As Integer, Name As String, Pass As String) As Boolean
    43.     Dim RS As New ADODB.Recordset
    44.    
    45.     If Index < LBound(Maps) Then
    46.         Exit Function
    47.     End If
    48.    
    49.     Call RS.Open("SELECT * FROM players WHERE name = '" & Name & "' AND pass = '" & Pass & "'", DB, adOpenStatic, adLockOptimistic)
    50.    
    51.     If RS.BOF And RS.EOF Then
    52.         Exit Function
    53.     End If
    54.    
    55.     If Index > UBound(Players) Then
    56.         ReDim Preserve Players(Index) As cPlayer
    57.     End If
    58.    
    59.     Set Players(Index) = New cPlayer
    60.     Let Players(Index).Name = RS!Name
    61.     Let Players(Index).Pass = RS!Pass
    62.     Let Players(Index).Access = RS!Access
    63.     Let Players(Index).Map = RS!Map
    64.     Let Players(Index).X = RS!X
    65.     Let Players(Index).Y = RS!Y
    66.    
    67.     Set RS = Nothing
    68.     Let dbLoadPlayer = True
    69. End Function
    70.  
    71. Public Function dbCreatePlayer(Index As Integer, Name As String, Pass As String) As Boolean
    72.     Dim RS As New ADODB.Recordset
    73.    
    74.     If Index < LBound(Maps) Then
    75.         Exit Function
    76.     End If
    77.    
    78.     Call RS.Open("SELECT * FROM players", DB, adOpenStatic, adLockOptimistic)
    79.     Call RS.AddNew
    80.    
    81.     If Index > UBound(Players) Then
    82.         ReDim Preserve Players(Index) As cPlayer
    83.     End If
    84.    
    85.     Set Players(Index) = New cPlayer
    86.     Let Players(Index).Name = Name
    87.     Let Players(Index).Pass = Pass
    88.    
    89.     Let RS!Name = Name
    90.     Let RS!Pass = Pass
    91.     Call RS.Update
    92.    
    93.     Set RS = Nothing
    94.     Let dbCreatePlayer = True
    95. End Function
    96.  
    97. Public Function dbSavePlayer(Index As Integer) As Boolean
    98.     Dim RS As New ADODB.Recordset
    99.    
    100.     If Index < LBound(Players) Or Index > UBound(Players) Or Players(Index) Is Nothing Then
    101.         Exit Function
    102.     End If
    103.    
    104.     Call RS.Open("SELECT * FROM players WHERE name = '" & Players(Index).Name & "'", DB, adOpenStatic, adLockOptimistic)
    105.    
    106.     If RS.BOF And RS.EOF Then
    107.         Call RS.AddNew
    108.     End If
    109.    
    110.     Let RS!Name = Players(Index).Name
    111.     Let RS!Pass = Players(Index).Pass
    112.     Let RS!Access = Players(Index).Access
    113.     Let RS!Map = Players(Index).Map
    114.     Let RS!X = Players(Index).X
    115.     Let RS!Y = Players(Index).Y
    116.     Call RS.Update
    117.    
    118.     Set RS = Nothing
    119.     Let dbSavePlayer = True
    120. End Function
    121.  
    122. Public Sub dbSavePlayers()
    123.     Dim I As Integer
    124.  
    125.     For I = 0 To UBound(Players)
    126.         Call dbSavePlayer(I)
    127.     Next
    128. End Sub
    129.  
    130. Public Function dbLoadMap(Index As Long) As Boolean
    131.     Dim Path As String
    132.     Dim File As String
    133.    
    134.     Let Path = App.Path & "\maps\"
    135.     Let File = Path & Index & ".DEM"
    136.    
    137.     If Index < LBound(Maps) Then
    138.         Exit Function
    139.     End If
    140.    
    141.     If Index > UBound(Maps) Then
    142.         ReDim Preserve Maps(Index) As cMap
    143.     End If
    144.    
    145.     Set Maps(Index) = New cMap
    146.     Let dbLoadMap = Maps(Index).Load(File)
    147.     Debug.Print "Loaded map " & Index
    148. End Function
    149.  
    150. Public Sub dbLoadMaps()
    151.     Dim Path As String
    152.     Dim File As String
    153.     Dim Count As Long
    154.     Dim I As Long
    155.    
    156.     Let Path = App.Path & "\maps\"
    157.     Let File = Path & "count.txt"
    158.    
    159.     Open File For Binary As #1
    160.         Get #1, , Count
    161.     Close #1
    162.  
    163.     If Count < 0 Then
    164.         Let Count = 0
    165.     End If
    166.    
    167.     Debug.Print "Ready to load " & Count + 1 & " maps"
    168.     ReDim Maps(Count) As cMap
    169.     For I = 0 To Count
    170.         Call dbLoadMap(I)
    171.     Next
    172. End Sub
    173.  
    174. Public Function dbSaveMap(Index As Long) As Boolean
    175.     Dim Path As String
    176.     Dim File As String
    177.    
    178.     Let Path = App.Path & "\maps\"
    179.     Let File = Path & Index & ".DEM"
    180.    
    181.     If Index < LBound(Maps) Or Index > UBound(Maps) Then
    182.         Exit Function
    183.     End If
    184.    
    185.     Let dbSaveMap = Maps(Index).Save(File)
    186.     Debug.Print "Saved map " & Index
    187. End Function
    188.  
    189. Public Sub dbSaveMaps()
    190.     Dim Path As String
    191.     Dim File As String
    192.     Dim Count As Long
    193.     Dim I As Long
    194.    
    195.     Let Path = App.Path & "\maps\"
    196.     Let File = Path & "count.txt"
    197.     If (Not Maps) = True Then Debug.Print "No maps to save": Exit Sub
    198.     Let Count = UBound(Maps)
    199.    
    200.     Open File For Binary As #1
    201.         Put #1, , Count
    202.     Close #1
    203.    
    204.     For I = 0 To Count
    205.         Call dbSaveMap(I)
    206.     Next
    207. End Sub

Page 1 of 2 12 LastLast

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