Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: One Dimensional Array and Iteration Demo
- ' Author: Joseph L. Bolen
- ' Date Created: Aug 2014
- '
- ' Description: Using a ListBox to display the values of an one dimensional array using
- ' various iteration approaches. Also, show how to declare and load an
- ' array.
- '
- ' Documentation is at:
- ' App's screen capture image is at http://imgur.com/TFT7liY
- ' App's Visual Basic .NET code is at http://pastebin.com/hE0XhfkZ
- ' App's video tutorial is at http://www.youtube.com/user/bolenpresents
- '
- ' See Also, MSDN's article "Arrays in Visual Basic" at
- ' http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx and
- ' "How to: Initialize an Array Variable in Visual Basic" at
- ' http://msdn.microsoft.com/en-us/library/y13tek7e.aspx .
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class MainForm
- #Region " Declare Class level variables"
- Private index As Integer
- Private sumTotal As Double
- 'Private myArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1}
- #End Region
- ' Display in the ListBox information about an array using
- ' various iteration methods
- Private Sub DisplayButton_Click(sender As Object, e As EventArgs) _
- Handles DisplayButton.Click
- ' Clear ListBox.
- MyListBox.Items.Clear()
- '---------------------------------------------------------------------
- ' Declare the array.
- Dim myArray(5) As Double
- ' Load array with values.
- myArray(0) = 3
- myArray(1) = 2.4
- myArray(2) = 15.6
- myArray(3) = -3.4
- myArray(4) = 20.1
- myArray(5) = 2.1
- 'ReDim myArray(6)
- 'ReDim Preserve myArray(6)
- 'myArray(6) = -9.3
- '---------------------------------------------------------------------
- 'Populate the ListBox with information about the array.
- 'Prevent ListBox from painting until all items are added.
- MyListBox.BeginUpdate()
- ' Loading the ListBox regarding the array using the Items.Add method.
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Total Elements in Array: myArray.Length.ToString()) value is: " & myArray.Length.ToString())
- MyListBox.Items.Add("Upper index of the first Rank in Array: myArray.GetUpperBound(0) value is: " & myArray.GetUpperBound(0).ToString())
- MyListBox.Items.Add("myArray element 0 value is: " & myArray(0).ToString)
- MyListBox.Items.Add("myArray element 1 value is: " & myArray(1).ToString)
- MyListBox.Items.Add("myArray element 2 value is: " & myArray(2).ToString)
- MyListBox.Items.Add("myArray element 3 value is: " & myArray(3).ToString)
- MyListBox.Items.Add("myArray element 4 value is: " & myArray(4).ToString)
- MyListBox.Items.Add("myArray element 5 value is: " & myArray(5).ToString)
- 'MyListBox.Items.Add("myArray element 6 value is: " & myArray(6).ToString)
- 'TODO: Iterate through myArray.
- 'MyListBox.Items.Add("Sum of all elements of myArray is: " & sumTotal.ToString)
- ' Have ListBox resume painting.
- MyListBox.EndUpdate()
- End Sub
- End Class
- '=========================================================================
- ' Starting Point
- '=========================================================================
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: One Dimensional Array and Iteration Demo
- ' Author: Joseph L. Bolen
- ' Date Created: Aug 2014
- '
- ' Description: Using a ListBox to display the values of an one dimensional array using
- ' various iteration approaches. Also, show how to declare and load an
- ' array.
- '
- ' Documentation is at:
- ' App's screen capture image is at http://imgur.com/TFT7liY
- ' App's Visual Basic .NET code is at http://pastebin.com/hE0XhfkZ
- ' App's video tutorial is at http://www.youtube.com/user/bolenpresents
- '
- ' See Also, MSDN's article "Arrays in Visual Basic" at
- ' http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx and
- ' "How to: Initialize an Array Variable in Visual Basic" at
- ' http://msdn.microsoft.com/en-us/library/y13tek7e.aspx .
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class MainForm
- #Region " Declare Class level variables"
- Private index As Integer
- Private sumTotal As Double
- 'Private myArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1}
- #End Region
- ' Display in the ListBox information about an array using
- ' various iteration methods
- Private Sub DisplayButton_Click(sender As Object, e As EventArgs) _
- Handles DisplayButton.Click
- ' Clear ListBox.
- MyListBox.Items.Clear()
- '---------------------------------------------------------------------
- ' Declare the array.
- Dim myArray(5) As Double
- ' Load array with values.
- myArray(0) = 3
- myArray(1) = 2.4
- myArray(2) = 15.6
- myArray(3) = -3.4
- myArray(4) = 20.1
- myArray(5) = 2.1
- 'ReDim myArray(6)
- 'ReDim Preserve myArray(6)
- 'myArray(6) = -9.3
- '---------------------------------------------------------------------
- 'Populate the ListBox with information about the array.
- 'Prevent ListBox from painting until all items are added.
- MyListBox.BeginUpdate()
- ' Loading the ListBox regarding the array using the Items.Add method.
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Total Elements in Array: myArray.Length.ToString()) value is: " & myArray.Length.ToString())
- MyListBox.Items.Add("Upper index of the first Rank in Array: myArray.GetUpperBound(0) value is: " & myArray.GetUpperBound(0).ToString())
- MyListBox.Items.Add("myArray element 0 value is: " & myArray(0).ToString)
- MyListBox.Items.Add("myArray element 1 value is: " & myArray(1).ToString)
- MyListBox.Items.Add("myArray element 2 value is: " & myArray(2).ToString)
- MyListBox.Items.Add("myArray element 3 value is: " & myArray(3).ToString)
- MyListBox.Items.Add("myArray element 4 value is: " & myArray(4).ToString)
- MyListBox.Items.Add("myArray element 5 value is: " & myArray(5).ToString)
- 'MyListBox.Items.Add("myArray element 6 value is: " & myArray(6).ToString)
- 'TODO: Iterate through myArray.
- 'MyListBox.Items.Add("Sum of all elements of myArray is: " & sumTotal.ToString)
- ' Have ListBox resume painting.
- MyListBox.EndUpdate()
- End Sub
- End Class
- '=========================================================================
- ' Array Loading
- '=========================================================================
- ' Declare the array.
- Dim myArray(5) As Double
- 'Or ...
- Dim myArray(0 to 5) As Double
- ' Load array with values.
- myArray(0) = 3
- myArray(1) = 2.4
- myArray(2) = 15.6
- myArray(3) = -3.4
- myArray(4) = 20.1
- myArray(5) = 2.1
- ReDim Preserve myArray(6)
- myArray(6) = -9.3
- ' OR ...
- Dim myArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1}
- Dim myArray = {3, 2.4, 15.6, -3.4, 20.1, 2.1}
- Dim myArray = {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"}
- '=========================================================================
- ' Iteration Examples
- '=========================================================================
- 'Iterate through myArray.
- ' Using a Do / Loop to iterate through myArray.
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using a Do / Loop to iterate through myArray:")
- MyListBox.Items.Add("")
- index = 0
- sumTotal = 0
- Do
- MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
- sumTotal += myArray(index)
- index += 1
- If index > myArray.GetUpperBound(0) Then Exit Do
- Loop
- ' Using a Do / Loop While to iterate through myArray.
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using a Do / Loop While to iterate through myArray:")
- MyListBox.Items.Add("")
- index = 0
- sumTotal = 0
- Do
- MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
- sumTotal += myArray(index)
- index += 1
- Loop While index <= myArray.GetUpperBound(0)
- ' Using a Do / Loop Until to iterate through myArray.
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using a Do / Loop Until to iterate through myArray:")
- MyListBox.Items.Add("")
- index = 0
- sumTotal = 0
- Do
- MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
- sumTotal += myArray(index)
- index += 1
- Loop Until index > myArray.GetUpperBound(0)
- ' Using a Do While / Loop to iterate through myArray. (Preferred)
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using a Do While / Loop to iterate through myArray:")
- MyListBox.Items.Add("")
- index = 0
- sumTotal = 0
- Do While index <= myArray.GetUpperBound(0)
- MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
- sumTotal += myArray(index)
- index += 1
- Loop
- ' Using a Do Until / Loop to iterate through myArray.
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using a Do Until / Loop to iterate through myArray:")
- MyListBox.Items.Add("")
- index = 0
- sumTotal = 0
- Do Until index > myArray.GetUpperBound(0)
- MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
- sumTotal += myArray(index)
- index += 1
- Loop
- ' Using a For / Next to iterate through myArray using myArray.Length. (Not Recommended)
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("myArray.Length is: " & myArray.Length.ToString)
- MyListBox.Items.Add("Using a For / Next to iterate through myArray:")
- MyListBox.Items.Add("")
- sumTotal = 0
- For idx As Integer = 0 To (myArray.Length - 1) Step 1
- MyListBox.Items.Add("myArray element " & idx.ToString & " value is: " & myArray(idx).ToString)
- sumTotal += myArray(idx)
- Next
- ' Using a For / Next to iterate through myArray using myArray.GetUpperBound(0). (Preferred)
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("myArray.GetUpperBound(0) is: " & myArray.GetUpperBound(0).ToString)
- MyListBox.Items.Add("Using a For / Next to iterate through myArray:")
- MyListBox.Items.Add("")
- sumTotal = 0
- For idx As Integer = 0 To myArray.GetUpperBound(0)
- MyListBox.Items.Add("myArray element " & idx.ToString & " value is: " & myArray(idx).ToString)
- sumTotal += myArray(idx)
- Next
- ' Using For Each / Next to iterate through an sorted ascending myArray. (Preferred)
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using Array.Sort(myArray):")
- Array.Sort(myArray) ' Ascending.
- MyListBox.Items.Add("Using For / Each to iterate through myArray:")
- MyListBox.Items.Add("")
- sumTotal = 0
- For Each value As Double In myArray
- MyListBox.Items.Add(value.ToString)
- sumTotal += value
- Next
- ' Using For Each / Next to iterate through a sorted descending myArray.
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using Array.Sort(myArray) then Array.Reverse(myArray):")
- Array.Sort(myArray) ' Ascending.
- Array.Reverse(myArray) ' Now descending.
- MyListBox.Items.Add("Using For / Each to iterate through myArray:")
- MyListBox.Items.Add("")
- sumTotal = 0
- For Each value As Double In myArray
- MyListBox.Items.Add(value.ToString)
- sumTotal += value
- Next
- ' Using LINQ to get Min, Max and Sum values of myArray. (Just for fun!)
- MyListBox.Items.Add("----------------------------------------------------")
- MyListBox.Items.Add("Using LINQ ...")
- MyListBox.Items.Add("myArray.Min value is: " & myArray.Min.ToString())
- MyListBox.Items.Add("myArray.Max value is: " & myArray.Max.ToString())
- MyListBox.Items.Add("myArray.Sum value is: " & myArray.Sum.ToString())
- MyListBox.Items.Add("myArray.Average value is: " & myArray.Average.ToString("N3"))
Advertisement
Add Comment
Please, Sign In to add comment