{"id":6013,"date":"2015-03-24T11:45:55","date_gmt":"2015-03-24T03:45:55","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=6013"},"modified":"2018-06-22T16:26:50","modified_gmt":"2018-06-22T08:26:50","slug":"visual-basic-2015-lesson-10-working-arrays","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-10-working-arrays\/","title":{"rendered":"Visual Basic 2015 Lesson 10: Working with Arrays"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a title=\"visual basic 2015 lesson 9\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-9-working-variable-constants\/\">[Lesson 9] <\/a>&lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a> &gt;&gt;<a title=\"visual basic 2015 tutorial lesson 11\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-11-performing-arithmetic-operations\/\"> [Lesson 11]<\/a><\/h4>\n<h3>10.1 The Concept\u00a0of Array<\/h3>\n<p>In this lesson, we shall learn how to work with a group of variables. A group of variables of the same data type is known as an <strong>array<\/strong> in Visual Basic 2015. To work with a single item, we only need to declare one variable. However,\u00a0 to deal with multiple items of similar type, we need to declare an array of variables. Otherwise, it will be rather tedious and time-consuming to declare one hundred\u00a0different names. In order to fix the issue, we declare only one array to store those names. We differentiate each item in the array by using subscript, for example, name(1), name(2),name(3) &#8230;&#8230;.etc.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nA two-dimensional array in Visual Basic 2015 is a table of items that is made up of rows and columns. The way to reference an element in a one-dimensional array is ArrayName(x), where x is the index or position number of the element. The way to reference an element in a\u00a0two-dimensional array is ArrayName(x,y), where (x,y) is the index or position number of the\u00a0element. Usually, it is sufficient to use one dimensional and two-dimensional array , you only need to use higher dimensional arrays if you need to deal with more complex problems. Let me illustrate the arrays with tables.<\/p>\n<h3>10.2 Dimension of an Array<\/h3>\n<p>In Visual Basic 2015, an array can be one-dimensional or multidimensional. One dimensional array is like a list of items or a table that consists of one row of items or one column of items.<\/p>\n<h4>Table 10.1: One Dimensional Array<\/h4>\n<table style=\"table-layout: fixed; border: 1 px solid black;\">\n<tbody>\n<tr>\n<td>Student Name<\/td>\n<td>Name(0)<\/td>\n<td>Name(1)<\/td>\n<td>Name(2)<\/td>\n<td>Name(3)<\/td>\n<td>Name(4)<\/td>\n<td>Name(5)<\/td>\n<td>Name(6)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Table 10.2: Two Dimensional Array<\/h4>\n<table>\n<tbody>\n<tr>\n<td>Name(0,0)<\/td>\n<td>Name(0,1)<\/td>\n<td>Name(0,2)<\/td>\n<td>Name(0,3)<\/td>\n<\/tr>\n<tr>\n<td>Name(1,0)<\/td>\n<td>Name(1,1)<\/td>\n<td>Name(1,2)<\/td>\n<td>Name(1,3)<\/td>\n<\/tr>\n<tr>\n<td>Name(2,0)<\/td>\n<td>Name(2,1)<\/td>\n<td>Name(2,2)<\/td>\n<td>Name(2,3)<\/td>\n<\/tr>\n<tr>\n<td>Name(3,0)<\/td>\n<td>Name(3,1)<\/td>\n<td>Name(3,2)<\/td>\n<td>Name(3,3)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>10.2 Declaring Arrays<\/h3>\n<p>In Visual Basic 2015, we can use the Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declares an array that could be used only in a local procedure or module.<\/p>\n<p>The statement to declare a one-dimensional array is as follows:<\/p>\n<pre style=\"font-size: 110%;\">Dim arrayName(n) as dataType\r\n<\/pre>\n<p>where n indicates the last index in the array. Please note that n does not indicate the number of elements in the array, it is one less than the number of elements (n-1) because the first element is always the zeroth element. The first element is arrayName(0), the second element is arrayName(1), the third element is\u00a0arrayName(2) and so on. The number of elements in an array is also known as length, we can retrieve the length of an array using the syntax\u00a0<strong>arrayName.length\u00a0<\/strong><\/p>\n<p>For example,<\/p>\n<pre style=\"font-size: 110%;\">Dim CusName(10) as String\r\n<\/pre>\n<p>will declare an array that consists of \u00a011 elements starting from CusName(0) through to CusName(10)<\/p>\n<p>To find out the length of the array, you can write the following code:<\/p>\n<h4><strong>Example 10.1<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\n\r\nDim CusName(10) As String\r\nMsgBox(CusName.Length)\r\n\r\nEnd Sub\r\n<\/pre>\n<p>Running the program will produce a message box that displays the length of the array i.e 11, as shown in Figure 10.1<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4416\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg\" alt=\"vb2013_figure10.1\" width=\"154\" height=\"155\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 10.1<\/strong><\/p>\n<p>You might also declare an array with a non-zero starting index buy initialize an index value other than zero, as follows:<\/p>\n<pre style=\"font-size: 110%;\">Dim arrayname As DataType()\r\n\r\narrayName=New String(){1,2,3,....,n)\r\n<\/pre>\n<p>This array will consist of n elements, starting with arrayName(1)<\/p>\n<h4><strong>\u00a0Example 10.2<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\n\r\nDim CusName As String()\r\nCusName = New String() {1, 2, 3}\r\nMsgBox(CusName.Length)\r\n\r\nEnd Sub\r\n<\/pre>\n<p>The message box will display the length as 3<\/p>\n<p>&nbsp;<\/p>\n<p>The statement to declare a two-dimensional array is as follow:<\/p>\n<pre style=\"font-size: 110%;\">Dim ArrayName(m,n) as dataType\r\n<\/pre>\n<p>where m and n indicate the last indices in the array. The number of elements or the length of the array is (m+1)x(n+1)<\/p>\n<h4><strong>Example 10.3<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\n\r\nDim CusName(5,6) As String\r\nMsgBox(CusName.Length)\r\n\r\nEnd Sub\r\n<\/pre>\n<p>Running the visual basic 2015 program and the message box will display a length of 42, as shown in Figure 10.2<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4425\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.2.jpg\" alt=\"vb2013_figure10.2\" width=\"154\" height=\"155\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 10.2<\/strong><\/p>\n<h4><strong>Example 10.4<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\nDim num As Integer\r\nDim CusName(5) As String\r\n\r\nFor num = 0 To 5\r\nCusName(num) = InputBox(\"Enter the customer name\", \"Enter Name\")\r\nListBox1.Items.Add(CusName(num))\r\nNext\r\nEnd Sub\r\n<\/pre>\n<p>This program will prompt the user to enter names in an input box for a total of 6 times and the names will be entered into a list box, as shown in Figure 10.3 and Figure 10.4<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4430\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.3.jpg\" alt=\"vb2013_figure10.3\" width=\"369\" height=\"159\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>\u00a0Figure 10.3\u00a0<\/strong><\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4432\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.4.jpg\" alt=\"vb2013_figure10.4\" width=\"300\" height=\"300\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 10.4\u00a0<\/strong><\/p>\n<div><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"1492877908\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/div>\n<h4 style=\"text-align: center;\"><a title=\"visual basic 2015 lesson 9\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-9-working-variable-constants\/\">[Lesson 9] <\/a>&lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a> &gt;&gt; <a title=\"visual basic 2015 tutorial lesson 11\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-11-performing-arithmetic-operations\/\">[Lesson 11]<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 9] &lt;&lt; [Contents] &gt;&gt; [Lesson 11] 10.1 The Concept\u00a0of Array In this lesson, we shall learn how to work with a group of variables. A group of variables of the same data type is known as an array in Visual Basic 2015. To work with a single item, we only need to declare one &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-10-working-arrays\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2015 Lesson 10: Working with Arrays<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-6013","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2015 Lesson 10: Working with Arrays - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article describe arrays in Visual Basic 2015\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2015 Lesson 10: Working with Arrays - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article describe arrays in Visual Basic 2015\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-22T08:26:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-10-working-arrays\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html\",\"name\":\"Visual Basic 2015 Lesson 10: Working with Arrays - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg\",\"datePublished\":\"2015-03-24T03:45:55+00:00\",\"dateModified\":\"2018-06-22T08:26:50+00:00\",\"description\":\"This article describe arrays in Visual Basic 2015\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg\",\"width\":154,\"height\":155},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2015 Lesson 10: Working with Arrays\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2015 Lesson 10: Working with Arrays - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article describe arrays in Visual Basic 2015","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2015 Lesson 10: Working with Arrays - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article describe arrays in Visual Basic 2015","og_url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-22T08:26:50+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-10-working-arrays\/","url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html","name":"Visual Basic 2015 Lesson 10: Working with Arrays - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg","datePublished":"2015-03-24T03:45:55+00:00","dateModified":"2018-06-22T08:26:50+00:00","description":"This article describe arrays in Visual Basic 2015","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure10.1.jpg","width":154,"height":155},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson10.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2015 Lesson 10: Working with Arrays"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=6013"}],"version-history":[{"count":51,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6013\/revisions"}],"predecessor-version":[{"id":13025,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6013\/revisions\/13025"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=6013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=6013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=6013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}