Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Tontow on April 20, 2004, 11:32 AM

Title: VB: run time error 9: sub script out of range
Post by: Tontow on April 20, 2004, 11:32 AM
im stumped, please help.

after the msgbox at 3000 it stops and i get a "run time error 9: sub script out of range"

and yes its a long text file


Public Sub writethis(StrMsg As String)
Dim intFile As Integer
Dim StrNew  As String
Dim StrBuffer As String
intFile = FreeFile
Open ("C:\WINDOWS\Desktop\Temperary\finished.txt") For Input As intFile
Do While Not EOF(intFile)
   Line Input #intFile, StrBuffer
   StrNew = StrNew & StrBuffer & vbCrLf
Loop
Close #intFile
StrNew = StrMsg & vbCrLf & StrNew
Open "C:\WINDOWS\Desktop\Temperary\finished.txt" For Output As #intFile
   Print #intFile, StrNew
Close #intFile
End Sub
'------------------------------------------------------------------------
Private Sub Form_Load()
Dim myArray() As String, buffer As String
Open "C:\WINDOWS\Desktop\Temperary\base.txt" For Binary Access Read As #1
buffer = Space$(LOF(1))
Get #1, , buffer
myArray = Split(buffer, vbCrLf)


Dim frist As String
Dim second As String
Dim third As String
Dim total As String
Dim total2 As String

frist = "set udg_SpecialEffectNumbers["
second = "]='"
third = "'" & vbCrLf
Close

Dim index As Integer
For index = 0 To 3371
total = total & frist & index & second & myArray(index) & third
Select Case index
   Case 1000
       MsgBox (index)
   Case 2000
       MsgBox (index)
   Case 3000
       MsgBox (index)
End Select

Next index

writethis total
writethis total2

MsgBox ("done")
End Sub
Title: Re:VB: run time error 9: sub script out of range
Post by: iago on April 20, 2004, 11:38 AM
I would imagine that "myArray(index)" is going off the end of the array (the subscript index is out of range of myArray).

<edit> Instead of looping 3371 times, isn't there a better way to do that in vb?  Like, myArray.upperBound() or something?  I can't remember vb that well, but I seem to recall something to get the upper bound of an array.

<edit2> also, shoudln't close have the reference number, like "close #1"?

<edit3> why are you opening the file for binary access?  Binary doesn't guarentee that vbcrlf's are detected correctly.
Title: Re:VB: run time error 9: sub script out of range
Post by: Eric on April 20, 2004, 12:35 PM
Quote from: iago on April 20, 2004, 11:38 AM
<edit> Instead of looping 3371 times, isn't there a better way to do that in vb?  Like, myArray.upperBound() or something?  I can't remember vb that well, but I seem to recall something to get the upper bound of an array.


For I = 0 To UBound(myArray())
      'Blah
Next I
Title: Re:VB: run time error 9: sub script out of range
Post by: iago on April 20, 2004, 12:51 PM
Aha, UBound.  Thanks :)

So you'll probably want to do
for i = 0 to UBound(myArray) -1
...

next i
Title: Re:VB: run time error 9: sub script out of range
Post by: Eli_1 on April 20, 2004, 06:06 PM
No.

Dim Blah(15) as String
' // 16 elements in this array (0-15)
' // In C/C++ Blah[15] is 15 elements (0-14 -- 15 does not exist).
Blah(15) = "weee"

is perfectly legal in VB, as compared to C/C++ where blah[15] would be a "fence post error."

So:

For i = 0 to UBound(Blah)
   ...
Next i
is correct.
Title: Re:VB: run time error 9: sub script out of range
Post by: Grok on April 20, 2004, 06:52 PM
Holy Mackeral!!!

VB is not installed on this computer, but please let me rewrite it for you ... you'll have to syntax check it though.  Now I left out the MsgBox at 1000, 2000, and 3000 linecount, since I figured those were just progress indicators for you.  Just from looking at your existing code, it has to be way slow.  This version will be much faster --

Public Sub AppendMessage(strMsg As String)
   Dim pfNum As Integer
   Dim sFile As String
   sFile = "C:\WINDOWS\Desktop\Temperary\finished.txt"
   pfNum = FreeFile
   Open sFile For Append As #pfNum
   Print #pfNum, strMsg
   Close #pfNum
End Sub

Private Sub Form_Load()
   Dim fNum As Integer
   Dim msgLine As String
   Dim s1 As String, s2 As String, s3 As String
   s1 = "set udg_SpecialEffectNumbers["
   s2 = "]='"
   s3 = "'"
   sFile = "C:\WINDOWS\Desktop\Temperary\base.txt"
   pfNum = FreeFile
   Open sFile For Input As #pfNum
   Do While EOF(pfNum) = False
       Line Input #pfNum, msgLine
       msgLine = s1 & lineCount & S2 & msgLine & s3
       AppendMessage msgLine
   Loop
   Close #pfNum    
   MsgBox "Done"
