{"id":2283,"date":"2013-02-08T15:17:03","date_gmt":"2013-02-08T07:17:03","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2017-11-10T12:38:21","modified_gmt":"2017-11-10T04:38:21","slug":"visual-basic-2012-lesson-6-managing-data","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-6-managing-data\/","title":{"rendered":"Visual Basic 2012 Lesson 6- Managing Data"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-5-writing-the-code\/\">[Lesson 5] <\/a>&lt;&lt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\"> [CONTENTS]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-7-mathematical-operations\/\">[Lesson 7]<\/a><\/strong><\/h4>\n<p>Visual Basic programming often involves manipulation of all sorts of data. Among them, some can be calculated while others are in the form of text, date, time and more. Visual Basic 2012 divides data into different types so that it is easier for the programmers to manage them.<\/p>\n<h3>6.1 Visual Basic 2012 Data Types<\/h3>\n<p>Visual Basic 2012 classifies data into two major data types,\u00a0 the numeric data type and the non-numeric data type.<\/p>\n<h4>6.1.1 Numeric Data Types<\/h4>\n<p>Numeric data types are types of data that consist of numbers that can be computed mathematically. Some examples of numeric data types are examination marks, height, weight, the price of goods, monthly bills, fees and etc.<\/p>\n<p>In Visual Basic 2012, numeric data are classified into seven types, depending on the range of values they can store. Calculations that do not require precision can use Integer or the Long integer in the computation. On the other hand, programs that require high precision calculation need to use Single and Double precision data types which also called floating point numbers. In addition, for currency calculation, you can use the currency data types. Lastly, if even more precision is required in performing a calculation, we can use the decimal data types. These data types are summarized in Table 6.1<\/p>\n<h4>Table 6.1<\/h4>\n<div style=\"overflow-x:auto;\">\n<table>\n<tbody>\n<tr>\n<th witdh=\"18%\">Type<\/th>\n<th width=\"20%\">Storage<\/th>\n<th width=\"62%\">Range of Values<\/th>\n<\/tr>\n<tr>\n<td>Byte<\/td>\n<td>1 byte<\/td>\n<td>0 to 255<\/td>\n<\/tr>\n<tr>\n<td>Integer<\/td>\n<td>2 bytes<\/td>\n<td>-32,768 to 32,767<\/td>\n<\/tr>\n<tr>\n<td>Long<\/td>\n<td>4 bytes<\/td>\n<td>-2,147,483,648 to -2,147,483,648 <\/td>\n<\/tr>\n<tr>\n<td>Single<\/td>\n<td>4 bytes<\/td>\n<td>-3.402823E+38 to -1.401298E-45 for negative values<br \/>\n1.401298E-45 to 3.402823E+38 for positive values\n<\/td>\n<\/tr>\n<tr>\n<td>Double<\/td>\n<td>8 bytes<\/td>\n<td>-1.79769313486232E+308 to -4.94065645841247E-324 for negative values<br \/>\n4.94065645841247E-324 to 1.79769313486232E+308 for positive values\n<\/td>\n<\/tr>\n<tr>\n<td>Variant(numeric)<\/td>\n<td>16 bytes<\/td>\n<td>Any value as large as double<\/td>\n<\/tr>\n<tr>\n<td>Currency<\/td>\n<td>8 bytes<\/td>\n<td>-922,337,203,685,477.5808 to 922,337,203,685,477.5807<\/td>\n<\/tr>\n<tr>\n<td>Decimal<\/td>\n<td>12 bytes<\/td>\n<td>+\/-79,228,162,514,264,337,593,543,950,335 if no decimal<br \/>\n+-7.9228162514264337593543950335\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4>6.1.2 Non-numeric Data Types<\/h4>\n<p>Non-numeric data types are data that cannot be manipulated mathematically using standard arithmetic operators. These types of data comprise text or string data types, the Date data types, the Boolean data types, the Object data type and the Variant data type. We can summarise the data types as shown in Table 6.2<\/p>\n<h4>Table 6.2 Non-numeric Data<\/h4>\n<div style=\"overflow-x:auto;\">\n<table>\n<tbody>\n<tr>\n<th witdh=\"24%\">Data Type<\/th>\n<th width=\"25%\">Storage<\/th>\n<th width=\"51%\">Range of Values<\/th>\n<\/tr>\n<tr>\n<td>String(fixed length)<\/td>\n<td>Length of String<\/td>\n<td>1 to 65,400 characters<\/td>\n<\/tr>\n<tr>\n<td>String(variable-length)<\/td>\n<td>Length +10 bytes<\/td>\n<td>0 to 2 billion characters<\/td>\n<\/tr>\n<tr>\n<td>Date<\/td>\n<td>8 bytes<\/td>\n<td>January 1,100 to December 3,9999 <\/td>\n<\/tr>\n<tr>\n<td>Boolean<\/td>\n<td>2 bytes<\/td>\n<td>True or False\n<\/td>\n<\/tr>\n<tr>\n<td>Object<\/td>\n<td>4 bytes<\/td>\n<td>Any embedded object\n<\/td>\n<\/tr>\n<tr>\n<td>Varient(text)<\/td>\n<td>Length+22 bytes<\/td>\n<td>Same as variable-length string<br \/>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\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=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4>6.1.3 Suffixes for Literals<\/h4>\n<p>Literals are values that you assign to a data. Usually, we\u00a0 add a suffix behind a literal so that Visual Basic 2012 can handle the calculation more accurately. For example, we can use num=1.3089# for a Double type data. Some of the suffixes are displayed in Table 6.3.<\/p>\n<h4>Table 6.3<\/h4>\n<div style=\"overflow-x:auto;\">\n<table style=\"width:80%;\" align=\"center\">\n<tbody>\n<tr>\n<th style=\"text-align:center\">Suffix<\/th>\n<th style=\"text-align:center\">Data Type<\/th>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">&#038;<\/td>\n<td style=\"text-align:center\">Long<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">!<\/td>\n<td style=\"text-align:center\">Single<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">#<\/td>\n<td style=\"text-align:center\">Double<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:center\">@<\/td>\n<td style=\"text-align:center\">Currency<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In addition, we need to enclose string literals within two quotations and we enclose date and time literals within two # sign. Strings can contain any characters, including numbers. The following are few examples:<\/p>\n<pre style=\"font-size: 110%;\">memberName=\"Turban, John.\"\r\nTelNumber=\"1800-900-888-777\"\r\nLastDay=#31-Dec-00#\r\nExpTime=#12:00 am#\r\n<\/pre>\n<h3>6.2 Managing Variables<\/h3>\n<p>A Variable is like a mailbox in the post office as the content of the variable changes\u00a0every now and then, just like the mailbox.\u00a0 In Visual Basic 2012, variables are areas allocated by the computer memory to hold data. In addition,\u00a0 each variable must be given a name. To name a variable in Visual Basic 2012, you have to follow a set of rules.<\/p>\n<h4>6.2.1 Variable Names<\/h4>\n<p>The following are the rules when naming the variables in Visual Basic 2012<\/p>\n<ul>\n<li>It must be less than 255 characters<\/li>\n<li>No spacing is allowed<\/li>\n<li>It must not begin with a number<\/li>\n<li>Period is not permitted<\/li>\n<\/ul>\n<p>Some examples of valid and invalid variable names are displayed in Table 6.4<\/p>\n<h4>Table 6.4<\/h4>\n<div style=\"overflow-x:auto;\">\n<table>\n<tbody>\n<tr>\n<th>Valid Name<\/th>\n<th>Invalid Name<\/th>\n<\/tr>\n<tr>\n<td>My_Car<\/td>\n<td>My.Car<\/td>\n<\/tr>\n<tr>\n<td>ThisYear<\/td>\n<td>1NewBoy<\/td>\n<\/tr>\n<tr>\n<td>Long_Name_Can_Be_USED<\/td>\n<td>He&#038;HisFather   *&#038; Not allowed<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h4>6.2.2 Declaring Variables<\/h4>\n<p>In Visual Basic 2012, we need to declare a variable by assigning a name and a relevant data type before using it. If you fail to do so, the program may encounter an error.\u00a0 Usually, we declare the variables in the general section of the code window using the Dim statement.\u00a0The syntax\u00a0 to declare a varibale is as follows:<\/p>\n<p>Dim Variable Name As Data Type<\/p>\n<h4>Example 6.1<\/h4>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\r\nDim password As String\r\nDim yourName As String\r\nDim firstnum As Integer\r\nDim secondnum As Integer\r\nDim total As Integer\r\nDim doDate As Date\r\nEnd Sub\r\n<\/pre>\n<p>You may also combine them in one line, separating each variable with a comma, as follows:<\/p>\n<pre style=\"font-size: 110%;\">Dim password As String, yourName As String, firstnum As Integer,.............\r\n<\/pre>\n<p>For string declaration, there are two possible forms, one for the variable-length string and another for the fixed-length string. For the variable-length string, just use the same syntax as example 6.1 above. However, for the fixed-length string, you have to use the syntax \u00a0as shown below:<\/p>\n<pre style=\"font-size: 110%;\">Dim VariableName as String * n, where n defines the number of characters the string can hold.\r\n<\/pre>\n<h4>Example 6.2:<\/h4>\n<p>Dim yourName as String * 10<\/p>\n<p>yourName can hold no more than 10 Characters.<\/p>\n<h4>6.2.3 Assigning Values to Variables<\/h4>\n<p>After declaring various variables using the Dim statements, we can assign values to those variables. The syntax of an assignment is<\/p>\n<p>Variable=Expression<\/p>\n<p>The variable can be a declared variable or a control property value. The expression could be a mathematical expression, a number, a string, a Boolean value (true or false) and etc. The following are some examples:<\/p>\n<pre style=\"font-size: 110%;\">firstNumber=100\r\nsecondNumber=firstNumber-99\r\nuserName=\"John Lyan\"\r\nuserpass.Text = password\r\nLabel1.Visible = True\r\nCommand1.Visible = false\r\nLabel4.Caption = textbox1.Text\r\nThirdNumber = Val(usernum1.Text)\r\ntotal = firstNumber + secondNumber+ThirdNumber\r\n<\/pre>\n<h3>6.3 Constants<\/h3>\n<p>Constants are different from variables in the sense that their values do not change during the running of the program.<\/p>\n<p><strong>6.3.1 Declaring a Constant<\/strong><\/p>\n<p>The syntax to declare a constant is<\/p>\n<pre style=\"font-size: 110%;\">Const Constant Name As Data Type = Value\r\n<\/pre>\n<h4>Example 6.3<\/h4>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\r\n Const Pi As Single=3.142\r\n Const Temp As Single=37\r\n Const Score As Single=100\r\nEnd Sub\r\n<\/pre>\n<p><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=\"6598395509\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-5-writing-the-code\/\">[Lesson 5] <\/a>&lt;&lt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\"> [CONTENTS]<\/a> &gt;&gt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-7-mathematical-operations\/\">[Lesson 7]<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 5] &lt;&lt; [CONTENTS] &gt;&gt; [Lesson 7] Visual Basic programming often involves manipulation of all sorts of data. Among them, some can be calculated while others are in the form of text, date, time and more. Visual Basic 2012 divides data into different types so that it is easier for the programmers to manage them. &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-6-managing-data\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2012 Lesson 6- Managing Data<\/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-2283","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 2012 Lesson 6- Managing Data in Visual Basic 2012<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2012 lesson shows you how to manage data in Visual Basic 2012\" \/>\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\/vb2012\/vb2012_lesson6.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2012 Lesson 6- Managing Data in Visual Basic 2012\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2012 lesson shows you how to manage data in Visual Basic 2012\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.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=\"2017-11-10T04:38:21+00:00\" \/>\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=\"5 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-2012-lesson-6-managing-data\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html\",\"name\":\"Visual Basic 2012 Lesson 6- Managing Data in Visual Basic 2012\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"datePublished\":\"2013-02-08T07:17:03+00:00\",\"dateModified\":\"2017-11-10T04:38:21+00:00\",\"description\":\"This Visual Basic 2012 lesson shows you how to manage data in Visual Basic 2012\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2012 Lesson 6- Managing Data\"}]},{\"@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 2012 Lesson 6- Managing Data in Visual Basic 2012","description":"This Visual Basic 2012 lesson shows you how to manage data in Visual Basic 2012","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\/vb2012\/vb2012_lesson6.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2012 Lesson 6- Managing Data in Visual Basic 2012","og_description":"This Visual Basic 2012 lesson shows you how to manage data in Visual Basic 2012","og_url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.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":"2017-11-10T04:38:21+00:00","twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-6-managing-data\/","url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html","name":"Visual Basic 2012 Lesson 6- Managing Data in Visual Basic 2012","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"datePublished":"2013-02-08T07:17:03+00:00","dateModified":"2017-11-10T04:38:21+00:00","description":"This Visual Basic 2012 lesson shows you how to manage data in Visual Basic 2012","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson6.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2012 Lesson 6- Managing Data"}]},{"@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\/2283","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=2283"}],"version-history":[{"count":54,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2283\/revisions"}],"predecessor-version":[{"id":12444,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2283\/revisions\/12444"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=2283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=2283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}