- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Visual Basic arrays
In this part of the Visual Basic programming tutorial, we will cover arrays. We will initiate arrays and read data from them.
Arrays are collections of data. A variable can hold only one item at a time. Arrays can hold multiple items. These items are called elements of the array. Arrays store data of the same data type. Each element can be referred to by an index. Arrays are zero based. The index of the first element is zero.
Collections serve the similar purpose. They are are more powerful than arrays. They will be described later.
Arrays are used to store data of our applications. We declare arrays to be of a certain data type. We specify their length. And we initialize arrays with data. We have several methods for working with array. We can modify the elements, sort them, copy them or search them.
Initializing arrays
There are several ways, how we can initialize an array in Visual Basic.
Option Strict On Module Example Sub Main() Dim array(5) As Integer array(0) = 3 array(1) = 2 array(2) = 1 array(3) = 5 array(4) = 6 For i As Integer = 0 To array.Length-1 Console.WriteLine(array(i)) Next End Sub End Module
We declare and initialize a numerical array. The contents of the array are printed to the console.
Dim array(5) As Integer
Here we declare an array which contains five elements. All elements are integers.
array(0) = 3 array(1) = 2 ...
We initialize the array with some data. This is assignment initialization. The indexes are in the parentheses. Number 3 is going to be the first element of the array, 2 the second.
For i As Integer = 0 To array.Length-1 Console.WriteLine(array(i)) Next
We go through the array and print its elements. An array has a Length
property, which gives the number of elements in the array. Since arrays are zero based, the indexes are 0..length-1.
We can declare and initialize an array in one statement.
Option Strict On Module Example Sub Main() Dim array() As Integer = { _ 2, 4, 5, 6, 7, 3, 2 } For Each i As Integer In array Console.WriteLine(i) Next End Sub End Module
This is a modified version of the previous program.
Dim array() As Integer = { _ 2, 4, 5, 6, 7, 3, 2 }
An array is declared and initialized in one step. The elements are specified in the curly brackets. We did not specify the length of the array. The compiler will do it for us.
For Each i As Integer In array Console.WriteLine(i) Next
We use the For Each
keyword to traverse the array and print its contents.
The bounds of an array
Visual Basic has two functions for getting the bounds of an array. The LBound()
function returns the lowest available subscript for the indicated dimension of an array. The UBound()
function returns the highest available subscript for the indicated dimension of an array. So far we have worked with one dimensional arrays.
Option Strict On Module Example Dim n1 As Integer Dim n2 As Integer Sub Main() Dim names() As String = { "Jane", "Lucy", _ "Timea", "Beky", "Lenka"} n1 = LBound(names) n2 = UBound(names) Console.WriteLine(n1) Console.WriteLine(n2) For i As Integer = n1 To n2 Console.WriteLine(names(i)) Next End Sub End Module
We have an array of names. We calculate and work with the lower and upper bound of that array.
n1 = LBound(names) n2 = UBound(names)
The n1
is the lowest index, the n2
the highest index in the names array.
For i As Integer = n1 To n2 Console.WriteLine(names(i)) Next
We examine the array using both the lower and upper bounds of the array.
$ ./bounds.exe 0 4 Jane Lucy Timea Beky Lenka
Output of the example.
Array dimensions
So far, we have worked with one dimensional arrays. The number of indexes needed to specify an element is called the dimension, or rank of the array.
We will work with two dimensional array.
Option Strict On Module Example Sub Main() Dim numbers(,) As Integer = { {2, 1}, {3, 5}, _ {4, 4}, {7, 2}, {0, 0} } For i As Integer = 0 To UBound(numbers, 1) For j As Integer = 0 To UBound(numbers, 2) Console.Write(CStr(numbers(i, j)) + " ") Next j Console.Write(vbNewLine) Next i End Sub End Module
If we need two indexes to access an element in an array, than we have a two dimensional array.
Dim numbers(,) As Integer = { {2, 1}, {3, 5}, _ {4, 4}, {7, 2}, {0, 0} }
We declare and initialize a two dimensional array in one statement. Note the comma inside the parentheses following the name of the array.
For i As Integer = 0 To UBound(numbers, 1) For j As Integer = 0 To UBound(numbers, 2) Console.Write(CStr(numbers(i, j)) + " ") Next j Console.Write(vbNewLine) Next i
We need two loops to get the data from the two dimensional array. The UBound()
function has an optional second parameter, rank. It is a dimension for which we retrieve the highest index. If the rank is omitted, the 1 dimension is assumed.
$ ./twodimensions.exe 2 1 3 5 4 4 7 2 0 0
Output of the code example.
Next we will work with a three dimensional array.
Option Strict On Module Example Sub Main() Dim i As Integer Dim j As Integer Dim k As Integer Dim nums(,,) As Integer = { _ {{12, 2, 8}}, _ {{14, 5, 2}}, _ {{3, 26, 9}}, _ {{4, 11, 2}} _ } For i = 0 To UBound(nums, 1) For j = 0 To UBound(nums, 2) For k = 0 To UBound(nums, 3) Console.Write(CStr(nums(i, j, k)) + " ") Next k Next j Console.Write(vbNewLine) Next i End Sub End Module
We have a numerical three dimensional array. Again, we initialize the array with numbers and print them to the terminal.
Dim nums(,,) As Integer = { _ {{12, 2, 8}}, _ {{14, 5, 2}}, _ {{3, 26, 9}}, _ {{4, 11, 2}} _ }
There is another comma between the parentheses on the left side and additional curly brackets on the right side.
For k = 0 To UBound(nums, 3) Console.Write(CStr(nums(i, j, k)) + " ") Next k
This loop goes through the third dimension. We use three indexes to retrieve the value from the array.
$ ./3darray.exe 12 2 8 14 5 2 3 26 9 4 11 2
We print the contents of the three dimensional array to the console.
There is a Rank()
function, which gives the number of dimensions of an array.
Option Strict On Module Example Sub Main() Dim array1() As Integer = {1, 2} Dim array2(,) As Integer = { { 1 }, { 2 } } Dim array3(, ,) As Integer = { { { 1, 2 }, { 2, 1 } } } Console.WriteLine(array1.Rank()) Console.WriteLine(array2.Rank()) Console.WriteLine(array3.Rank()) End Sub End Module
We have three arrays. We use the Rank()
function to get the number of dimensions for each of them.
Console.WriteLine(array1.Rank())
Here we get the rank for the first array.
Jagged arrays
Arrays that have elements of the same size are called rectangular arrays. In contrast, arrays which have elements of different size are called jagged arrays. Jagged arrays are declared and initialized differently.
Option Strict On Module Example Sub Main() Dim jagged As Integer()() = New Integer(4)() {} jagged(0) = New Integer() {1} jagged(1) = New Integer() {3, 4} jagged(2) = New Integer() {5, 6, 7} jagged(3) = New Integer() {5, 6} jagged(4) = New Integer() {9} For i As Integer = 0 To jagged.GetUpperBound(0) For j As Integer = 0 To jagged(i).GetUpperBound(0) Console.Write(jagged(i)(j) & " ") Next Console.Write(vbNewLine) Next End Sub End Module
This is an example of a jagged array.
Dim jagged As Integer()() = New Integer(4)() {}
This is a declaration of a jagged array. We have an array of arrays. More specifically, we have declared an array to have five arrays of Integer data type.
jagged(0) = New Integer() {1} jagged(1) = New Integer() {3, 4} ...
Each of the arrays must be individually initialized.
Console.Write(jagged(i)(j) & " ")
Unlike in rectangular arrays, each index is surrounded by parentheses.
Array methods
There are various methods for working with arrays. These methods can be used for retrieving, modifying data, sorting, copying, searching data. These methods that we use are static methods of the Array class or member methods of the array objects.
Option Strict On Module Example Sub Main() Dim names() As String = {"Jane", "Frank", "Alice", "Tom" } Array.Sort(names) For Each el As String In names Console.Write(el + " ") Next Console.Write(vbNewLine) Array.Reverse(names) For Each el As String In names Console.Write(el + " ") Next Console.Write(vbNewLine) End Sub End Module
In this example, we sort the data.
Dim names() As String = {"Jane", "Frank", "Alice", "Tom" }
We have an array of strings.
Array.Sort(names)
The Sort()
method sorts the data alphabetically.
Array.Reverse(names)
The Reverse()
method reverses the sequence of the elements in the entire one-dimensional array.
$ ./sorting.exe Alice Frank Jane Tom Tom Jane Frank Alice
We have ordered the names in ascending and descending order.
The following example uses SeValue()
, GetValue()
, IndexOf()
, Copy()
and Clear()
methods.
Option Strict On Module Example Dim names() As String = {"Jane", "Frank", "Alice", "Tom" } Dim girls(0 To 3) As String Sub Main() names.SetValue("Beky", 1) names.SetValue("Erzebeth", 3) Console.WriteLine(names.GetValue(1)) Console.WriteLine(names.GetValue(3)) Console.WriteLine(Array.IndexOf(names, "Erzebeth")) Array.Copy(names, girls, names.Length) For Each el As String In girls Console.Write(el + " ") Next Console.Write(vbNewLine) Array.Clear(names, 0, 2) For Each el As String In names Console.Write(el + " ") Next Console.Write(vbNewLine) End Sub End Module
This example introduces additional methods.
Dim girls(0 To 3) As String
Yet another way to declare an array.
names.SetValue("Beky", 1) names.SetValue("Erzebeth", 3)
The SetValue()
sets a value for a specific index in the array.
Console.WriteLine(names.GetValue(1)) Console.WriteLine(names.GetValue(3))
We retrieve the values from the array with the GetValue()
method.
Console.WriteLine(Array.IndexOf(names, "Erzebeth"))
The IndexOf()
method returns an index for the first occurrence of a specific value.
Array.Copy(names, girls, names.Length)
The Copy()
method copies values from the source array to the destination array. The first parameter is the source array, the second is the destination array. The third parameter is the length; it specifies the number of elements to copy.
Array.Clear(names, 0, 2)
The Clear()
method clears elements from the array. It takes three parameters, the array, the start index and the number of elements from the index to clear.
In this part of the Visual Basic tutorial, we worked with arrays. We described various types of arrays and methods to work with them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论