How reinitialize an array with modular scope?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • forestjoeri
    New Member
    • Mar 2018
    • 1

    How reinitialize an array with modular scope?

    I'm a beginner in coding and my introduction to software development class is teaching Visual Basic 2015.

    Assignment tells me that I need to code a click handler so that when the button is clicked, it removes all values from the array, and tells me that I should do this by creating a new array and assigning it to the array variable.

    I'm pulling my hair out trying to understand how I'm supposed to code a new array with modular scope in the click handler's private sub.

    To do what the assignment asks of me, I think it's much easier to code the array as a list and simply code listname.Clear( ) but I don't think thats what my teacher/the assignment wants me to do.

    Can I receive any help?

    for example, i've tried creating two arrays (array1 and array2) and putting array1 = array2 in the clickhandler to assign array2's null values to array1 and it only works once.

    i've tried using Redim but that doesn't seem to be what the assignment is asking me to do and even if i clear the list, if i enter more values than the upperbound (19) (it has to be 19 for this assignment) i get an exception. (example: if i input 15 values then hit the clear button (redim), and enter 6 values, i get an exception).
  • IronRazer
    New Member
    • Jan 2013
    • 83

    #2
    This would depend on how the array is declared and used. For example, if the array is declared as an empty array then you can just clear its contents by assigning an empty array to it as shown in this first example below...

    If you run this example and press Button3 first, you will see that the array is empty, contains 0 elements. Then press Button1 and then Button3 again, you now see it contains 10 elements. Last, press Button2 and then Button3 again, you will see that the array is empty again.
    Code:
    Public Class Form1
        Private MyArray() As Integer = {} 'declared as an empty array
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MyArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 'example of filling the Integer Array with some integer values
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            MyArray = {} 'example of resetting/clearing the Integer Array by assigning an empty array '{}' to it.
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            MessageBox.Show("MyArray contains " & MyArray.Length.ToString & " elements.")
        End Sub
    End Class
    On the other hand, if the array is declared as a fixed length array like in the below example, you could use the ReDim keyword to reset it's elements back to their default values (0 in this case).

    You can test this by pressing the buttons in the same order as I explained for the first example.
    Code:
    Public Class Form1
        Private MyArray(9) As Integer 'declared as a fixed length array which has 10 elements (0 to 9)
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i As Integer = 0 To MyArray.Length - 1 'fills the 10 elements of the array with Integer values
                MyArray(i) = i * 2
            Next
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ReDim MyArray(9) 'ReDim the array. This will set the 10 elements back to their default values of 0.
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            MessageBox.Show("MyArray contains: " & String.Join(", ", MyArray))
        End Sub
    End Class

    Comment

    Working...