Array & Iteration


SUBMITTED BY: Guest

DATE: Aug. 8, 2014, 9:57 a.m.

FORMAT: C#

SIZE: 20.3 kB

HITS: 23918

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

comments powered by Disqus