Classes - Introduction with Form Class

Objectives


Discussion

Summary of Object-Oriented Programming (OOP)

VB as Object Oriented Language

Simple Approach in this Unit

Public Class AgeInfo

    Private iAge as Int16 'Property

    Public Sub New() 'Constructor
       iAge = 0
    End Sub

    Public Sub SetAge(ByVal Age as Int16) 'Method
       if Age > 0 then iAge = Age
    End Sub    

    Public Function GetAge() As Int16 'Method
       Return iAge
    End Function

End Class

Back to top


Demonstration

Throughout this course you have been programming with classes and objects.  However VS has been handling most of the details.  In this demo we will demonstrate some simple concepts of OOP by using a form class and adding properties and methods to it.  In the next lesson we will actually create our own class file.  The purpose of this demo is to relate OOP concepts to something we are already familiar with, the form class.

1.  Create a new project, Unit 10.  Add a second form (Form2).   We will add code to Form2 to demonstrate how to add properties and methods.   We will then use Form1 to create an object of the Form2 class. We have actually been doing this throughout the course; now we are going to focus on the OOP aspects of it.  We are not going to cover events in this unit. 

2.  Open the code window for Form2 .  Your window should look like the following although we have left out the Windows Form Designer Generated Code.

Public Class Form2

   Inherits System.Windows.Forms.Form

End Class

3.  To create a class we must use the Public Class header and End Class statement.  Anything that we put between these two lines will be part of the class.  Notice the Inherits statement.  OOP enables classes to inherit capabilities of other classes.  In this example then, the Form2 class will be able to everything that the generic System.Windows.Forms.Form class can do.  We are not going to cover the concept of inheritance in any more detail.  

4.  Now we will add a property to Form 2 using the Java approach.  Keep in mind that VB provides a Property keyword that enables you to define properties.  However, we will not use this approach.  To add a property we will create a Private variable declared under the Inherits statement and outside of all subs and functions.   We could have made the variable public and then other forms could access it directly.  By making it private we will have to create public subs and functions to access the variable.  The main benefit is that the person who designs the class can control how information goes in and out of the property through the subs and functions.  Generally these subs and functions are called "get" and "set" although they do not have to be.  "Get" means that the user will get the current value of the property and "Set" means that the user will set the value of the property.  Type the following under the Inherits statement in Form2 to create a "property" that will hold a string:

Private sX As String

5.  Since the variable is Private you can not access it from another form.  So we will create public methods to assign (set) and retrieve (get) values of the variable.  You will be able to access these methods from another form since they are public.  In the future you may find that you don't want to let the user set a value of a property.  In that case you would not create the "set" method.  Now add the following code to the Form2 class.

Public Sub setX(ByVal x As String)
   sX = x
End Sub
Public Function getX() As String
   Return sX
End Function

6.  Now we will add a sub that will act as a method.  It is a very simple sub and just intended to demonstrate methods.  Add the following to the class for Form2:

Public Sub DisplayX()
   MessageBox.Show(sX)
End Sub

7.  Now we have finished defining the Form2 class. We will use Form1 to instantiate a Form2 object and experiment with the methods and properties.   Add four buttons to Form1 (btnInstantiate, btnSetX, btnGetX, btnDisplayX ).  Since we are going to need to refer to the same Form2 object in all of the buttons, we need to declare our Form2 object at the top of Form1 so that all buttons can use it.  At the top of the code window for Form1 (under the Inherits statement) add the following to declare a Form2 object:

Private frm2 As Form2

8.  Add the following code to the click event for btnInstantiate:

frm2 = New Form2()

9.  Add the following code to the click event for btnSetX:

frm2.setX(InputBox("Enter value for x"))

10.  Add the following code to the click event for btnGetX:

MessageBox.Show("The value of X is: " & frm2.getX)

11.  Add the following code to the click event for btnDisplayX:

frm2.DisplayX()

12.  Now run and test the program.  Notice what each button is doing and study the code. 

13.  Try the following.  Somewhere in the one of the events in the code window for Form1 type "frm2."  Notice that the public methods are listed but the private variable sX is not.  Then type "frm2.setx(".  Notice that you are prompted for a string.

Back to top
Exercises

1.  Each class has at least one constructor  Public Sub New(...) as shown in the example above in the discussion.  We did not create nor discuss the constructor for Form2 in the Demonstration.  Where is the constructor for Form2? 

2.  Search the Internet for information about object-oriented programming and classes.  Specifically find examples of classes in Java that demonstrate some of the ideas discussed in this lesson.   It is very important that you understand the general idea of classes and the similarities between VB and Java (especially if you expect to take the AP Java course).

Back to top
Links & Help

Keep in mind that VB allows you to use a "property" keyword for setting property values.  We are not following this approach because we are trying to follow the Java approach.  The following references may use the "property" keyword.

Back to top