|
-
Oct 11th, 2004, 10:46 AM
#1
Thread Starter
Banned
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.
-
Oct 11th, 2004, 11:10 AM
#2
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.
-
Oct 11th, 2004, 11:14 AM
#3
Thread Starter
Banned
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
-
Oct 11th, 2004, 11:19 AM
#4
You'll have copy/paste your code related to problem described.
-
Oct 11th, 2004, 11:23 AM
#5
Thread Starter
Banned
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
Last edited by nareth; Oct 11th, 2004 at 12:40 PM.
-
Oct 11th, 2004, 12:19 PM
#6
Frenzied Member
-
Oct 11th, 2004, 12:20 PM
#7
Thread Starter
Banned
-
Oct 11th, 2004, 12:22 PM
#8
Frenzied Member
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)
-
Oct 11th, 2004, 12:25 PM
#9
Thread Starter
Banned
ug.... why does it work without 1 on a difrent array?
-
Oct 11th, 2004, 12:27 PM
#10
Frenzied Member
Never tried it as a default. Always used a 1 myself.
-
Oct 11th, 2004, 12:27 PM
#11
Thread Starter
Banned
thanks for the sugestion but it dint work
-
Oct 11th, 2004, 12:41 PM
#12
Addicted Member
To Correct this i added;
VB Code:
On Local Error Resume Next
If Err.Number = 9 then
Redim Maps(0) as CMap
End if
-
Oct 11th, 2004, 12:42 PM
#13
Thread Starter
Banned
... i know how you would think that would help. but the array is suposed to be already dimensioned...
-
Oct 11th, 2004, 12:44 PM
#14
Thread Starter
Banned
its already dimensioned... so that code wont work
-
Oct 11th, 2004, 12:47 PM
#15
Addicted Member
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
-
Oct 11th, 2004, 12:55 PM
#16
Thread Starter
Banned
so what you mean is when the ubound is the same or lower as lbound it is "empty"
-
Oct 11th, 2004, 01:05 PM
#17
Thread Starter
Banned
-
Oct 11th, 2004, 01:09 PM
#18
Addicted Member
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.
-
Oct 11th, 2004, 01:09 PM
#19
Thread Starter
Banned
yeah your right. but it loads maps from files.
-
Oct 11th, 2004, 01:17 PM
#20
Addicted Member
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
-
Oct 11th, 2004, 01:20 PM
#21
Thread Starter
Banned
i dont get it im kinda new to this
-
Oct 11th, 2004, 01:20 PM
#22
Addicted Member
i know it's a bit of a cop out but it will do the same job
-
Oct 11th, 2004, 01:26 PM
#23
Thread Starter
Banned
oh i think i know where you are getting to. but i jsut want ubound to work -.-
-
Oct 11th, 2004, 01:30 PM
#24
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)
-
Oct 11th, 2004, 01:32 PM
#25
Thread Starter
Banned
belive it or not but it does
-
Oct 11th, 2004, 01:38 PM
#26
At what line exactly do you get the error ?
-
Oct 11th, 2004, 01:41 PM
#27
Thread Starter
Banned
i highlighted it with bold and purple
-
Oct 11th, 2004, 01:48 PM
#28
And you call dbSaveMaps sub after you call dbConnect sub ?
-
Oct 11th, 2004, 01:50 PM
#29
Thread Starter
Banned
lol yeah dbconnect is called before savemaps is called
-
Oct 11th, 2004, 01:57 PM
#30
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
-
Oct 11th, 2004, 01:58 PM
#31
Thread Starter
Banned
merry this is the strange thing; i can use lbound jsut fine...
-
Oct 11th, 2004, 02:08 PM
#32
Thread Starter
Banned
and the ubound should be 5 because that is what count.tt contains
-
Oct 11th, 2004, 02:14 PM
#33
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?
-
Oct 11th, 2004, 02:19 PM
#34
Thread Starter
Banned
---------------------------
Dark Eternity Server
---------------------------
Can't save - no maps!
---------------------------
OK
---------------------------
-
Oct 11th, 2004, 02:22 PM
#35
So, you hadn't loaded maps before trying to save them. Quite simple
-
Oct 11th, 2004, 02:24 PM
#36
Thread Starter
Banned
-
Oct 11th, 2004, 02:27 PM
#37
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?
-
Oct 11th, 2004, 02:28 PM
#38
Thread Starter
Banned
jup i stepped trough and they ARE loaded
-
Oct 11th, 2004, 02:32 PM
#39
Thread Starter
Banned
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
-
Oct 11th, 2004, 02:35 PM
#40
Thread Starter
Banned
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|