{"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>\nNotice 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
<\/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>\nOur 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>\nNow 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>\nThe 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>\nAll 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>\nThis 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
<\/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
<\/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
<\/p>\n
Once you click away you should see the integers that you typed in as shown below:<\/p>\n
<\/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":"\n
Simple VB Code and All About Arrays<\/title>\n\n\n\n\n\t\n\t\n\t\n