0% found this document useful (0 votes)
4 views

Array Example 1

Uploaded by

mwisilwandile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array Example 1

Uploaded by

mwisilwandile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Public Class Form1

Private Sub BtnDisplayElements_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles BtnDisplayElements.Click
LstFriends.Items.Clear()
'Declare and populate an array
Dim strMyFriends(4) As String
strMyFriends(0) = "Elaine"
strMyFriends(1) = "Richard"
strMyFriends(2) = "Debra"
strMyFriends(3) = "Wendy"
strMyFriends(4) = "Ben"
'Displaying elements from the array
For Each strName As String In strMyFriends
LstFriends.Items.Add(strName)
Next
End Sub

Private Sub BtnSortAscending_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles BtnSortAscending.Click
LstFriends.Items.Clear()
'Declare and populate an array
Dim strMyFriends(4) As String
strMyFriends(0) = "Elaine"
strMyFriends(1) = "Richard"
strMyFriends(2) = "Debra"
strMyFriends(3) = "Wendy"
strMyFriends(4) = "Ben"

Array.Sort(strMyFriends)
'Displaying elements in array
For Each strName As String In strMyFriends
LstFriends.Items.Add(strName)
Next

End Sub

Private Sub BtnSortDescending_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles BtnSortDescending.Click
LstFriends.Items.Clear()
'Declare and populate an array
Dim strMyFriends(4) As String
strMyFriends(0) = "Elaine"
strMyFriends(1) = "Richard"
strMyFriends(2) = "Debra"
strMyFriends(3) = "Wendy"
strMyFriends(4) = "Ben"
'Sorting the array first and then reverse the order for descending
'Remove the sort and test, Notice the element positions will be reversed
Array.Sort(strMyFriends)
Array.Reverse(strMyFriends)

'Displaying elements in array


For Each strName As String In strMyFriends
LstFriends.Items.Add(strName)
Next
End Sub

Private Sub btnReverseArray_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnReverseArray.Click
LstFriends.Items.Clear()
Dim strMyFriends(4) As String
strMyFriends(0) = "Elaine"
strMyFriends(1) = "Richard"
strMyFriends(2) = "Debra"
strMyFriends(3) = "Wendy"
strMyFriends(4) = "Ben"

'Notice the element positions will be reversed

Array.Reverse(strMyFriends)

'Displaying elements in array


For Each strName As String In strMyFriends
LstFriends.Items.Add(strName)
Next
End Sub

Private Sub BtnNumbers_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles BtnNumbers.Click
Dim numbers(4) As Integer

numbers(0) = 0
numbers(1) = 1
numbers(2) = 20
numbers(3) = 30
numbers(4) = 15
Array.Reverse(numbers)
For Each number As Integer In numbers
LstFriends.Items.Add(number)
Next
'Notice the reverse will display elements in reverse order
End Sub

Private Sub btnArraySize_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnArraySize.Click
Dim Colours(3) As String
'Declare a variable to hold the length of the array
Dim intArraySize As Integer

Colours(0) = "Yellow"
Colours(1) = "Red"
Colours(2) = "Blue"
Colours(3) = "Green"

'Get the array size


intArraySize = Colours.Length

'Display the array size


MessageBox.Show("The array size is: " & intArraySize)
End Sub

Private Sub btnSearching_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnSearching.Click
Dim strColors = {"Red", "Green", "Blue", "Ren"}

'Finding a string
'Startswith makes use of a function that will check for a string
'The String will be case sensitive

Dim value1 As String = Array.Find(strColors, Function(x)


(x.StartsWith("Ren")))

MessageBox.Show(value1)

End Sub
End class

You might also like