{"id":1032,"date":"2012-04-05T17:11:09","date_gmt":"2012-04-05T09:11:09","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2018-06-24T18:53:43","modified_gmt":"2018-06-24T10:53:43","slug":"visual-basic-2010-lesson-4","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-4\/","title":{"rendered":"Visual Basic 2010 Lesson 4 -Object Oriented Programming"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-3\/\">[Lesson 3]<\/a> &lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-5\/\"> [Lesson 5]<\/a><\/strong><\/h4>\n<p>Visual Basic 2010 is very much similar to Visual Basic 6 in terms of syntaxes and program structure, but their underlying concepts are very different. The main difference is that VB2010 is an object-oriented Programming Language under the .NET framework. Although VB6 may have some OOP capabilities, it is not an object-oriented programming language.<\/p>\n<p>VB6 is not an object-oriented programming language because it does not have inheritance capabilities. On the other hand, VB2010 is an Object Oriented Programming Language, just like other OOP such as C++ and Java. It is different from VB6 because it focuses more on the data itself while VB6 focuses more on the actions. VB6 is known as a procedural or functional programming language. Some other procedural programming languages are C, Pascal, and Fortran.<\/p>\n<p>An object-oriented programming language has three core technologies namely <strong>encapsulation, inheritanc<\/strong>e and <strong>polymorphism<\/strong>. These three concepts are explained below:<\/p>\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=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3><span style=\"color: #000000;\">4.1 Encapsulation<\/span><\/h3>\n<p>Encapsulation refers to the creation of self-contained modules that bind processing functions to the data. These user-defined data types are called classes. Each class contains data as well as a set of methods which manipulate the data. The data components of a class are called instance variables and one instance of a class is an object. For example, in a library system, a class could be a member, and John and Sharon could be two instances (two objects) of the library class.<\/p>\n<h3><span style=\"color: #000000;\">4.2 Inheritance <\/span><\/h3>\n<p>In object-oriented programming, classes are created according to their hierarchies, and inheritance allows the structure and methods in one class to be passed down the hierarchy to another class. That means less programming is required when adding functions to complex systems, therefore save time and effort. If we add a step at the bottom of a hierarchy, we only need to add the processing and data associated with that unique step. Everything else about that step is inherited. The ability to reuse existing objects is a major advantage of object-oriented programming.<\/p>\n<h3><span style=\"color: #000000;\">4.3 Polymorphism<\/span><\/h3>\n<p>Object-oriented programming allows procedures about objects to be created whose exact type is not known until runtime. For example, a screen cursor may change its shape from an arrow to a line depending on the program mode. The routine to move the cursor on the screen in response to mouse movement would be written for &#8220;cursor,&#8221; and polymorphism allows that cursor to take on whatever shape is required at runtime. It also allows new shapes to be easily integrated.<\/p>\n<h3>4.4 Class<\/h3>\n<p>Visual Basic 2010 allows users to write programs that break down into modules. These modules represent the real-world objects known as classes or types. An object can be created out of a class known as an instance of the class. A class can also comprise subclass. For example, the apple tree is a subclass of the plant class and the apple in your backyard is an instance of the apple tree class. Another example is student class is a subclass of the human class while your son John is an instance of the student class.<\/p>\n<p>A class consists of data members as well as methods. In VB2010, the program structure to define a Human class can be written as follows:<\/p>\n<pre style=\"font-size: 110%; width: auto;\">Public Class Human\r\n<span style=\"color: #008000;\"> 'Data Members<\/span>\r\nPrivate Name As String\r\nPrivate Birthdate As String\r\nPrivate Gender As String\r\nPrivate Age As Integer\r\n<span style=\"color: #008000;\"> 'Methods<\/span>\r\n Overridable Sub ShowInfo( )\r\n MessageBox.Show(Name)\r\n MessageBox.Show(Birthdate)\r\n MessageBox.Show(Gender)\r\n MessageBox.Show(Age)\r\nEnd Sub\r\nEnd Class\r\n<\/pre>\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=\"1723562988\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>Let&#8217;s look at one example of how to create a class. The following example shows you how to create a class that can calculate your BMI (Body Mass Index).<\/p>\n<p>To create a class, start VB2010 as usual and choose Windows Applications. In the VB2010 IDE, click on Project on the menu bar and select Add Class, the Add New Item dialog appears, as shown in the Figure 4.1 below:<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2357\" title=\"AddClass\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\" alt=\"\" width=\"819\" height=\"480\" \/><\/a><\/p>\n<p><strong>Figure 4.1<\/strong><\/p>\n<p>The default class Class1.vb will appear as a new tab in a code window. Rename the class as MyClass.vb. Rename the form as MyFirstClass.vb.<\/p>\n<p>Now, in the MyClass.vb window, enter the follow code<\/p>\n<pre style=\"font-size: 110%; width: auto;\">Public Function BMI(ByVal height As Single, ByVal weight As Single)\r\n BMI = Format((weight) \/ (height ^ 2), \"0.00\")\r\nEnd Function\r\n<\/pre>\n<p>Now you have created a class (an object) called MyClass with a method known as BMI.<\/p>\n<p>In order to use the BMI class, \u00a0insert a button into the form and click on the button to enter the following code:<\/p>\n<pre style=\"font-size: 110%; width: auto;\">Private Sub BtnCalBmi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalBmi.Click \r\nDim MyObject As Object\r\nDim h, w As Single \r\n MyObject = New MyClass1() \r\n h = InputBox(\"What is your height in meter\") \r\n w = InputBox(\"What is your weight in kg\")\r\n MessageBox.Show(MyObject.BMI(h, w))\r\nEnd Sub\r\n<\/pre>\n<p>When you run this program and click the button, the user can enter his or her height and weight\u00a0subsequently into two input boxes and the value of BMI will be shown in a pop-up message box.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- vb2010 tutorial matched --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"8075128701\" data-ad-format=\"autorelaxed\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-3\/\">[Lesson 3]<\/a> &lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-tutorial\/\">[CONTENTS]<\/a> &gt;&gt;<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-5\/\"> [Lesson 5]<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 3] &lt;&lt;\u00a0[CONTENTS] &gt;&gt; [Lesson 5] Visual Basic 2010 is very much similar to Visual Basic 6 in terms of syntaxes and program structure, but their underlying concepts are very different. The main difference is that VB2010 is an object-oriented Programming Language under the .NET framework. Although VB6 may have some OOP capabilities, it is &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2010-lesson-4\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2010 Lesson 4 -Object Oriented Programming<\/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-1032","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 2010 Lesson 4 -Object Oriented Programming - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This Visual Basic 2010 lesson explains the concepts of Object Oriented Programming in Visual Basic 2010\" \/>\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\/vb2010\/vb2010_Lesson4.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2010 Lesson 4 -Object Oriented Programming - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This Visual Basic 2010 lesson explains the concepts of Object Oriented Programming in Visual Basic 2010\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.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-24T10:53:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.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-2010-lesson-4\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html\",\"name\":\"Visual Basic 2010 Lesson 4 -Object Oriented Programming - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\",\"datePublished\":\"2012-04-05T09:11:09+00:00\",\"dateModified\":\"2018-06-24T10:53:43+00:00\",\"description\":\"This Visual Basic 2010 lesson explains the concepts of Object Oriented Programming in Visual Basic 2010\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg\",\"width\":\"819\",\"height\":\"480\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2010 Lesson 4 -Object Oriented Programming\"}]},{\"@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 2010 Lesson 4 -Object Oriented Programming - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This Visual Basic 2010 lesson explains the concepts of Object Oriented Programming in Visual Basic 2010","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\/vb2010\/vb2010_Lesson4.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2010 Lesson 4 -Object Oriented Programming - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This Visual Basic 2010 lesson explains the concepts of Object Oriented Programming in Visual Basic 2010","og_url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.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-24T10:53:43+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.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-2010-lesson-4\/","url":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html","name":"Visual Basic 2010 Lesson 4 -Object Oriented Programming - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg","datePublished":"2012-04-05T09:11:09+00:00","dateModified":"2018-06-24T10:53:43+00:00","description":"This Visual Basic 2010 lesson explains the concepts of Object Oriented Programming in Visual Basic 2010","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2012\/04\/AddClass.jpg","width":"819","height":"480"},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2010\/vb2010_Lesson4.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2010 Lesson 4 -Object Oriented Programming"}]},{"@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\/1032","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=1032"}],"version-history":[{"count":64,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1032\/revisions"}],"predecessor-version":[{"id":13127,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1032\/revisions\/13127"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}