Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 "Array Subscript out of Range"... I'm stumped.
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ZeroPaladn




PostPosted: Thu Jan 11, 2007 2:42 pm   Post subject: "Array Subscript out of Range"... I'm stumped.

VisualBASIC:
Option Explicit
Dim libraryArray() As String ' Stores Library Contents
Dim i As Integer, j As Integer, m As Integer, l As Integer ' Reserved for FOR LOOPS
Dim helpTopic As String ' Carries the help topic string used for the mini help
Dim addFlag As Boolean ' Used as a flag to determine whether a new item can be added

Private Sub Form_Load() ' Loads file to be used by the program
    Open "H:\ICS 3MI\Visual Basic\Final Project\libraryFile.txt" For Input As 1
    Do Until EOF(1) ' Exits file when it reaches the end of it
        ReDim libraryArray(i, 2) ' Reset libraryArray so it can accept more enteries
        Input #1, libraryArray(i, 0), libraryArray(i, 1), libraryArray(i, 2)
        txtDisplay.Text = txtDisplay.Text + libraryArray(i, 0) + ", " + libraryArray(i, 1) + ", " + libraryArray(i, 2) + vbNewLine
        i = i + 1 ' Adds one to itself, used with the ReDim function so it can accept more enteries
    Loop
    Close #1
    txtAddNumber.MaxLength = 3
End Sub

Private Sub btnAdd_Click()
    For j = 0 To i Step 1
        If txtAddArtist.Text = libraryArray(i, 1) And txtAddAlbum.Text = libraryArray(i, 0) Then
            addFlag = False
        ElseIf txtAddNumber.Text = libraryArray(i, 2) Then
            addFlag = False
        Else
            addFlag = True
        End If
    Next j
    If addFlag = True Then
        ReDim libraryArray(i + 1, 2)
            Open "H:\ICS 3MI\Visual Basic\Final Project\libraryFile.txt" For Input As 1
            Write #1, txtAddAlbum.Text, txtAddArtist.Text, txtAddNumber.Text
            Close #1
            Open "H:\ICS 3MI\Visual Basic\Final Project\libraryFile.txt" For Input As 1
            txtDisplay.Text = ""
            For m = 1 To i Step 1
                Input #1, libraryArray(i, 0), libraryArray(i, 1), libraryArray(i, 2)
                txtDisplay.Text = txtDisplay.Text + libraryArray(i, 0) + ", " + libraryArray(i, 1) + ", " + libraryArray(i, 2) + vbNewLine
            Next m
            Close #1
        Else
            MsgBox ("Invalid entry. Click on the Help button and type 'add' for more info.")
    End If
End Sub

This is a library program (for my [FP] for school and CompSci) and im puzzled. I load a file with 15 strings in it, like this.
[syntax=".txt file"]Billy Talent II,Billy Talent,001
Crisis,Alexisonfire,002
Hybrid Theory,Linkin Park,003
The Sufferer and the Witness,Rise Against,004
Meteora,Linkin Park,005[/syntax]

It all loads fine, but when i go to add new item to this list, it searches through the available enteries that i store into a flexible array upon loading the form. when it searches through the array and comparing it to the text inside the text boxes in my form, i get the array subscript out of range thingy. I'm stumped, cause i is used in the size of the array along with the ending point for the for loop.

VisualBASIC:
If txtAddArtist.Text = libraryArray(i, 1) And txtAddAlbum.Text = libraryArray(i, 0) Then ' This is where i get the error. i = 5 when i trace executed it.

Can anyone shed some light on this, greatly apreciated.
Sponsor
Sponsor
Sponsor
sponsor
Silent Avenger




PostPosted: Thu Jan 11, 2007 4:13 pm   Post subject: Re: "Array Subscript out of Range"... I'm stumped.

Try putting a stop in and checking what the value for I is when you check the variable. Also make sure that the variable i is not being changed or used for anything else between the time you make the size of the array and when you do the decision statement.
Monstrosity_




PostPosted: Thu Jan 11, 2007 11:21 pm   Post subject: Re: "Array Subscript out of Range"... I'm stumped.

ZeroPaladn @ Thu Jan 11, 2007 3:42 pm wrote:
It all loads fine, but when i go to add new item to this list, it searches through the available enteries that i store into a flexible array upon loading the form. when it searches through the array and comparing it to the text inside the text boxes in my form, i get the array subscript out of range thingy. I'm stumped, cause i is used in the size of the array along with the ending point for the for loop.

Yup, and since the only thing in that line thats changing is i.. so this will prompt you to go look where i was last modified. Oh, look at that.. after each line you increment i by one so your redim statement works on the next iteration. Also note that you probably didn't mean to use i as the index, perhaps your loop variable j was the right choice? Either case you need to fix that i value.
ZeroPaladn




PostPosted: Fri Jan 12, 2007 8:36 am   Post subject: Re: "Array Subscript out of Range"... I'm stumped.

i was last modified during the Form_Load, when I have to ReDim the array to fit more enteries. The thing is, i gets to 5 from the file that I provided. Yet when I trace executed, i = 5. Yet it is 5 when the array get our of range. Thus is why I'm stumped. The only time i is modified is when I have to ReDim the array so it can fit the new entry, but I never get to that part with the error that I get. I'll post when I get it.

EDIT

Found the stupidest error ever...
VisualBASIC:
For j = 0 To i Step 1
    If txtAddArtist.Text = libraryArray(j, 1) And txtAddAlbum.Text = libraryArray(j, 0) Then
        addFlag = False
    ElseIf txtAddNumber.Text = libraryArray(j, 2) Then
        addFlag = False
    Else
        addFlag = True
    End If
Next j
' Note how the first if statement had libraryArray(i, 1), but I should have used libraryArray(j, 1)


One down, but the error still remains. now j does out of range at the same area. Again I'm stumped. At least I got rid of the dumb error Razz

EDIT AGAIN

Joy, found the error!

VisualBASIC:
For j = 0 To i - 1 Step 1
    If txtAddArtist.Text = libraryArray(j, 1) And txtAddAlbum.Text = libraryArray(j, 0) Then
        addFlag = False
    ElseIf txtAddNumber.Text = libraryArray(j, 2) Then
        addFlag = False
    Else
        addFlag = True
    End If
Next j
' Notice the beginning of the for loop! it used to be For j = 0 to i Step 1, but libraryArray only goes to 4.
' Ergo, i - 1 was the awnser.


But it is not the end...

VisualBASIC:
Write #1, txtAddAlbum.Text, txtAddArtist.Text, txtAddNumber.Text
' Bad file mode. Apparently I can't write to the file. Damnit!


Grrr... it just never ends!

...EDIT...

Can't write to a file when I'm opening it for input...

VisualBASIC:
Open "H:\ICS 3MI\Visual Basic\Final Project\libraryFile.txt" For Append As 1
' Note the Append used to be Input.


Done and Done. Thanks for the help guys!
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: