Creating arrays and add elements to it
In Visual Basic (VB), you can create arrays of different data types (e.g., integers, strings, etc.)
and access their elements using index values. Here's a code example demonstrating how to
create arrays and access their members:
```vb
Module Module1
Sub Main()
' Creating an array of integers
Dim intArray(4) As Integer
intArray(0) = 10
intArray(1) = 20
intArray(2) = 30
intArray(3) = 40
intArray(4) = 50
' Accessing the elements of the integer array
Console.WriteLine("Integer Array Elements:")
For i As Integer = 0 To 4
Console.WriteLine("Element " & i & ": " & intArray(i))
Next
' Creating an array of strings
Dim strArray() As String = {"Apple", "Banana", "Orange", "Mango"}
' Accessing the elements of the string array
Console.WriteLine(vbCrLf & "String Array Elements:")
For i As Integer = 0 To strArray.Length - 1
Console.WriteLine("Element " & i & ": " & strArray(i))
Next
Console.ReadLine()
End Sub
End Module
```
In the code above:
- An array of integers named `intArray` is created with 5 elements, and each element is
assigned a value.
- The elements of the integer array are accessed and displayed using a `For` loop.
- An array of strings `strArray` is created and initialized with string values.
- The elements of the string array are accessed and displayed using a `For` loop, where
`strArray.Length` is used to get the length of the array.
You can run this code in an IDE or an online VB compiler to see how elements of arrays are
accessed and displayed. This example illustrates the creation, initialization, and accessing of
elements in integer and string arrays in Visual Basic.