JayBeeOH

One Dimensional Array and Iteration Demo

Aug 13th, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 14.93 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2014. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   One Dimensional Array and Iteration Demo
  13. ' Author:         Joseph L. Bolen
  14. ' Date Created:   Aug 2014
  15. '
  16. ' Description:    Using a ListBox to display the values of an one dimensional array using
  17. '                 various iteration approaches.  Also, show how to declare and load an
  18. '                 array.
  19. '
  20. '                 Documentation is at:
  21. '                   App's screen capture image is at http://imgur.com/TFT7liY
  22. '                   App's Visual Basic .NET code is at http://pastebin.com/hE0XhfkZ
  23. '                   App's video tutorial is at http://www.youtube.com/user/bolenpresents
  24. '
  25. '                 See Also, MSDN's article "Arrays in Visual Basic" at
  26. '                   http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx and
  27. '                   "How to: Initialize an Array Variable in Visual Basic" at
  28. '                   http://msdn.microsoft.com/en-us/library/y13tek7e.aspx .
  29. '-------------------------------------------------------------------------------------------
  30. Option Strict On
  31.  
  32. Public Class MainForm
  33.  
  34. #Region "  Declare Class level variables"
  35.  
  36.     Private index As Integer
  37.     Private sumTotal As Double
  38.     'Private myArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1}
  39. #End Region
  40.  
  41.     ' Display in the ListBox information about an array using
  42.     ' various iteration methods
  43.     Private Sub DisplayButton_Click(sender As Object, e As EventArgs) _
  44.         Handles DisplayButton.Click
  45.  
  46.         ' Clear ListBox.
  47.         MyListBox.Items.Clear()
  48.  
  49.         '---------------------------------------------------------------------
  50.         ' Declare the array.
  51.         Dim myArray(5) As Double
  52.  
  53.         ' Load array with values.
  54.         myArray(0) = 3
  55.         myArray(1) = 2.4
  56.         myArray(2) = 15.6
  57.         myArray(3) = -3.4
  58.         myArray(4) = 20.1
  59.         myArray(5) = 2.1
  60.  
  61.         'ReDim myArray(6)
  62.         'ReDim Preserve myArray(6)
  63.         'myArray(6) = -9.3
  64.  
  65.         '---------------------------------------------------------------------
  66.  
  67.         'Populate the ListBox with information about the array.
  68.         'Prevent ListBox from painting until all items are added.
  69.         MyListBox.BeginUpdate()
  70.  
  71.         ' Loading the ListBox regarding the array using the Items.Add method.
  72.         MyListBox.Items.Add("----------------------------------------------------")
  73.         MyListBox.Items.Add("Total Elements in Array: myArray.Length.ToString()) value is: " & myArray.Length.ToString())
  74.         MyListBox.Items.Add("Upper index of the first Rank in Array: myArray.GetUpperBound(0) value is: " & myArray.GetUpperBound(0).ToString())
  75.  
  76.         MyListBox.Items.Add("myArray element 0 value is: " & myArray(0).ToString)
  77.         MyListBox.Items.Add("myArray element 1 value is: " & myArray(1).ToString)
  78.         MyListBox.Items.Add("myArray element 2 value is: " & myArray(2).ToString)
  79.         MyListBox.Items.Add("myArray element 3 value is: " & myArray(3).ToString)
  80.         MyListBox.Items.Add("myArray element 4 value is: " & myArray(4).ToString)
  81.         MyListBox.Items.Add("myArray element 5 value is: " & myArray(5).ToString)
  82.         'MyListBox.Items.Add("myArray element 6 value is: " & myArray(6).ToString)
  83.  
  84.         'TODO: Iterate through myArray.
  85.  
  86.  
  87.         'MyListBox.Items.Add("Sum of all elements of myArray is: " & sumTotal.ToString)
  88.  
  89.         ' Have ListBox resume painting.
  90.         MyListBox.EndUpdate()
  91.     End Sub
  92. End Class
  93. '=========================================================================
  94. ' Starting Point
  95. '=========================================================================
  96. '------------------------------------------------------------------------------------------
  97. '           Notice of My Copyright and Intellectual Property Rights
  98. '
  99. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  100. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  101. ' publish or provide such intellectual property to any other person or entity for any
  102. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  103. '
  104. '                 Copyright © 2014. All rights reserved.
  105. '        All trademarks remain the property of their respective owners.
  106. '-------------------------------------------------------------------------------------------
  107. ' Program Name:   One Dimensional Array and Iteration Demo
  108. ' Author:         Joseph L. Bolen
  109. ' Date Created:   Aug 2014
  110. '
  111. ' Description:    Using a ListBox to display the values of an one dimensional array using
  112. '                 various iteration approaches.  Also, show how to declare and load an
  113. '                 array.
  114. '
  115. '                 Documentation is at:
  116. '                   App's screen capture image is at http://imgur.com/TFT7liY
  117. '                   App's Visual Basic .NET code is at http://pastebin.com/hE0XhfkZ
  118. '                   App's video tutorial is at http://www.youtube.com/user/bolenpresents
  119. '
  120. '                 See Also, MSDN's article "Arrays in Visual Basic" at
  121. '                   http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx and
  122. '                   "How to: Initialize an Array Variable in Visual Basic" at
  123. '                   http://msdn.microsoft.com/en-us/library/y13tek7e.aspx .
  124. '-------------------------------------------------------------------------------------------
  125. Option Strict On
  126.  
  127. Public Class MainForm
  128.  
  129. #Region "  Declare Class level variables"
  130.  
  131.     Private index As Integer
  132.     Private sumTotal As Double
  133.     'Private myArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1}
  134. #End Region
  135.  
  136.     ' Display in the ListBox information about an array using
  137.     ' various iteration methods
  138.     Private Sub DisplayButton_Click(sender As Object, e As EventArgs) _
  139.         Handles DisplayButton.Click
  140.  
  141.         ' Clear ListBox.
  142.         MyListBox.Items.Clear()
  143.  
  144.         '---------------------------------------------------------------------
  145.         ' Declare the array.
  146.         Dim myArray(5) As Double
  147.  
  148.         ' Load array with values.
  149.         myArray(0) = 3
  150.         myArray(1) = 2.4
  151.         myArray(2) = 15.6
  152.         myArray(3) = -3.4
  153.         myArray(4) = 20.1
  154.         myArray(5) = 2.1
  155.  
  156.         'ReDim myArray(6)
  157.         'ReDim Preserve myArray(6)
  158.         'myArray(6) = -9.3
  159.  
  160.         '---------------------------------------------------------------------
  161.  
  162.         'Populate the ListBox with information about the array.
  163.         'Prevent ListBox from painting until all items are added.
  164.         MyListBox.BeginUpdate()
  165.  
  166.         ' Loading the ListBox regarding the array using the Items.Add method.
  167.         MyListBox.Items.Add("----------------------------------------------------")
  168.         MyListBox.Items.Add("Total Elements in Array: myArray.Length.ToString()) value is: " & myArray.Length.ToString())
  169.         MyListBox.Items.Add("Upper index of the first Rank in Array: myArray.GetUpperBound(0) value is: " & myArray.GetUpperBound(0).ToString())
  170.  
  171.         MyListBox.Items.Add("myArray element 0 value is: " & myArray(0).ToString)
  172.         MyListBox.Items.Add("myArray element 1 value is: " & myArray(1).ToString)
  173.         MyListBox.Items.Add("myArray element 2 value is: " & myArray(2).ToString)
  174.         MyListBox.Items.Add("myArray element 3 value is: " & myArray(3).ToString)
  175.         MyListBox.Items.Add("myArray element 4 value is: " & myArray(4).ToString)
  176.         MyListBox.Items.Add("myArray element 5 value is: " & myArray(5).ToString)
  177.         'MyListBox.Items.Add("myArray element 6 value is: " & myArray(6).ToString)
  178.  
  179.         'TODO: Iterate through myArray.
  180.  
  181.  
  182.         'MyListBox.Items.Add("Sum of all elements of myArray is: " & sumTotal.ToString)
  183.  
  184.         ' Have ListBox resume painting.
  185.         MyListBox.EndUpdate()
  186.     End Sub
  187. End Class
  188. '=========================================================================
  189. ' Array Loading
  190. '=========================================================================
  191.         ' Declare the array.
  192.         Dim myArray(5) As Double
  193.  
  194.     'Or ...
  195.  
  196.     Dim myArray(0 to 5) As Double
  197.  
  198.  
  199.         ' Load array with values.
  200.         myArray(0) = 3
  201.         myArray(1) = 2.4
  202.         myArray(2) = 15.6
  203.         myArray(3) = -3.4
  204.         myArray(4) = 20.1
  205.         myArray(5) = 2.1
  206.  
  207.         ReDim Preserve myArray(6)
  208.         myArray(6) = -9.3
  209.  
  210.         ' OR ...
  211.  
  212.         Dim myArray As Double() = {3, 2.4, 15.6, -3.4, 20.1, 2.1}
  213.  
  214.     Dim myArray =  {3, 2.4, 15.6, -3.4, 20.1, 2.1}
  215.  
  216.     Dim myArray = {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"}
  217.  
  218. '=========================================================================
  219. ' Iteration Examples
  220. '=========================================================================
  221. 'Iterate through myArray.
  222. ' Using a Do / Loop to iterate through myArray.
  223.         MyListBox.Items.Add("----------------------------------------------------")
  224.         MyListBox.Items.Add("Using a Do / Loop to iterate through myArray:")
  225.         MyListBox.Items.Add("")
  226.  
  227.         index = 0
  228.         sumTotal = 0
  229.         Do
  230.             MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
  231.             sumTotal += myArray(index)
  232.             index += 1
  233.             If index > myArray.GetUpperBound(0) Then Exit Do
  234.         Loop
  235.  
  236.  
  237. ' Using a Do / Loop While to iterate through myArray.
  238.         MyListBox.Items.Add("----------------------------------------------------")
  239.         MyListBox.Items.Add("Using a Do / Loop While to iterate through myArray:")
  240.         MyListBox.Items.Add("")
  241.  
  242.         index = 0
  243.         sumTotal = 0
  244.         Do
  245.             MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
  246.             sumTotal += myArray(index)
  247.             index += 1
  248.         Loop While index <= myArray.GetUpperBound(0)
  249.  
  250.  
  251. ' Using a Do / Loop Until to iterate through myArray.
  252.         MyListBox.Items.Add("----------------------------------------------------")
  253.         MyListBox.Items.Add("Using a Do / Loop Until to iterate through myArray:")
  254.         MyListBox.Items.Add("")
  255.  
  256.         index = 0
  257.         sumTotal = 0
  258.         Do
  259.             MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
  260.             sumTotal += myArray(index)
  261.             index += 1
  262.         Loop Until index > myArray.GetUpperBound(0)
  263.  
  264.  
  265. ' Using a Do While / Loop to iterate through myArray. (Preferred)
  266.         MyListBox.Items.Add("----------------------------------------------------")
  267.         MyListBox.Items.Add("Using a Do While / Loop to iterate through myArray:")
  268.         MyListBox.Items.Add("")
  269.  
  270.         index = 0
  271.         sumTotal = 0
  272.         Do While index <= myArray.GetUpperBound(0)
  273.             MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
  274.             sumTotal += myArray(index)
  275.             index += 1
  276.         Loop
  277.  
  278.  
  279. ' Using a Do Until / Loop to iterate through myArray.
  280.         MyListBox.Items.Add("----------------------------------------------------")
  281.         MyListBox.Items.Add("Using a Do Until / Loop to iterate through myArray:")
  282.         MyListBox.Items.Add("")
  283.  
  284.         index = 0
  285.         sumTotal = 0
  286.         Do Until index > myArray.GetUpperBound(0)
  287.             MyListBox.Items.Add("myArray element " & index.ToString & " value is: " & myArray(index).ToString)
  288.             sumTotal += myArray(index)
  289.             index += 1
  290.         Loop
  291.  
  292.  
  293. ' Using a For / Next to iterate through myArray using myArray.Length. (Not Recommended)
  294.         MyListBox.Items.Add("----------------------------------------------------")
  295.         MyListBox.Items.Add("myArray.Length is: " & myArray.Length.ToString)
  296.         MyListBox.Items.Add("Using a For / Next to iterate through myArray:")
  297.         MyListBox.Items.Add("")
  298.  
  299.         sumTotal = 0
  300.         For idx As Integer = 0 To (myArray.Length - 1) Step 1
  301.             MyListBox.Items.Add("myArray element " & idx.ToString & " value is: " & myArray(idx).ToString)
  302.             sumTotal += myArray(idx)
  303.         Next
  304.  
  305.  
  306. ' Using a For / Next to iterate through myArray using myArray.GetUpperBound(0). (Preferred)
  307.         MyListBox.Items.Add("----------------------------------------------------")
  308.         MyListBox.Items.Add("myArray.GetUpperBound(0) is: " & myArray.GetUpperBound(0).ToString)
  309.         MyListBox.Items.Add("Using a For / Next to iterate through myArray:")
  310.         MyListBox.Items.Add("")
  311.  
  312.         sumTotal = 0
  313.         For idx As Integer = 0 To myArray.GetUpperBound(0)
  314.             MyListBox.Items.Add("myArray element " & idx.ToString & " value is: " & myArray(idx).ToString)
  315.             sumTotal += myArray(idx)
  316.         Next
  317.  
  318.  
  319. ' Using For Each / Next to iterate through an sorted ascending myArray. (Preferred)
  320.         MyListBox.Items.Add("----------------------------------------------------")
  321.         MyListBox.Items.Add("Using Array.Sort(myArray):")
  322.         Array.Sort(myArray) ' Ascending.
  323.  
  324.         MyListBox.Items.Add("Using For / Each to iterate through myArray:")
  325.         MyListBox.Items.Add("")
  326.  
  327.         sumTotal = 0
  328.         For Each value As Double In myArray
  329.             MyListBox.Items.Add(value.ToString)
  330.             sumTotal += value
  331.         Next
  332.  
  333.  
  334. ' Using For Each / Next to iterate through a sorted descending myArray.
  335.         MyListBox.Items.Add("----------------------------------------------------")
  336.         MyListBox.Items.Add("Using Array.Sort(myArray) then Array.Reverse(myArray):")
  337.         Array.Sort(myArray) ' Ascending.
  338.         Array.Reverse(myArray) ' Now descending.
  339.  
  340.         MyListBox.Items.Add("Using For / Each to iterate through myArray:")
  341.         MyListBox.Items.Add("")
  342.  
  343.         sumTotal = 0
  344.         For Each value As Double In myArray
  345.             MyListBox.Items.Add(value.ToString)
  346.             sumTotal += value
  347.         Next
  348.  
  349.  
  350. ' Using LINQ to get Min, Max and Sum values of myArray. (Just for fun!)
  351.         MyListBox.Items.Add("----------------------------------------------------")
  352.         MyListBox.Items.Add("Using LINQ ...")
  353.         MyListBox.Items.Add("myArray.Min value is: " & myArray.Min.ToString())
  354.         MyListBox.Items.Add("myArray.Max value is: " & myArray.Max.ToString())
  355.         MyListBox.Items.Add("myArray.Sum value is: " & myArray.Sum.ToString())
  356.         MyListBox.Items.Add("myArray.Average value is: " & myArray.Average.ToString("N3"))
Advertisement
Add Comment
Please, Sign In to add comment