'------------------------------------------------------------------------------------------ ' 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: Array and Iteration Demo ' Author: Joseph L. Bolen ' Date Created: Jul 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. In addition, the program will show various ways to populate a ' ListBox's item collection. ' ' Documentation is at: ' App's screen capture image is at http://imgur.com/7PSfkjX ' App's Visual Basic .NET code is at http://pastebin.com/VZz3vTZX ' 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 Imports System Imports System.IO Public Class MainForm #Region " Declare Class level variables." Const namesFile As String = "Names.txt" Dim idx As Integer Dim total As Double #End Region ' Load the ListBox. Private Sub LoadButton_Click(sender As Object, e As EventArgs) _ Handles LoadButton.Click ' Clear ListBox. MyListBox.Items.Clear() ' Declare the array. Dim myNumericArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1} 'Populate the ListBox with infomation 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("myNumericArray.Length value is: " & myNumericArray.Length.ToString()) MyListBox.Items.Add("myNumericArray.Count value is " & myNumericArray.Count().ToString) MyListBox.Items.Add("myNumericArray.GetUpperBound(0) value is: " & myNumericArray.GetUpperBound(0).ToString()) MyListBox.Items.Add("myNumericArray.Min value is: " & myNumericArray.Min.ToString()) MyListBox.Items.Add("myNumericArray.Max value is: " & myNumericArray.Max.ToString()) MyListBox.Items.Add("myNumericArray.Sum value is: " & myNumericArray.Sum.ToString()) MyListBox.Items.Add("myNumericArray.Average value is: " & myNumericArray.Average.ToString("N3")) ' Using a For / Next to interate through myNumericArray and myNumericArray.Length. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("myNumericArray.Length is: " & myNumericArray.Length.ToString) MyListBox.Items.Add("Using a For / Next to interate through myNumericArray:") MyListBox.Items.Add("") total = 0 For index As Integer = 0 To (myNumericArray.Length - 1) Step 1 MyListBox.Items.Add("myNumericArray element " & index.ToString & " value is: " & myNumericArray(index).ToString) total += myNumericArray(index) Next MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Have ListBox resume painting. MyListBox.EndUpdate() End Sub ' Load the ListBox using code from a file. Private Sub LoadFromFile() ' Add above Public Class ... ' Imports System ' Imports System.IO Try Using sr As New StreamReader(namesFile) Dim line As String MyListBox.BeginUpdate() Do While Not sr.EndOfStream line = sr.ReadLine MyListBox.Items.Add(line) Loop MyListBox.EndUpdate() End Using Catch ex As Exception MessageBox.Show("The file '" & namesFile & "' could not be read.", "File Input Problem", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try 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: Array and Iteration Demo ' Author: Joseph L. Bolen ' Date Created: Jul 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. In addition, the program will show various ways to populate a ' ListBox's item collection. ' ' 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 Imports System Imports System.IO Public Class MainForm #Region " Declare Class level variables." Const namesFile As String = "Names.txt" Dim idx As Integer Dim total As Double #End Region ' Load the ListBox. Private Sub LoadButton_Click(sender As Object, e As EventArgs) _ Handles LoadButton.Click End Sub ' Load the ListBox using code from a file. Private Sub LoadFromFile() ' Add above Public Class ... ' Imports System ' Imports System.IO Try Using sr As New StreamReader(namesFile) Dim line As String MyListBox.BeginUpdate() Do While Not sr.EndOfStream line = sr.ReadLine MyListBox.Items.Add(line) Loop MyListBox.EndUpdate() End Using Catch ex As Exception MessageBox.Show("The file '" & namesFile & "' could not be read.", "File Input Problem", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub End Class '=================================================================================== ' Load ListBox '=================================================================================== ' Clear ListBox MyListBox.Items.Clear() 'Load the ListBox using code. ' Loading the ListBox with names using the Items.Add method. MyListBox.BeginUpdate() MyListBox.Items.Add("Joe") MyListBox.Items.Add("Sally") MyListBox.Items.Add("Jim") MyListBox.Items.Add("George") MyListBox.Items.Add("Susan") MyListBox.Items.Add("Linda") MyListBox.Items.Add("Peter") MyListBox.EndUpdate() ' Loading the ListBox with names using Items.AddRange MyListBox.Items.AddRange(New Object() {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"}) 'Loading the ListBox with string values from an array using Items.AddRange Dim myStringArray As String() = {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"} MyListBox.Items.AddRange(myStringArray) ' Load the ListBox with names using code from a file. LoadFromFile() '=================================================================================== ' Array Loading '=================================================================================== ' Declare the array. Dim myNumericArray(5) As Double ' Load array with values. myNumericArray(0) = 3 myNumericArray(1) = 2.4 myNumericArray(2) = 15.6 myNumericArray(3) = -3.4 myNumericArray(4) = 20.1 myNumericArray(5) = 2.1 'ReDim Preserve myNumericArray(6) 'myNumericArray(6) = -9.3 ' OR ... 'Dim myNumericArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1} '=================================================================================== ' Iteration Examples '=================================================================================== ' Declare the array. Dim myNumericArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1} 'Populate the ListBox with infomation 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("myNumericArray.Length value is: " & myNumericArray.Length.ToString()) MyListBox.Items.Add("myNumericArray.Count value is " & myNumericArray.Count().ToString) MyListBox.Items.Add("myNumericArray.GetUpperBound(0) value is: " & myNumericArray.GetUpperBound(0).ToString()) MyListBox.Items.Add("myNumericArray.Min value is: " & myNumericArray.Min.ToString()) MyListBox.Items.Add("myNumericArray.Max value is: " & myNumericArray.Max.ToString()) MyListBox.Items.Add("myNumericArray.Sum value is: " & myNumericArray.Sum.ToString()) MyListBox.Items.Add("myNumericArray.Average value is: " & myNumericArray.Average.ToString("N3")) ' Have ListBox resume painting. MyListBox.EndUpdate() '================================================================================= 'Iterate through myNumericArray. ' Using a Do / Loop to interate through myNumericArray. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("Using a Do / Loop to interate through myNumericArray:") MyListBox.Items.Add("") idx = 0 total = 0 Do MyListBox.Items.Add("myNumericArray element " & idx.ToString & " value is: " & myNumericArray(idx).ToString) total += myNumericArray(idx) idx += 1 If idx > myNumericArray.GetUpperBound(0) Then Exit Do Loop MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using a Do / Loop While to interate through myNumericArray. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("Using a Do / Loop While to interate through myNumericArray:") MyListBox.Items.Add("") idx = 0 total = 0 Do MyListBox.Items.Add("myNumericArray element " & idx.ToString & " value is: " & myNumericArray(idx).ToString) total += myNumericArray(idx) idx += 1 Loop While idx < myNumericArray.Length MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using a Do / Loop Until to interate through myNumericArray. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("Using a Do / Loop Until to interate through myNumericArray:") MyListBox.Items.Add("") idx = 0 total = 0 Do MyListBox.Items.Add("myNumericArray element " & idx.ToString & " value is: " & myNumericArray(idx).ToString) total += myNumericArray(idx) idx += 1 Loop Until idx >= myNumericArray.Length MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using a Do While / Loop to interate through myNumericArray. (Preferred) MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("Using a Do While / Loop to interate through myNumericArray:") MyListBox.Items.Add("") idx = 0 total = 0 Do While idx < myNumericArray.Length MyListBox.Items.Add("myNumericArray element " & idx.ToString & " value is: " & myNumericArray(idx).ToString) total += myNumericArray(idx) idx += 1 Loop MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using a Do Until / Loop to interate through myNumericArray. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("Using a Do Until / Loop to interate through myNumericArray:") MyListBox.Items.Add("") idx = 0 total = 0 Do Until idx >= myNumericArray.Length MyListBox.Items.Add("myNumericArray element " & idx.ToString & " value is: " & myNumericArray(idx).ToString) total += myNumericArray(idx) idx += 1 Loop MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using a For / Next to interate through myNumericArray and myNumericArray.Length. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("myNumericArray.Length is: " & myNumericArray.Length.ToString) MyListBox.Items.Add("Using a For / Next to interate through myNumericArray:") MyListBox.Items.Add("") total = 0 For index As Integer = 0 To (myNumericArray.Length - 1) Step 1 MyListBox.Items.Add("myNumericArray element " & index.ToString & " value is: " & myNumericArray(index).ToString) total += myNumericArray(index) Next MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using a For / Next to interate through myNumericArray and myNumericArray.Count. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("myNumericArray.Count is " & myNumericArray.Count().ToString) MyListBox.Items.Add("Using a For / Next to interate through myNumericArray:") MyListBox.Items.Add("") total = 0 For index As Integer = 0 To (myNumericArray.Count - 1) MyListBox.Items.Add("myNumericArray element " & index.ToString & " value is: " & myNumericArray(index).ToString) total += myNumericArray(index) Next MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using a For / Next to interate through myNumericArray and myNumericArray.GetUpperBound(0). (Preferred) MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("myNumericArray.GetUpperBound(0) is: " & myNumericArray.GetUpperBound(0).ToString) MyListBox.Items.Add("Using a For / Next to interate through myNumericArray:") MyListBox.Items.Add("") total = 0 For index As Integer = 0 To myNumericArray.GetUpperBound(0) MyListBox.Items.Add("myNumericArray element " & index.ToString & " value is: " & myNumericArray(index).ToString) total += myNumericArray(index) Next MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using For Each / Next to interate through an sorted acending myNumericArray. (Preferred) MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("Using Array.Sort(myNumericArray):") Array.Sort(myNumericArray) ' Ascending. MyListBox.Items.Add("Using For / Each to interate through myNumericArray:") MyListBox.Items.Add("") total = 0 For Each value As Double In myNumericArray MyListBox.Items.Add(value.ToString) total += value Next MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using For Each / Next to interate through a sorted decending myNumericArray. MyListBox.Items.Add("----------------------------------------------------") MyListBox.Items.Add("Using Array.Sort(myNumericArray) then Array.Reverse(myNumericArray):") Array.Sort(myNumericArray) ' Ascending. Array.Reverse(myNumericArray) ' Now descending. MyListBox.Items.Add("Using For / Each to interate through myNumericArray:") MyListBox.Items.Add("") total = 0 For Each value As Double In myNumericArray MyListBox.Items.Add(value.ToString) total += value Next MyListBox.Items.Add("Total of all elements of myNumericArray is: " & total.ToString) ' Using LINQ to get Max value and Sum of myNumericArray. (Just for fun!) MyListBox.Items.Add("----------------------------------------------------") Dim arrayMax = (From value In myNumericArray).Max() MyListBox.Items.Add("Maximum value in myNumericArray using LINQ is: " & arrayMax.ToString) Dim arraySum = (From value In myNumericArray).Sum() MyListBox.Items.Add("Sum of all values in myNumericArray using LINQ is: " & arraySum.ToString) ' Have ListBox resume painting. MyListBox.EndUpdate()