{"id":1339,"date":"2008-02-11T00:00:19","date_gmt":"2008-02-11T05:00:19","guid":{"rendered":"http:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers"},"modified":"2008-02-08T21:42:21","modified_gmt":"2008-02-09T02:42:21","slug":"simple-vb-code-strings-booleans-and-integers","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers","title":{"rendered":"Simple VB Code with Strings Booleans and Integers"},"content":{"rendered":"

In this tutorial we are going to learn to use strings, Booleans, and Integers to check for more specific features before events can happen. An Integer is a type of variable that can only contain numbers. A string is a variable that can text and numbers. Strings must be enclosed in quotation marks when setting them. A Boolean is a variable that is either true of false. In order to use these variables in our applications we need to \u201cdimensionalize\u201d (Dim in VB) them to be ready. By using dim we tell the application what the name of it is and what type of variable it is. The naming convention should be the same as the controls in our last tutorials (three letter abbreviations, followed by a descriptive name), such as strString ,intInteger, and booBoolean. Dim need to be made directly under the Class set in the code. Puttin them here allows all Sub routines to be able to call them. If you were to dim on during a Sub it wont only be able to be used during that particular sub. The ones that I am Dim\u2019ing are shown below:<\/p>\n

\r\nPublic Class frmLogin\r\n    Dim strUser As String\r\n    Dim strPassword As String\r\n    Dim intAttempts As String\r\n    Dim booLogin As Boolean\r\n<\/pre>\n

As shown above, you can see that we are going to use two strings (User and Password). We also have an integer (Attempts) and a Boolean (Login). What we are going to do is make a login page that after trying to log in three times pops a message saying that the maximum number of attempts has been reached and the application will now be terminated. If you log in correctly you will get a login successful message box. Note: If you are planning on making an application that authenticates people \u2013 this is NOT the method to do that as we are going to put the password directly in the code. This way anyone can open the .exe in a resource editor\/hex editor and plainly see the password. <\/p>\n

Next, we need to set the string equal to what we want the password and username to be. We will set this on form load. To get the form load code section double-click on the title bar of the form. For this tutorial we are making the username Mike and the password TeamTutorials (it will be case sensitive).<\/p>\n

\r\n    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\r\n        strPassword = "TeamTutorials"\r\n        strUser = "Mike"\r\n    End Sub\r\n<\/pre>\n

Next we need to lay out our form. I will have two pictures (one for the names of the controls and on for the text that are in the controls):<\/p>\n

Strings_Booleans_and_Integers_01<\/p>\n

Now, let\u2019s start adding out code to the form. Double click on the exit button and add the word End to it. This will kill the application when clicked:<\/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

The next thing we need to put in the code is the checking to see if the username and password entered match what we have. We also want to check to make sure the password box is not blank, and if it is alert the user to it. The username can be blank. <\/p>\n

(Please note that I did this in a way that will make you understand how the If statements work in order to get this tutorial. There are many easier ways to achieve this and there are parts of the code that are un-needed. For example, the Boolean is completely useless and does not need to be there, but I wanted you to see its function.)<\/p>\n

\r\n    Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click\r\n        If txtPassword.Text <> "" Then\r\n            If txtUser.Text = strUser Then\r\n                If txtPassword.Text = strPassword Then\r\n                    booLogin = True\r\n                    If booLogin = True Then\r\n                        MsgBox("Succesfully logged in!", MsgBoxStyle.Information, "Welcome")\r\n                        txtPassword.Text = ""\r\n                        txtUser.Text = ""\r\n                        booLogin = False\r\n                    End If\r\n                Else\r\n                    MsgBox("Password is incorrect, please try again.", MsgBoxStyle.Information, "Incorrect Password!")\r\n                    txtPassword.Text = ""\r\n                End If\r\n            Else\r\n                MsgBox("Username is incorrect, plaese try again.", MsgBoxStyle.Information, "Incorrect Username!")\r\n\r\n            End If\r\n        Else\r\n            MsgBox("Please enter a password!", MsgBoxStyle.Information, "Alert!")\r\n        End If\r\n    End Sub\r\n<\/pre>\n

The code above is actually very simple. Just the fact that there are if statement in if statements (nested if statements) makes it confusing to look at. Here is a picture to help line up the if statements and make it a little easier to understand:<\/p>\n

Strings_Booleans_and_Integers_02<\/p>\n

We simply check to make sure the password text box is not empty, then we check to make sure the user name equals what it should and if it doesn\u2019t give a message box. Then we check to make sure the password equals or preset one and if not give a message box. If it matches it sets our Boolean equal to false. Then we check to see if the Boolean is equal to true and give the welcome message box if it is. After we log in successfully we blank out both of the text boxes and set the Boolean back to false.<\/p>\n

One last thing, since this is a password field, we should make it so that the password comes up as asterisks as the user types it so that other around wont be able to read what the password is. To do this we simply need to click on the text box that we want to do that to and locate the following property in the property toolbar:<\/p>\n

Strings_Booleans_and_Integers_03<\/p>\n

and type the asterisk. You can use whatever you would like instead of the asterisk as well. Now let\u2019s test everything. To start run the application and type the username with no password in the box and you should get the following window: <\/p>\n

Strings_Booleans_and_Integers_04<\/p>\n

Next type anything in for the password but type the user name incorrectly and you should receive the following box:<\/p>\n

Strings_Booleans_and_Integers_05<\/p>\n

Next type the username correctly but type the password in correctly and you should get this box instead:<\/p>\n

Strings_Booleans_and_Integers_06<\/p>\n

Finally, type the username and password both in correctly and you should receive our final box that says this all was a success:<\/p>\n

Strings_Booleans_and_Integers_07<\/p>\n

This concludes this tutorial. I hope this makes If statements something you can easily understand now. I hope you were able to follow it and thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"

This is the third tutorial in the series that will give you a very basic understanding of VB and allow you to learn more advanced features. We are going to be going over strings, integers, and boolean variables.<\/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 with Strings Booleans and Integers<\/title>\n<meta name=\"description\" content=\"This is the third tutorial in the series that will give you a very basic understanding of VB and allow you to learn more advanced features. We are going to be going over strings, integers, and boolean variables.\" \/>\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-code-strings-booleans-and-integers\" \/>\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-code-strings-booleans-and-integers#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/strings_booleans_and_integers_01.jpg\",\"contentUrl\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/strings_booleans_and_integers_01.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#webpage\",\"url\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers\",\"name\":\"Simple VB Code with Strings Booleans and Integers\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#primaryimage\"},\"datePublished\":\"2008-02-11T05:00:19+00:00\",\"dateModified\":\"2008-02-09T02:42:21+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"This is the third tutorial in the series that will give you a very basic understanding of VB and allow you to learn more advanced features. We are going to be going over strings, integers, and boolean variables.\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple VB Code with Strings Booleans and Integers\"}]},{\"@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 with Strings Booleans and Integers","description":"This is the third tutorial in the series that will give you a very basic understanding of VB and allow you to learn more advanced features. We are going to be going over strings, integers, and boolean variables.","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-code-strings-booleans-and-integers","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-code-strings-booleans-and-integers#primaryimage","inLanguage":"en-US","url":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/strings_booleans_and_integers_01.jpg","contentUrl":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/02\/strings_booleans_and_integers_01.jpg"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#webpage","url":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers","name":"Simple VB Code with Strings Booleans and Integers","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#primaryimage"},"datePublished":"2008-02-11T05:00:19+00:00","dateModified":"2008-02-09T02:42:21+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"This is the third tutorial in the series that will give you a very basic understanding of VB and allow you to learn more advanced features. We are going to be going over strings, integers, and boolean variables.","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/programming-tutorials\/simple-vb-code-strings-booleans-and-integers#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Simple VB Code with Strings Booleans and Integers"}]},{"@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\/1339"}],"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=1339"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1339\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}