Question Help with Swapping elements of user submitted array

o9z

Member
Joined
Aug 8, 2007
Messages
6
Programming Experience
Beginner
Yes, this is for school. No, it is not a graded assignment. This is part of the lab for end of the week, and I am not understanding how to accomplish this.

Here are the tasks:

· Create two arrays, ask the user to populate them and display both arrays

· Swap the contents of the arrays using loops and then display the content.

What is the best method to have the end user populate an array? I am doing this as a windows form. Have multiple text boxes that they have to fill out? Set the elements of the array to the text box values? And how to swap the contents at run time is something I am not sure of. Isn't there a Array Swap function or something?

Thanks

Here is what I have so far:
Note: the bottom section was a test..and it isn't working as expected.

VB.NET:
Public Class Form1
    Dim numberarray(3) As Integer
    Dim numberarray2(3) As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        numberarray(0) = txtOneOne.Text
        numberarray(1) = txtOneTwo.Text
        numberarray(2) = txtOneThree.Text
        numberarray(3) = txtOneFour.Text
        numberarray2(0) = txtTwoOne.Text
        numberarray2(1) = txtTwoTwo.Text
        numberarray2(2) = txtTwoThree.Text
        numberarray2(3) = txtTwoFour.Text

        For i As Integer = 0 To numberarray.Count - 1
            MsgBox(numberarray(i).ToString)
        Next
        Dim tmp As String
        tmp = numberarray(0)
        numberarray(0) = numberarray(1)
        numberarray(1) = tmp
        tmp = Nothing
        MsgBox("After:")
        For i As Integer = 0 To numberarray.Count - 1
            MsgBox(numberarray(i).ToString)
        Next
    End Sub
End Class
 
The 'best' method of having an end user fill an array is going to be pretty much down to the types of users and what you're expecting them to enter - in other words, a hard one to answer.

One way you could do it is to have a single textbox for each array and ask the user to enter a comma delimited string, then use String.Split to create your array.

There doesn't appear to be anything wrong with your swapping code - what's not working as expected?
 
Back
Top