473,466 Members | 4,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How reinitialize an array with modular scope?

1 New Member
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
1 1979
IronRazer
83 New Member
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

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

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
8
by: Jim | last post by:
In a C# project I'm working on for an iterative design application, I need to dispose of a large arrray of a struct object and reinitialize the array between iterations. That is, the user starts...
1
by: Seigfried | last post by:
An array can be initialized with code like this: Dim myArray() as String = {"1", "2", "3"} What I'd like to do is reinitialize the same array. Something like the statement: myArrray = {"4",...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
2
by: Tyno Gendo | last post by:
I'm writing a test "modular site". So far I have created an App class, a Module Manager class and a couple of test modules. The Manager looks in a directory called 'modules' and then for every...
4
by: souravmallik | last post by:
I have a structure declared in a header file, and its initialized in the header file too. struct rectype{ char out_storenum; unsigned int out_usrcode; unsigned int...
16
by: Nkhosinathie | last post by:
hi, i'm writing a program that is using 4 arrays of pointers to char,called article,noun,verb and preposition.the program should create a sentence by selecting a word at random from each array in the...
1
by: drewschott | last post by:
I'm having a problem receiving the full amount of information i need on a grocery order from that i'm accessing inputted values using form to mail in php. a snippet of my form looks like this:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.