Object Orientation with VB using Visual Studio

Download the Visual Studio 2010 solutions for this tutorial. To open each project
double-click on the solution file which is located in the solution folder.

List of Contents

Lab 1 – Classes and Objects
Lab 2 – Inside Methods
Lab 3 – Debugging
Lab 4 – Inheritance
Lab 5 – Operator Overloading
Lab 6 – Structs
Lab 7 – Interfaces

Lab 1 – Classes and Objects

  1. Create a class library project called DateTest:
    • Open Visual Studio 2012 and select the File menu, then New, then Project.
    • Make sure (if you are using the Professional Version) that Visual Basic and then Windows is selected at the left of the screen under Installed Templates.
    • Name the project and the solution: DateTest.
    • Browse to the place on your computer where you wish Visual Studio to create the directory for your solution (keeping the tick box selected).
    • Select Class Library and then click OK.
    • Rename the Class1.vb file MyDate.vb.
  2. Define a class called Date in the project. Add a constructor and also define a method called DisplayDate() in the class.
  3. Add a second overloaded constructor to the Date class. This will initiate the month and days fields only in the class, the year will default to the current year.
  4. To add three properties for the year, month, day, to the Date class.
    Public Class MyDate
       'Properties
       Public Property Year As Integer = 2011
       Public Property Month As String
       Public Property Day As Integer
       'Constructer
       Public Sub New(ByVal intYear As Integer, ByVal strMonth As _
       String, ByVal intDay As Integer)
          Year = intYear
          Month = strMonth
          Day = intDay
       End Sub
       'Overloaded Constructer
       Public Sub New(ByVal strMonth As String, ByVal intDay As _
       Integer)
          Month = strMonth
          Day = intDay
       End Sub
       'A method to display the date
       Public Sub DisplayDate()
          Console.WriteLine(String.Format("The Date is: {0} {1} {2}",       Day.ToString(), Month, Year.ToString()))
       End Sub
    End Class
  5. View code file.
  6. Add a new console application project called Tester to the solution:
    • Right-click on the project name: DateTest.
    • Select Add.
    • Select New Project.
    • Make sure the location of the project is within the solution folder: DateTest.
  7. Add a reference in the console application to the Date class library:
    • Go to the Project Menu or right-click on the project name.
    • Select Add Reference.
    • Select the Browse tab and navigate to and select the MyDate.dll file within the bin/Debug folder within the Date folder (which contains your solution).
    • Type Imports DateTest into the beginning of the Module1.vb code file. DateTest is the namespace and solution name.
  8. Create an oDate object from the Date class, in the console application.
  9. 'Create an instance of the date object
    Dim oDate As MyDate = New MyDate(1968, "July", 18)
  10. Test the methods and properties of the oDate object.
    'Change the property values in oDate
    oDate.Year = 1977
    oDate.Month = "October"
    oDate.Day = 25
    'And then display them
    oDate.DisplayDate()
  11. View code file.

Lab 2 – Inside Methods

  1. Add a GetDate method with ref parameters for year, month, day, to the Date class created in Lab 1.
  2. Add code to GetDate() to return valid parameters for year, month, day.
    'A method to return the date
    Public Function GetDate(ByRef intDay As Integer, ByRef strMonth As String, ByRef intYear As Integer) As String
       intDay = Day
       strMonth = Month
       intYear = Year
       Return (String.Format("The Date is: {0} {1} {2}", Day.ToString(),
       Month, Year.ToString()))
    End Function
  3. Test the method by calling it, then displaying the returned year, month, day values.
    Dim intDay As Integer = 0
    Dim strMonth As String = ""
    Dim intYear As Integer = 0
    'Display the values in the first date object using the GetDate method
    Console.WriteLine(oDate.GetDate(intDay, strMonth, intYear))
    'Show how the GetDate method has changed the local variables by using ref
    Console.WriteLine("The Date is: " + intDay.ToString() + " " + strMonth + " " + intYear.ToString())
  4. View code file.

Lab 3 – Debugging

