increase array index by 1?
hi I’m having trouble inputting data in to an array. The first input is fine but after that it just overwrites the first input. i tried to write it so that it would increase the array index by one each time but it hasn’t worked. Any suggestions?
Code:
Public Class Form1
Private InputStudentName As String
Private StudentNames(9) As String
Private Questions(29, 2) As String
Private Sub StudentLogOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StudentLogOn.Click
StudentNames(0) = ""
StudentNames(1) = ""
StudentNames(2) = ""
StudentNames(3) = ""
StudentNames(4) = ""
StudentNames(5) = ""
StudentNames(6) = ""
StudentNames(7) = ""
StudentNames(8) = ""
StudentNames(9) = ""
Dim n As Integer
For n = n To n
StudentNames(n) = InputBox("enter your full name")
n = n + 1
Next
Re: increase array index by 1?
Code:
Redim Preserve StudentNames(StudentNames.Length)
Re: increase array index by 1?
Huh???
Say n = 1, your for loop is from 1 to 1. It should only ever execute once. Then inside your loop you manually increment your loop counter.
You should also never use a variable for your loop counter that is also one of the bounds of the loop, and you shouldn't manually adjust the loop counter.
Re: increase array index by 1?
you can't do a loop as
Code:
for n = n to n
next
it will only run once. Not to mention a for loop increments n automatically, you don't need to add 1 to it inside the loop. You should be doing
Code:
for n = 0 to (StudentNames.Length - 1)
'code here
next
to loop all elements of the array.
Re: increase array index by 1?
ah ok i will give that a go. thanks
Re: increase array index by 1?
Quote:
Originally Posted by
kleinma
you can't do a loop as
Code:
for n = n to n
next
it will only run once. Not to mention a for loop increments n automatically, you don't need to add 1 to it inside the loop. You should be doing
Code:
for n = 0 to (StudentNames.Length - 1)
'code here
next
to loop all elements of the array.
ok, i see. that loops the input box till the end of the array. that not really what i need the program to do. i need it so that a student can log in to the program, with their name stored in to the array, then they can run the rest of the progam before another student enters their name
Re: increase array index by 1?
since this is obviously homework, can you post the actual homework question?
Re: increase array index by 1?
When you are inputing your data into the variable, that is where you want to increment the array index.
It looks like you're filling each array index with "" and then cycling through each one, then inputting the value's in via input box.
Declare a variable for your index array.
vb Code:
Dim intArrayIndex as Integer = 0
When you are inputing your data, you're going to input into the following.
vb Code:
StudentNames(intArrayIndex) = InputBox("enter your full name")
To increment the array index (ie the integer value)