Breaking News
You are here: Home / Notes / SEM-4 / .NET PROGRAMMING / Arrays in vb.net

Arrays in vb.net

An arrays stores a fixed-size sequential collection of elements of the same type. it is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

arrays

 

Example

There are many syntax forms we use to declare one-dimensional arrays in the VB.NET language. To start, this example creates an array by specifying in the maximum index in the first statement.

Dim intData(30)	  ' an array of 31 elements
Dim strData(20) As String	' an array of 21 strings
Dim twoDarray(10, 20) As Integer	'a two dimensional array of integers
Dim ranges(10, 100)	 'a two dimensional array

 

Integer array

Often you can initialize an array in a simpler way than in the previous example. Here, we use the curly-bracket { } syntax to create an array of three elements. Note that you do not need to declare a maximum index on the array.

Module Module1
    Sub Main()
	' Create array of three integers.
	Dim array() As Integer = {10, 30, 50}

	For Each element As Integer In array
	    Console.WriteLine(element)
	Next
    End Sub
End Module

 

String array

Arrays are useful for reference types like Strings. We create a four-element String array and place four String references inside of it. One interesting thing is that the strings are actually not stored in the array.

Module Module1
    Sub Main()
	' Create array of maximum index 3.
	Dim array(3) As String
	array(0) = "test"
	array(1) = "abc"
	array(2) = "xyz"

	' Display.
	For Each element As String In array
	    Console.WriteLine(element)
	Next
    End Sub
End Module

 

Return array

How can you have a function in your VB.NET program return an array? This program demonstrates the correct syntax. The Example function creates a two-element array and then returns it.

Module Module1
    Sub Main()
	Console.WriteLine(String.Join(",", Example()))
    End Sub

    ''' <summary>
    ''' Return array.
    ''' </summary>
    Function Example() As String()
	Dim array(1) As String
	array(0) = "abx"
	array(1) = "xyz"
	Return array
    End Function
End Module

 

Methods of the Array Class

The following table provides some of the most commonly used methods of the Array class:

S.N Method Name & Description
1 Public Shared Sub Clear ( array As Array, index As Integer, length As Integer ) 
Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
2 Public Shared Sub Copy ( sourceArray As Array, destinationArray As Array, length As Integer ) 
Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.
3 Public Sub CopyTo ( array As Array, index As Integer ) 
Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.
4 Public Function GetLength ( dimension As Integer ) As Integer 
Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
5 Public Function GetLongLength ( dimension As Integer ) As Long 
Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
6 Public Function GetLowerBound ( dimension As Integer ) As Integer 
Gets the lower bound of the specified dimension in the Array.
7 Public Function GetType As Type
Gets the Type of the current instance (Inherited from Object).
8 Public Function GetUpperBound ( dimension As Integer ) As Integer 
Gets the upper bound of the specified dimension in the Array.
9 Public Function GetValue ( index As Integer ) As Object 
Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
10 Public Shared Function IndexOf ( array As Array, value As Object ) As Integer 
Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.
11 Public Shared Sub Reverse ( array As Array ) 
Reverses the sequence of the elements in the entire one-dimensional Array.
12 Public Sub SetValue ( value As Object, index As Integer ) 
Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
13 Public Shared Sub Sort ( array As Array ) 
Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
14 Public Overridable Function ToString As String
Returns a string that represents the current object (Inherited from Object).

 

Download Paper Solutions from Below Link

About admin

Scroll To Top