{"id":1352,"date":"2008-02-18T00:00:38","date_gmt":"2008-02-18T05:00:38","guid":{"rendered":"http:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays"},"modified":"2008-02-13T08:49:37","modified_gmt":"2008-02-13T13:49:37","slug":"simple-vb-tutorials-%e2%80%93-all-about-arrays","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays","title":{"rendered":"Simple VB Code and All About Arrays"},"content":{"rendered":"

Arrays are a very useful part of VB programming and can be used for many different things. Arrays are a collection of variables that are all the same type. They can be as big or little as you need them to be. For example, lets say I need to use 5 numbers in one of my applications. Instead of using 5 different integers and I can use an integer array. The following code is dimming the array for use in the application (As mentioned in the previous tutorial(s) any variable that you want to be used by the whole application needs to be dimmed right under the Public Class for the form.):<\/p>\n

\r\nPublic Class frmArrays\r\n    Dim intNumber(4) As Integer\r\nEnd Class\r\n<\/pre>\n

Notice how the intNumber is followed by a number that is enclosed in parenthesis. This number tells the application how large to make the integer. By this I mean we can store 5 different numbers in the integer we just created. (0 counts as one \u2013 so 4 actually means five.) To call then we simply put the number of the array we want to retrieve in those parentheses. Now that you know what an array is and what it does, let\u2019s make an example and put it to use. Let\u2019s make our form fist. As usual, I will show two forms, one with the names of the objects on the form, and one showing the normal text.<\/p>\n

All_About_Arrays_01<\/p>\n

Note, you should make all of the text boxes in the lower portion ReadOnly. There is a ReadOnly setting in the properties. Set it to true.Now that we have our forms made, let\u2019s being coding. As usual, let\u2019s do our exit button first since it\u2019s the easiest. Double-click on it and enter \u201cEnd\u201d as shown:<\/p>\n

\r\n    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click\r\n        End\r\n    End Sub \r\n<\/pre>\n

Our next section of code is the add integer button. We are going to make it add the integer to the next available slot in the array. In order to do this we are going to use another single integer to determine how many times we have added an integer to our array. So we need to Dim the integer times under our Class for the form (as above):<\/p>\n

\r\n    Dim intTries As Integer\r\n<\/pre>\n

Now we need to add the code that will actually check for the array to be used 5 times. Since the integer that we created (intTries) will be starting at zero, we need to stop it once it reaches 5. We also need to make sure that the characters entered into the textbox that we have are indeed only numbers. To do this we will use a neat feature built into VB 2005 and greater called TryParse. It automatically tries to parse the data in the textbox and will fail if it contains text:<\/p>\n

\r\n    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click\r\n        If intTries = 5 Then\r\n            MsgBox("Array is full! Time to show values.", MsgBoxStyle.Exclamation, "Error!")\r\n        Else\r\n            If Integer.TryParse(txtInteger.Text, intNumber(intTries)) = False Then\r\n                MsgBox("There are letters in the textbox, these cannot be added!", MsgBoxStyle.Exclamation, "Numbers Only!")\r\n            Else\r\n                intTries = intTries + 1\r\n            End If\r\n        End If\r\n        txtInteger.Text = ""\r\n    End Sub \r\n<\/pre>\n

The first thing the code is doing is checking to make sure we have not reached the limit of our integer. It does this by checking our integer that we made. If it is it alerts the user that the array is full. If not it check the text box to see if there are any letters or punctuation in them. The code in that line is looking in the text box and sets the result to intNumber(intTries) \u2013 which in this case is intNumber(0). If there are no letters it will simply set it to whatever is in the textbox. If there are letters it will set it to zero. Our code only increments intTries up by one IF the textbox contains only number. The next time around it will be storing the integer to intNumber(intTries) again but this time it will be equal to intNumber(1). Lastly it blanks out our textbox for entry of another number. Now we need to set the code to show the array in our text boxes that we made on the bottom part of the form. <\/p>\n

\r\n    Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click\r\n        txtInteger1.Text = intNumber(0)\r\n        txtInteger2.Text = intNumber(1)\r\n        txtInteger3.Text = intNumber(2)\r\n        txtInteger4.Text = intNumber(3)\r\n        txtInteger5.Text = intNumber(4)\r\n        MsgBox("Integers need reset. Please click reset array to start over.", MsgBoxStyle.Exclamation, "Complete!")\r\n    End Sub \r\n<\/pre>\n

All this code does is set the text of the textbox equal to one of the integers in the array and alerts the user to reset the array before trying to add more integers to it. Now the last thing we need to do is add the code to the clear button. <\/p>\n

\r\n    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click\r\n        intTries = 0\r\n        intNumber(0) = 0\r\n        intNumber(1) = 0\r\n        intNumber(2) = 0\r\n        intNumber(3) = 0\r\n        intNumber(4) = 0\r\n        txtInteger1.Text = ""\r\n        txtInteger2.Text = ""\r\n        txtInteger3.Text = ""\r\n        txtInteger4.Text = ""\r\n        txtInteger5.Text = ""\r\n    End Sub \r\n<\/pre>\n

This sets all integers back to zero and blanks out all of the text boxes on the bottom part of the form. This concludes the code we need to write. In the next tutorials I will show you how to prevent writing a 0 to each integer by using a Do, While statement to get it done easier. Now, let\u2019s test our program. If you type in numbers and letters into the box and hit add to array you should see the following:<\/p>\n

All_About_Arrays_02<\/p>\n

Now you should be able to add integers to it until you get the following box to alert you that it is full:<\/p>\n

All_About_Arrays_03<\/p>\n

One the array is full we should now be able to show the contents of it in our boxes below. Click on Show Ints. to populate the boxes and you will get this reminder:<\/p>\n

All_About_Arrays_04<\/p>\n

Once you click away you should see the integers that you typed in as shown below:<\/p>\n

All_About_Arrays_05<\/p>\n

Now that you have seen them all you should be able to hit clear and start all over again. This concludes this tutorial and thank-you for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"

This is one of the many tutorials that you can use to learn the basics of VB. These tutorials will help lay the groundwork so you can move on to more advanced programming. In this tutorial, we look at arrays and how they are used in VB programming. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,26],"tags":[],"yoast_head":"\nSimple VB Code and All About Arrays<\/title>\n<meta name=\"description\" content=\"This is one of the many tutorials that you can use to learn the basics of VB. These tutorials will help lay the groundwork so you can move on to more advanced programming. In this tutorial, we look at arrays and how they are used in VB programming.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-\u2013-all-about-arrays\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Maguire\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/all_about_arrays_01.jpg\",\"contentUrl\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/all_about_arrays_01.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#webpage\",\"url\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays\",\"name\":\"Simple VB Code and All About Arrays\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#primaryimage\"},\"datePublished\":\"2008-02-18T05:00:38+00:00\",\"dateModified\":\"2008-02-13T13:49:37+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"This is one of the many tutorials that you can use to learn the basics of VB. These tutorials will help lay the groundwork so you can move on to more advanced programming. In this tutorial, we look at arrays and how they are used in VB programming.\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple VB Code and All About Arrays\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\",\"name\":\"Mike Maguire\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"caption\":\"Mike Maguire\"},\"sameAs\":[\"http:\/\/mikemaguire.me\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple VB Code and All About Arrays","description":"This is one of the many tutorials that you can use to learn the basics of VB. These tutorials will help lay the groundwork so you can move on to more advanced programming. In this tutorial, we look at arrays and how they are used in VB programming.","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":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-\u2013-all-about-arrays","twitter_misc":{"Written by":"Mike Maguire","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#primaryimage","inLanguage":"en-US","url":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/all_about_arrays_01.jpg","contentUrl":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/all_about_arrays_01.jpg"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#webpage","url":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays","name":"Simple VB Code and All About Arrays","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#primaryimage"},"datePublished":"2008-02-18T05:00:38+00:00","dateModified":"2008-02-13T13:49:37+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"This is one of the many tutorials that you can use to learn the basics of VB. These tutorials will help lay the groundwork so you can move on to more advanced programming. In this tutorial, we look at arrays and how they are used in VB programming.","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-tutorials-%e2%80%93-all-about-arrays#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Simple VB Code and All About Arrays"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b","name":"Mike Maguire","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","caption":"Mike Maguire"},"sameAs":["http:\/\/mikemaguire.me"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1352"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=1352"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1352\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}