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).
1 1979
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. - 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. - 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
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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>
|
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...
|
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...
|
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...
|
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",...
|
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....
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |