Results 1 to 4 of 4

Thread: Parallel Arrays: For loop error

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    45

    Parallel Arrays: For loop error

    Hi,

    This application is an assignment that requires for me to list the names of kids into 2 list boxes from the (strChild)array. The second array(rdoList) consists of if the child is acceptable to participate in the sled race. So 2 arrays, one has the child's full name, second has the yes or no value of participation. The error I am getting is when I'm trying to display the elements in the (strChild) array that are parallel to the "Yes" value in the (rdoList) array into the list box and I get a "Conversion from string "Y" to type 'Integer' is not valid"

    I highlighted the line RED which gives me the error.

    Code:
    Public Class Main
        'Declaring arrays
        Dim strChild(-1) As String ' holds the variables of the child's full name
        Dim rdoList(-1) As String  ' holds the Yes or No radio button selection
    
        Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
            Dim count As Integer = 0
    
            'Validation
            If txtFName.Text = "" Or txtLName.Text = "" Then
                MessageBox.Show("Please enter a First and Last Name")
            Else
                'Stores the name into the array
                ReDim Preserve strChild(strChild.Length)
                strChild(strChild.Length - 1) = txtLName.Text & "," & txtFName.Text
    
                'Stores the value of the radio button into the array
                If rdoYes.Checked Then
                    ReDim Preserve rdoList(rdoList.Length)
                    rdoList(rdoList.Length - 1) = "Y"
                Else
                    ReDim Preserve rdoList(rdoList.Length)
                    rdoList(rdoList.Length - 1) = "N"
                End If
    
                For i As Integer = 0 To rdoList(rdoList.Length - 1)
                    If rdoList(i) = "Y" Then
                        Do Until 0 = strChild(strChild.Length - 1)
                            lstAllowed.Items.Add(strChild(i))
                        Loop
                    End If
                Next
            End If
    
            'Clears out text boxes
            txtFName.Clear()
            txtLName.Clear()
            txtFName.Focus()
        End Sub
    
        Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
            'Closes the form
            Me.Close()
        End Sub
    End Class
    Last edited by flintvic; Jan 15th, 2013 at 08:19 AM.

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Parallel Arrays: For loop error

    Code:
    For i As Integer = 0 To rdoList(rdoList.Length - 1)
    Break it down, I'm using the #10 as a sample value:

    Code:
    For
    i As Integer = 0 
    To
    rdoList
    (
    rdoList.Length - 1
    )
    And evaluate

    Code:
    For
    0
    To
    rdoList
    (
     10
    )
    Code:
    For 0 To rdoList(10)
    Code:
    For 0 To "Y"
    .. see the problem now?
    Last edited by Grimfort; Jan 15th, 2013 at 08:40 AM.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    45

    Re: Parallel Arrays: For loop error

    I get it because 0 (integer) cannot iterate to Y(string) in the loop but how do I get around it. I tried using a string. Should I use Cint?

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Parallel Arrays: For loop error

    You are trying to loop through an array, by starting at 0 and going up to the number of items. You are getting the number of items, and then trying to use that to get a specific item (the last one) from the array itself, you just need the number of items. I can easily give you the code, but I have given you everything you need to solve it yourself .

    Here are some examples:

    For intProfLoop As Integer = 0 To UBound(intProfileLinks) - 1

    For intIndex As Integer = 0 To intBaseFolderRefs.Length - 1

Tags for this Thread

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