For all remaining lab exercises throughout this course, examine and debug your code by:

  1. Setting breakpoints in lines of code by clicking in the margin. When run the code will pause at the first beakpoint.
  2. Stepping through lines of code when a breakpoint is hit by pressing F11. The code will execute line by line. Hover the mouse pointer over a variable to show its current value.
  3. Using these debug windows to understand and fix code:
    • Watch
    • Locals
    • Immediate
    • Call Stack
  4. Debug windows are available only when the code is paused in break mode. Use the Debug>Windows command to show them.

Lab 4 – Inheritance

  1. Open Visual Studio and create a new Visual Basic Console Application project.
  2. Define a class called Control, with a constructor and a DrawControl() method.
  3. Add only a Console.Write() statement to the DrawControl() method.
    Public Class Control
       'These members are private and thus invisible
       'to derived class methods we'll examine this
       'later in the chapter
       Private top As Integer
       Private left As Integer
       'Constructor takes two integers to
       'fix location on the console
       Public Sub New(ByVal top As Integer, ByVal left As Integer)
          Me.top = top
          Me.left = left
       End Sub
       'Simulates drawing the control
       Public Overridable Sub DrawControl()
          Console.WriteLine("Drawing Control at {0}, {1}", top, left)
       End Sub
    End Class
  4. Create a class called TextBox that inherits from DrawControl, but replaces the DrawWindow method. Change the Console.Write() statement to show a different message.
    'TextBox derives from Control
    Public Class TextBox
    Inherits Control
    'A new version (note keyword) because in the
    'derived method we change the behavior
    Public Overrides Sub DrawControl()
       MyBase.DrawControl() 'Invoke the base method
       Console.WriteLine("Writing string to the textbox: {0}", mTextBoxContents)
    End Sub
  5. Test the TextBox class by calling its DrawControl method.
    'Create a base instance
    Dim contr As Control = New Control(5, 10)
    contr.DrawControl()
    'Create a derived instance
    Dim txt As TextBox = New TextBox(20, 30, "Hello world")
    txt.DrawControl()
  6. View code file.

Lab 5 – Operator Overloading

  1. Open Visual Studio and create a new Visual Basic Console Application project.
  2. Define a class called Fraction, with a constructor and a ToString() method.
  3. Add two integer field to the class to hold the numerator and the denominator.
  4. Add a constructor to initialize the numerator and the denominator.
  5. Create a subtraction operator (-) using operator overloading.
  6. Test the operator.
  7.  Your code may look something like this:

    Public Class Fraction
       Private numerator As Integer
       Private denominator As Integer
       'Create a fraction by passing in the numerator
       'and denominator
       Public Sub New(ByVal numerator As Integer, ByVal denominator As _
       Integer)
          Me.numerator = numerator
          Me.denominator = denominator
       End Sub
       'Overloaded operator- takes two fractions
       'and returns their sum
       Public Shared Operator -(ByVal lhs As Fraction, ByVal rhs As _
       Fraction) As Fraction
          Dim firstProduct As Integer = 0
          Dim secondProduct As Integer = 0
          'Like fractions (shared denominator) can be added
          'by adding their numerators
          If (lhs.denominator = rhs.denominator) Then
             Return New Fraction(lhs.numerator - rhs.numerator, _
             lhs.denominator)
          End If
          'Simplistic solution for unlike fractions
          firstProduct = (lhs.numerator * rhs.denominator) - _
          (rhs.numerator * lhs.denominator)
          secondProduct = lhs.denominator * rhs.denominator
          Return New Fraction(firstProduct, secondProduct)
       End Operator
       'Return a string representation of the fraction
       Public Overrides Function ToString() As String
          Dim s As String = numerator.ToString() + "/" +
          denominator.ToString()
          Return s
       End Function
    End Class
    Dim f1 As Fraction = New Fraction(1, 2)
    Console.WriteLine("f1: {0}", f1.ToString())
    Dim f2 As Fraction = New Fraction(1, 6)
    Console.WriteLine("f2: {0}", f2.ToString())
    'Test the overloaded minus operator
    Dim f3 As Fraction = f1 - f2
    Console.WriteLine("f1 - f2 = f3: {0}", f3.ToString())
  8. View code file.