End Sub
Title: Re:VB: run time error 9: sub script out of range
Post by: Eibro on April 20, 2004, 11:34 PM
Quote from: Eli_1 on April 20, 2004, 06:06 PM
No.

Dim Blah(15) as String
' // 16 elements in this array (0-15)
' // In C/C++ Blah[15] is 15 elements (0-14 -- 15 does not exist).
Blah(15) = "weee"

is perfectly legal in VB, as compared to C/C++ where blah[15] would be a "fence post error."

So:

For i = 0 to UBound(Blah)
   ...
Next i
is correct.
IIRC, Dim Blah(15) as String will create a string with 15 elements ( 1-15 ). Unless Option Base 0 is specified, in which case elements would be numbered ( 0-14 ), as in most other languages.
Title: Re:VB: run time error 9: sub script out of range
Post by: Stealth on April 21, 2004, 12:20 AM
It's the other way around -- Option Base 1 will start the array at 1, by default Array(15) will be 0 to 15. Additionally, you can specify Array(# to #) for a more specific size.

Quote
So you'll probably want to do
for i = 0 to UBound(myArray) -1
...

next i

The UBound function returns the proper upper boundary of the array, not the number of elements as it does elsewhere, so you don't have to loop to UBound - 1. :)
Title: Re:VB: run time error 9: sub script out of range
Post by: Eli_1 on April 21, 2004, 05:56 AM
Quote from: Eibro on April 20, 2004, 11:34 PMUnless Option Base 0 is specified, in which case elements would be numbered ( 0-14 ), as in most other languages.
Option base 0 is default and would set from 0 to 15.
Title: Re:VB: run time error 9: sub script out of range
Post by: Tontow on April 21, 2004, 10:14 PM
Quote from: Grok on April 20, 2004, 06:52 PM
Holy Mackeral!!!

VB is not installed on this computer, but please let me rewrite it for you ... you'll have to syntax check it though.  Now I left out the MsgBox at 1000, 2000, and 3000 linecount, since I figured those were just progress indicators for you.  Just from looking at your existing code, it has to be way slow.  This version will be much faster --

Public Sub AppendMessage(strMsg As String)
   Dim pfNum As Integer
   Dim sFile As String
   sFile = "C:\WINDOWS\Desktop\Temperary\finished.txt"
   pfNum = FreeFile
   Open sFile For Append As #pfNum
   Print #pfNum, strMsg
   Close #pfNum
End Sub

Private Sub Form_Load()
   Dim fNum As Integer
   Dim msgLine As String
   Dim s1 As String, s2 As String, s3 As String
   s1 = "set udg_SpecialEffectNumbers["
   s2 = "]='"
   s3 = "'"
   sFile = "C:\WINDOWS\Desktop\Temperary\base.txt"
   pfNum = FreeFile
   Open sFile For Input As #pfNum
   Do While EOF(pfNum) = False
       Line Input #pfNum, msgLine
       msgLine = s1 & lineCount & S2 & msgLine & s3
       AppendMessage msgLine
   Loop
   Close #pfNum    
   MsgBox "Done"
End Sub


thank you, thats much faster but its not putting in the number between  the [], wich is kinda improtaint
Title: Re:VB: run time error 9: sub script out of range
Post by: Grok on April 21, 2004, 10:45 PM
Quote from: Tontow on April 21, 2004, 10:14 PM
Quote from: Grok on April 20, 2004, 06:52 PM
Holy Mackeral!!!

VB is not installed on this computer, but please let me rewrite it for you ... you'll have to syntax check it though.  Now I left out the MsgBox at 1000, 2000, and 3000 linecount, since I figured those were just progress indicators for you.  Just from looking at your existing code, it has to be way slow.  This version will be much faster --

Public Sub AppendMessage(strMsg As String)
   Dim pfNum As Integer
   Dim sFile As String
   sFile = "C:\WINDOWS\Desktop\Temperary\finished.txt"
   pfNum = FreeFile
   Open sFile For Append As #pfNum
   Print #pfNum, strMsg
   Close #pfNum
End Sub

Private Sub Form_Load()
   Dim fNum As Integer
   Dim msgLine As String
   Dim lineCount As Long
   Dim s1 As String, s2 As String, s3 As String
   s1 = "set udg_SpecialEffectNumbers["
   s2 = "]='"
   s3 = "'"
   sFile = "C:\WINDOWS\Desktop\Temperary\base.txt"
   pfNum = FreeFile
   Open sFile For Input As #pfNum
   lineCount = 0
   Do While EOF(pfNum) = False
       Line Input #pfNum, msgLine
       lineCount = lineCount + 1
       msgLine = s1 & lineCount & S2 & msgLine & s3
       AppendMessage msgLine
   Loop
   Close #pfNum    
   MsgBox "Done"
End Sub


thank you, thats much faster but its not putting in the number between  the [], wich is kinda improtaint

Fixed.
Title: Re:VB: run time error 9: sub script out of range
Post by: Tontow on April 22, 2004, 04:36 PM
thank you very much