By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

How reinitialize an array with modular scope?

P: 1
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).
Mar 25 '18 #1
Share this Question
Share on Google+
1 Reply


IronRazer
P: 82
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.
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private MyArray() As Integer = {} 'declared as an empty array
  3.  
  4.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5.         MyArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 'example of filling the Integer Array with some integer values
  6.     End Sub
  7.  
  8.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  9.         MyArray = {} 'example of resetting/clearing the Integer Array by assigning an empty array '{}' to it.
  10.     End Sub
  11.  
  12.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  13.         MessageBox.Show("MyArray contains " & MyArray.Length.ToString & " elements.")
  14.     End Sub
  15. End Class
  16.  
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.
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private MyArray(9) As Integer 'declared as a fixed length array which has 10 elements (0 to 9)
  3.  
  4.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5.         For i As Integer = 0 To MyArray.Length - 1 'fills the 10 elements of the array with Integer values
  6.             MyArray(i) = i * 2
  7.         Next
  8.     End Sub
  9.  
  10.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  11.         ReDim MyArray(9) 'ReDim the array. This will set the 10 elements back to their default values of 0.
  12.     End Sub
  13.  
  14.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  15.         MessageBox.Show("MyArray contains: " & String.Join(", ", MyArray))
  16.     End Sub
  17. End Class
  18.  
Mar 25 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.