Lab 6 – Structs

  1. Open Visual Studio and create a new Visual Basic Console Application project.
  2. Define a structure called Colour, with a constructor and a ToString() method.
    'Declare a struct named Colour
    Public Structure Colour
       'The struct has private data
       Private redValue As Integer
       Private greenValue As Integer
       Private blueValue As Integer
       'Constructor
       Public Sub New(ByVal rVal As Integer, ByVal gVal As Integer, _
       ByVal bVal As Integer)
          redValue = rVal
          greenValue = gVal
          blueValue = bVal
       End Sub
       'Display the Struct as a String
       Public Overrides Function ToString() As String
          Return String.Format("{0}, {1}, {2}", redValue, greenValue,
          blueValue)
       End Function
    End Structure
  3. Add three integer properties to represent the red, green and blue component of the colour.
    'Property
    Public Property Red() As Integer
       Get
       Return redValue
    End Get
    Set(ByVal value As Integer)
       redValue = value
       End Set
    End Property
    Public Property Green() As Integer
       Get
       Return greenValue
    End Get
    Set(ByVal value As Integer)
       greenValue = value
       End Set
    End Property
    Public Property Blue() As Integer
       Get
       Return blueValue
    End Get
    Set(ByVal value As Integer)
       blueValue = value
       End Set
    End Property
  4. Test the structure.
    Public Class Tester
       Public Sub Run()
          'Create an instance of the struct
          Dim colour1 As Colour = New Colour(100, 50, 250)
          'Display the values in the struct
          Console.WriteLine("colour1 colour: {0}", colour1)
          'Invoke the default constructor
          Dim colour2 As Colour = New Colour()
          Console.WriteLine("colour2 colour: {0}", colour2)
          'Pass the struct to a method
          MyMethod(colour1)
          'Redisplay the values in the struct
          Console.WriteLine("colour1 colour: {0}", colour1)
       End Sub
       'Method takes a struct as a parameter
       Public Sub MyMethod(ByVal col As Colour)
          'Modify the values through the properties
          col.Red = 200
          col.Green = 100
          col.Blue = 50
          Console.WriteLine("colour1 colour: {0}", col)
       End Sub
    End Class
    Sub Main()
       Dim t As Tester = New Tester()
       t.Run()
       Console.ReadLine()
    End Sub
  5. View code file.

Lab 7 – Interfaces

  1. Open Visual Studio and create a new Visual Basic Console Application project.
  2. Define an interface called IClient.
    'Define the interface
    Interface IClient
  3. Add properties for name and address details and a method called Order(), to the interface.
    Sub Order(ByVal orderNum As Integer)
    Property Name() As String
    Property Address() As String
  4. Create a second class called Customer that implements all the interface’s members.
    'Create a Customer class that implements the IClient interface
    Public Class Customer
    Implements IClient
       Public Sub New(ByVal s As String)
          Console.WriteLine("Creating a New Customer ID: {0}", s)
       End Sub
       'Implement the Order method
       Public Sub Order(ByVal newOrder As Integer) Implements _
       IClient.Order
          Console.WriteLine("Implementing the Order Method for IClient.
          The Order Number is: {0}", newOrder)
       End Sub
       'Implement the properties
       Public Property Name As String Implements IClient.Name
       Public Property Address As String Implements IClient.Address
    End Class
  5. Test the Customer class.
    Class Tester
       Public Sub Run()
          Dim cust As Customer = New Customer("H56388")
          cust.Name = "Brian Ferry"
          cust.Address = "23 Orange Lane, Clifton, Bristol, BS6 5FH"
          Console.WriteLine("The Name of the Customer is: {0}", _
          cust.Name)
          Console.WriteLine("The Address of the Customer is: {0}",
          cust.Address)
          cust.Order(1234)
       End Sub
    End Class
  6. View code file.

Back to the beginning

Scroll to Top
Share via
Copy link