Array is a data type that is often used in the program, an array in Visual Basic .NET, although the syntax structure has retained many similarities between Visual Basic, but there is a lot of differences with Visual Basic. The biggest, and the difference between the Visual Basic programmers is the second point of the following: 1. A array in Visual Basic .NET, the starting point of the index value in the array element is "0", not The starting point of the index value in Visual Basic is generally "1". This definition method is very similar to the twin sister Visual C # of Visual Basic .NET. 2. Since there is a unified starting point in the Visual Basic .NET in the Visual Basic., An array is defined in the Visual Basic .NET, there is no longer need keyword "to" to set the range of arrays. This declares a array in Visual Basic .NET will be a little simpler than Visual Basic. About array definitions will be described in detail later. One. This section main content: In the next content, the following knowledge points will be described in conjunction with specific examples: 1. A group, including one-dimensional array, multi-dimensional array, and a static array and a dynamic array, and initialization. And access the elements in the array. 2. Reproduction the array, the use of the REDIM statement, and the role of its preserve keyword. 3. Use the for Each .... Next statement to traverse each element in the array, the for Each .... NEXT statement is a statement that is the most commonly used operation data, which can easily access each element in the array. 4. The declaration parameter is the process or function of the array. Since the array is a reference type, the process or process of the processing method and the parameter is a bit different from the function or process of the declaration parameter as an array. 5. Briefly introduce the ARRAY class and its main common, and the specific method of an array operable by the Array class.
two. Declaration, initialization array and access arrays: In Visual Basic .NET stated that an array is still a DIM statement, its declaration array and Visual Basic's main differences have been briefly said above. Below the Visual Basic .NET states a string array of length "3", and the code is initialized:
DIM arrstring (2) as string = {"Monday", "Tuesday", "Wednesday"}
The following code is an array of 2 × 2 two-dimensional string and initializes:
DIM Arrdate (1, 1) as string = {{"Monday", "No. 18"}, {"Tuesday", "No. 19"}}
The difference between the static array and the dynamic array is that the length of the static array is fixed, while the length of the dynamic array is not fixed. The above declaration is a static array, and the role of the following two codes is to declare a one-dimensional array and two-dimensional array, and initialize them:
Dim Arrstring () AS String = {"Tuesday", "Tuesday", "Wednesday"} 'declares a dynamic one-dimensional array, and initializes DIM Arrdate (,) as string = {{"Monday," 18 "}, {" Tuesday "," No. 19 "}} 'declares a dynamic two-dimensional array, and initialize
An array of other data types and more dimensions, its declaration and initialization method and the above basis, only according to the different number of dimensions and data types according to the array, can be modified. When the array declares and initializes, you can access the corresponding index value in the array. The following segment is an access to one-dimensional array and a two-dimensional array of two-dimensional arrays, respectively:
DIM STEMP1 AS STRING = arrstring (1) 'Access the second element in the Arrstring array DIM STEMS STRING = Arrdate (1, 1)' Access the second line, second column elements in the Arrdate array
three. Re-admission array:
The Visual Basic .NET reappoints the array and the Visual Basic is basically similar, and still use the redim statement. Use the Redim statement in Visual Basic. Note to pay attention to the following three points:
1. The Redim statement can only appear in the process level. This means that you cannot use the RedIm statement in the class or module level code area to re-declare the array.
2. RedIM statement is just the size of one or more dimensions of the array that have been formally declared, but cannot change the dimension of the array.
3. RedIM statement cannot change the data type of the elements in the array, and the difference between the DIM statement declaration array is that the array of re-declaration cannot be initialized in the Redim statement.
The most common keyword is "preserve" when using Redim to renew array. The role of "preserve" is to indicate whether the elements in the original number group are copied in the reappointment array when reapplying the array. Please compare the second paragraph of the following:
Code one:
Dim Arrstring (2) AS String = {"Monday", "Tuesday", "Wednesday"} Redim Preserve Arrstring (4) 'Re-decoding Arrstring array, the length of the array is changed 5, and the original number group is replicated in the new array Element Arrstring (3) = "Thursday" arrstring (4) = "Friday"
Code 2:
DIM Arrstring (2) AS String = {"Monday", "Tuesday", "Wednesday"} Redim Arrstring (4) 'Reproducing Arrstring array, the length of the array is changed to 5, do not copy the original number group in the new array Element arrstring (0) = "Monday" arrstring (1) = "Tuesday" arrstring (2) = "Wednesday" arrstring (3) = "Thursday" arrstring (4) = "Friday"
By comparing the above segment, it can be seen that in the first segment code, since the PRESERVE keyword is used in RedIM, the element of the original number group is copied in the new array, which only needs to be Two elements are initialized; while the second segment is not used in the new array in the new array, all elements of the array are initialized. By comparing, you have a deeper understanding of the use of redefined arrays and "preserve" keywords.
four. The use of the for Each .... NEXT statement in the array: for Each .... Next statement seems to be done for the two data types, one is a collection, and the other is an array. Through the for Each .... NEXT statement, you can access any of the elements in the array. Please analyze the following code carefully: DIM arrstring (2) as string = {"Monday", "Tuesday", "Wednesday"} Dim Temp as stringfor Each Temp in Arrstringif Temp = "Wednes" THENMSGBOX ("Find in arrays "Wednesday" string ") exit for 'Exit for Each .... NEXT statement end ifnext
The function of the above code is to access each element in the Arrstring array through the for Each .... If you find that an element is a string "Wednesday", the prompt box will pop up, and exit the loop.
Also use for ... next statement, you can implement the same functions above, just feel a bit unprofessional, and it is a bit cumbersome. The following code implements the above for ... NEXT statement to implement the above for Each ... The same function code:
DIM I as integerfor i = 0 to mystring.length - 1IF arrstring (i) = "Wednesday" THENMSGBOX ("Find" in array "" Wednesday "string" EXIT Forend ifnext
Fives. The process or function of the declaration parameter is array:
Since the array is a reference type, if it is necessary to pay more attention to the parameter process and function in array, the following code is a typical code for the process of Visual Basic .NET, which is visible to the array type before calling the array type. A keyword "byval", "Byval" keyword is the value of the array that cannot be changed during the following process:
Sub DisplayArray (Byval MyString () AS STRING) End Sub
six. Brief introduction to the System.Array class and its members:
The Array class is a base class that is the base class of all arrays in the public language runtime. It provides methods for creating, operation, searching, and sort arrays. Any element in array is a value in Array. The length of Array is the total number of elements that it can contain. Array's rank is the dimension in Array. The lower limit of the Array in the Array is the starting index of this dimension in Array, and the dimensions of multidimensional Array can have different boundaries. Table 01 and Table 02 are the common attributes and common methods of the Array class, respectively, and their description:
Property Description Is ISFixedSize Gets the value indicating whether Array has a fixed size. IsReadOnly gets the value indicating whether Array is a read-only. Length Gets the total number of elements in all dimensions of Array. Rank gets the rank of Array (that is, the dimension). Table 01: Common properties of the System.Array class and their description
method
Description
BinarySearch searches for values in one-dimensional sorting array using binary search algorithms. CLEAR sets a series of elements in Array to zero, false or empty references (ie: nothing), the set value when the specific operation depends on the element type. Copy copies a part of an Array to another, and performs forced type conversion and packing as needed. CopyTo copies all elements of the current one-dimensional array to the specified one-dimensional Array (starting from the specified target array index). GetLength Gets the number of elements in the specified dimension of Array. GetLowerBound gets the lower limit of the specified dimension in Array. GetUpperBound gets the upper limit of the specified dimension of Array. GetValue Gets the value of the specified element in the current Array. LastIndexo returns an index of the last match of a value in one-dimensional Array or some of the array. Reverse reverses the order of elements in one-dimensional Array or part of Array. SetValue Sets the specified element in the current Array to the specified value. Sort sorts elements in a one-dimensional Array object. Table 02: Common methods for the System.Array class and its description. A specific example of an array:
The functionality of the examples described below contains the declaration, initialization, reinstation, reinitialization, array of elements in arrays, traversal, lookup, and modification of the array as parameters, etc. in array. Here is the specific steps implemented in this example:
1. Start Visual Studio .NET.
2. Select the menu [File] | [New] | [Project] to pop up the [New Project] dialog box.
3. Set the [Project Type] to [Visual Basic Project].
4. Set [Template] to [Windows Application].
5. Enter [Array] in the [Name] text box.
6. Enter [E: /VS.NET Project] in the [Position], then click the [OK] button, which generates a name "array" in the "E: /VS.NET project" directory. The folder and the project file named [array] is created inside.
7. Switch the current window of Visual Studio .Net to the [Form1.vb (Design)] window, and drag into the following components from the [Windows Forms] tab in [Toolbox] to the Form1 Form, and Perform the corresponding operation:
Four Button components, which are used to create a "Create One-Dimer), create a" Create 2D array ", and implement" sorting one-dimensional array sorting "and implementing" redefining the one-dimensional array ". After the four Button components are dragged into the Form1 design form, double-click the four components, so that the system will generate the processing code corresponding to the Click time of the four components in the Form1.vb file.
A ListBox component is used to display the content of the created one-dimensional array.
8. Follow the values in Table 03 to set the main properties of the setting components:
Component Type Component Name Properties Settings Results
FORM
Form1 Text Array Form1 MaximizeBox Falseform1MinimizeBox Falseform1 FormBorderstyle fixedSingLELELE
Listbox
Listbox1 Itemheight 12ListBox1 Location Point (28, 169) Listbox1 Size Size (246, 136)
Button
Button1 Text create a one-dimensional array Button1 FlatStyle FlatButton2 Text to create a two-dimensional array Button2 FlatStyle FlatButton3 Text of one-dimensional array sorting Button3 FlatStyle FlatButton4 Text redefine the one-dimensional array Button4 FlatStyle Flat
Table 01 Table 03: [Array] The main attribute setting value table of each component in the project form
The various components in the form are adjusted according to the position, size and arrangement of each component in Figure 01: Figure 01: [Array] Item Design Interface
9. Switch the current window of Visual Studio .Net to [Form1.vb], enter the editing interface of the Form1.vb file. The following code is then replaced with the processing code corresponding to the Click event of the Button1 component in Form1.vb. The following code is created by creating a input dialog box:
Private sub button1_click (byval e as system.EventArgs) handles button1.clickdim i as integerfor i = 0 to 3arrstring (i) = inputbox ("Please enter a string in the text box", "Please Enter a string to initialize the created one-dimensional array! "," Item "& i) NextButbutton3.enabled = trueButton4.enabled = trueDisplayArray (arrstring) end sub
10. Replace the processing code corresponding to the Click event of the Button2 component in Form1.vb with the following code, the function of the following code is to create a two-dimensional array and initialize:
Private sub button2_click (byval e as system.EventArgs) Handles button2.clickdim arrdate (,) as string = {{"Monday", "No. 18"}, {"Tuesday", "No. 19 "}} 'Creates a two-dimensional array and initializes MSGBox (Arrdate (0, 1))' to access the element END SUB in this 2D array.
11. Replace the processing code corresponding to the Click event of the Button3 component in Form1.vb with the following code, the following code is first defined a process name "DisplayArray", this process can be used for data in a given one-dimensional array Sort and display it through ListBox. Secondly, define the Click event of Button3, in which the DISPLAYARRAY process is called, the data in one-dimensional array is sorted by:
'Define a process, the function of the process is to display each element Sub DisplayArray in a given string array through the ListBox component (Byval MyString () AS String, ListBox1.Items.clear () DIM TEMP AS STRINGFOR EACH TEMP IN MyStringListBox1.Items. Add (Temp) Nextend Subprivate Sub Button3_Click (Byval e as system.EventArgs) Handles Button3.ClickArray.Sort (Arrstring) DisplayArray (Arrstring) End Sub
12. Replace the processing code corresponding to the Click event of the Button4 component in Form1.vb with the following code, the function of the following code is to redefine the one-dimensional array:
Private Sub Button4_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.ClickReDim Preserve arrString (5) 'Please note that the role of the Preserve keyword Dim i As IntegerFor i = 4 To 5arrString (i) = InputBox ( "Please enter the string in the text box", "Please enter the string to initialize the recreated one-dimensional array!", "Item" & i) NextdisPlayArray (arrstring) End Sub13. Save all the modifications above, Array] All work of the project is completed. At this point, click Shortcut F5 Run, Figure 02, Figure 03 is the running interface after the program compile:
Figure 02: Enter a string to initialize the interface of the one-dimensional array
Figure 03: Interface after sorting each element in a one-dimensional array
Eight. to sum up:
This article first introduces the array in Visual Basic .NET and the difference in the original Visual Basic, in the Visual Basic .NET in the Visual Basic .NET is more like Visual C #. Secondly, the applying method for arrays, including one-dimensional array, multi-dimensional array, also including static arrays and dynamic arrays, initialization arrays, and elements in an access array. Then introduce the reappointment array method, the use of the REDIM statement, and the role of its preserve keyword. And use the for Each .... NEXT statement to traverse each element in the array, the for Each .... Next statement is a statement that is the most commonly used operation data, it can easily access each element in the array. It is convenient to use for ... next statement. Last introduction to the process and function of the argument as parameters, and the Array class and the main method of use. The reason why it is proposed to introduce the call to the argument and function of the argument, because the array is a reference type, so when the declaration parameter is a function or process, the process method and the parameter are a bit different from the value of the value type. . Array is a class that provides an array of arrays in the .NET Framework SDK. This paper mainly introduces the specific method of using the Array class operand. The above is the main content of this article, I believe that readers who carefully read and carry out the corresponding exercises must have been mastered.