Results 1 to 8 of 8

Thread: increase array index by 1?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    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

  2. #2

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: increase array index by 1?

    Code:
    For n = n To n
    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.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    Re: increase array index by 1?

    ah ok i will give that a go. thanks

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    7

    Re: increase array index by 1?

    Quote Originally Posted by kleinma View Post
    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

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: increase array index by 1?

    since this is obviously homework, can you post the actual homework question?

  8. #8
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    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:
    1. Dim intArrayIndex as Integer = 0

    When you are inputing your data, you're going to input into the following.
    vb Code:
    1. StudentNames(intArrayIndex) = InputBox("enter your full name")

    To increment the array index (ie the integer value)
    vb Code:
    1. intArrayIndex += 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width