i made my own class module. and when i use ubound(cMy) i get subscript out of range. how can i fix this
Printable View
i made my own class module. and when i use ubound(cMy) i get subscript out of range. how can i fix this
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.
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
You'll have copy/paste your code related to problem described.
VB Code:
Public Maps() As cMap Public Sub dbConnect() Set DB = New ADODB.Connection Let DB.CursorLocation = adUseClient Call DB.Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DBHost & ";DATABASE=" & DBName, DBUser, DBPass) ReDim Players(0) As cPlayer ReDim Maps(0) As cMap End Sub Public Function dbLoadMap(Index As Long) As Boolean Dim Path As String Dim File As String Let Path = App.Path & "\maps\" Let File = Path & Index & ".DEM" If Index < LBound(Maps) Then Exit Function End If If Index > UBound(Maps) Then ReDim Preserve Maps(Index) As cMap End If Set Maps(Index) = New cMap Let dbLoadMap = Maps(Index).Load(File) End Function Public Sub dbLoadMaps() Dim Path As String Dim File As String Dim Count As Long Dim I As Long Let Path = App.Path & "\maps\" Let File = Path & "count.txt" Open File For Binary As #1 Get #1, , Count Close #1 ReDim Maps(Count) As cMap For I = 0 To Count Call dbLoadMap(I) Next End Sub Public Function dbSaveMap(Index As Long) As Boolean Dim Path As String Dim File As String Let Path = App.Path & "\maps\" Let File = Path & Index & ".DEM" If Index < LBound(Maps) Or Index > UBound(Maps) Then Exit Function End If Let dbSaveMap = Maps(Index).Save(File) End Function Public Sub dbSaveMaps() Dim Path As String Dim File As String Dim Count As Long Dim I As Long Let Path = App.Path & "\maps\" Let File = Path & "count.txt" [COLOR=purple][B]Let Count = UBound(Maps)[/B][/COLOR] Open File For Binary As #1 Put #1, , Count Close #1 For I = 0 To Count Call dbSaveMap(I) Next End Sub
guys - shouldn't it be
VB Code:
ubound(My,1)
1 stands for what?
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)
ug.... why does it work without 1 on a difrent array?
Never tried it as a default. Always used a 1 myself.
thanks for the sugestion but it dint work
To Correct this i added;
VB Code:
On Local Error Resume Next If Err.Number = 9 then Redim Maps(0) as CMap End if
... i know how you would think that would help. but the array is suposed to be already dimensioned...
its already dimensioned... so that code wont work
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:
Let Count = UBound(Maps) - 1
so what you mean is when the ubound is the same or lower as lbound it is "empty"
i dont get it
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.
yeah your right. but it loads maps from files.
what about
VB Code:
Global CMapCount as Integer Class CMap Sub Load(file as string) CMapCount = CMapCount + 1 'blah End Sub Sub Save(file as string CMapCount = CMapCount - 1 'blah End Sub End Class Count = CMapCount
i dont get it im kinda new to this
i know it's a bit of a cop out but it will do the same job
oh i think i know where you are getting to. but i jsut want ubound to work -.-
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:
Dim Maps() As cMap ReDim Maps(5) Debug.Print Maps(100)
belive it or not but it does
At what line exactly do you get the error ?
i highlighted it with bold and purple
And you call dbSaveMaps sub after you call dbConnect sub ?
lol yeah dbconnect is called before savemaps is called
VB Code:
If (Not ArrayName) = True Then 'the array is empty, you get an error if you use UBound or LBound Else 'the array has items, you can use LBound and UBound End If
:)
merry this is the strange thing; i can use lbound jsut fine...
and the ubound should be 5 because that is what count.tt contains
VB Code:
Public Sub dbSaveMaps() Dim Path As String Dim File As String Dim Count As Long Dim I As Long Let Path = App.Path & "\maps\" Let File = Path & "count.txt" 'this line added If (Not Maps) = True Then MsgBox "Can't save - no maps!": Exit Sub Let Count = UBound(Maps) Open File For Binary As #1 Put #1, , Count Close #1 For I = 0 To Count Call dbSaveMap(I) Next End Sub
What happens?
---------------------------
Dark Eternity Server
---------------------------
Can't save - no maps!
---------------------------
OK
---------------------------
So, you hadn't loaded maps before trying to save them. Quite simple :)
they are loaded...-.-
VB Code:
Public Maps() As cMap Public Sub dbConnect() Set DB = New ADODB.Connection Let DB.CursorLocation = adUseClient Call DB.Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DBHost & ";DATABASE=" & DBName, DBUser, DBPass) ReDim Players(0) As cPlayer ReDim Maps(0) As cMap Debug.Print "Maps initialized" End Sub Public Function dbLoadMap(Index As Long) As Boolean Dim Path As String Dim File As String Let Path = App.Path & "\maps\" Let File = Path & Index & ".DEM" If Index < LBound(Maps) Then Exit Function End If If Index > UBound(Maps) Then ReDim Preserve Maps(Index) As cMap End If Set Maps(Index) = New cMap Let dbLoadMap = Maps(Index).Load(File) Debug.Print "Loaded map " & Index End Function Public Sub dbLoadMaps() Dim Path As String Dim File As String Dim Count As Long Dim I As Long Let Path = App.Path & "\maps\" Let File = Path & "count.txt" Open File For Binary As #1 Get #1, , Count Close #1 Debug.Print "Ready to load " & Count + 1 & " maps" ReDim Maps(Count) As cMap For I = 0 To Count Call dbLoadMap(I) Next End Sub Public Function dbSaveMap(Index As Long) As Boolean Dim Path As String Dim File As String Let Path = App.Path & "\maps\" Let File = Path & Index & ".DEM" If Index < LBound(Maps) Or Index > UBound(Maps) Then Exit Function End If Let dbSaveMap = Maps(Index).Save(File) Debug.Print "Saved map " & Index End Function Public Sub dbSaveMaps() Dim Path As String Dim File As String Dim Count As Long Dim I As Long Let Path = App.Path & "\maps\" Let File = Path & "count.txt" If (Not Maps) = True Then Debug.Print "No maps to save": Exit Sub Let Count = UBound(Maps) Open File For Binary As #1 Put #1, , Count Close #1 For I = 0 To Count Call dbSaveMap(I) Next End Sub
What reads in the debug?
jup i stepped trough and they ARE loaded
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
VB Code:
Public Sub Destroy() Dim oForm As Form Dim I As Long Call sckStop For I = 0 To fServer.sClient.UBound Call sckClose(CInt(I)) Next Call dbSaveMaps Erase Maps Call dbSavePlayers Erase Players Call dbClose Call RmSysTray For Each oForm In Forms Call Unload(oForm) Set oForm = Nothing Next End Sub '******************************************************* Public Players() As cPlayer Public Maps() As cMap Public Sub dbConnect() Set DB = New ADODB.Connection Let DB.CursorLocation = adUseClient Call DB.Open("DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & DBHost & ";DATABASE=" & DBName, DBUser, DBPass) ReDim Players(0) As cPlayer ReDim Maps(0) As cMap Debug.Print "Maps initialized" End Sub Public Sub dbClose() Set DB = Nothing End Sub Public Function dbLoadPlayer(Index As Integer, Name As String, Pass As String) As Boolean Dim RS As New ADODB.Recordset If Index < LBound(Maps) Then Exit Function End If Call RS.Open("SELECT * FROM players WHERE name = '" & Name & "' AND pass = '" & Pass & "'", DB, adOpenStatic, adLockOptimistic) If RS.BOF And RS.EOF Then Exit Function End If If Index > UBound(Players) Then ReDim Preserve Players(Index) As cPlayer End If Set Players(Index) = New cPlayer Let Players(Index).Name = RS!Name Let Players(Index).Pass = RS!Pass Let Players(Index).Access = RS!Access Let Players(Index).Map = RS!Map Let Players(Index).X = RS!X Let Players(Index).Y = RS!Y Set RS = Nothing Let dbLoadPlayer = True End Function Public Function dbCreatePlayer(Index As Integer, Name As String, Pass As String) As Boolean Dim RS As New ADODB.Recordset If Index < LBound(Maps) Then Exit Function End If Call RS.Open("SELECT * FROM players", DB, adOpenStatic, adLockOptimistic) Call RS.AddNew If Index > UBound(Players) Then ReDim Preserve Players(Index) As cPlayer End If Set Players(Index) = New cPlayer Let Players(Index).Name = Name Let Players(Index).Pass = Pass Let RS!Name = Name Let RS!Pass = Pass Call RS.Update Set RS = Nothing Let dbCreatePlayer = True End Function Public Function dbSavePlayer(Index As Integer) As Boolean Dim RS As New ADODB.Recordset If Index < LBound(Players) Or Index > UBound(Players) Or Players(Index) Is Nothing Then Exit Function End If Call RS.Open("SELECT * FROM players WHERE name = '" & Players(Index).Name & "'", DB, adOpenStatic, adLockOptimistic) If RS.BOF And RS.EOF Then Call RS.AddNew End If Let RS!Name = Players(Index).Name Let RS!Pass = Players(Index).Pass Let RS!Access = Players(Index).Access Let RS!Map = Players(Index).Map Let RS!X = Players(Index).X Let RS!Y = Players(Index).Y Call RS.Update Set RS = Nothing Let dbSavePlayer = True End Function Public Sub dbSavePlayers() Dim I As Integer For I = 0 To UBound(Players) Call dbSavePlayer(I) Next End Sub Public Function dbLoadMap(Index As Long) As Boolean Dim Path As String Dim File As String Let Path = App.Path & "\maps\" Let File = Path & Index & ".DEM" If Index < LBound(Maps) Then Exit Function End If If Index > UBound(Maps) Then ReDim Preserve Maps(Index) As cMap End If Set Maps(Index) = New cMap Let dbLoadMap = Maps(Index).Load(File) Debug.Print "Loaded map " & Index End Function Public Sub dbLoadMaps() Dim Path As String Dim File As String Dim Count As Long Dim I As Long Let Path = App.Path & "\maps\" Let File = Path & "count.txt" Open File For Binary As #1 Get #1, , Count Close #1 If Count < 0 Then Let Count = 0 End If Debug.Print "Ready to load " & Count + 1 & " maps" ReDim Maps(Count) As cMap For I = 0 To Count Call dbLoadMap(I) Next End Sub Public Function dbSaveMap(Index As Long) As Boolean Dim Path As String Dim File As String Let Path = App.Path & "\maps\" Let File = Path & Index & ".DEM" If Index < LBound(Maps) Or Index > UBound(Maps) Then Exit Function End If Let dbSaveMap = Maps(Index).Save(File) Debug.Print "Saved map " & Index End Function Public Sub dbSaveMaps() Dim Path As String Dim File As String Dim Count As Long Dim I As Long Let Path = App.Path & "\maps\" Let File = Path & "count.txt" If (Not Maps) = True Then Debug.Print "No maps to save": Exit Sub Let Count = UBound(Maps) Open File For Binary As #1 Put #1, , Count Close #1 For I = 0 To Count Call dbSaveMap(I) Next End Sub