CIS206 - Advanced Visual Basic - Week 3

Objects and Classes


Week 3 begins the work on classes (the model) and objects (one or more instances based on the model)

Four examples for tonight


Example 1

This example deals mostly with the concepts of objects. Controls added to the form at design time can be 'wired' to any subroutine and is not inherently linked to the default design time event handler. Each object is passed to the event handler along with a second parameter that contains specific information related to the specific event being handled. 

Download the example


Example 2

Objects are just like any other variable with respect to their scope. Objects can be local to a subroutine or event handler, or they can be global to the project. There is a specific sequence of events that take place as the objects are created and as they are destroyed. 

A basic class and the interface as viewed by the client, along with the constructor and destructor tracing is demonstrated. The interface consists of the properties and methods as they are exposed by the class. Private variables are not available to the client.  

Download the example


Example 3

This example is a bit more extensive and there are more properties and methods, but also makes use of a Visual Basic module, shared variables and read-only properties. You will see that the class itself is incomplete, but this does not stop the development of any client project as long as the classes public interface does not change.

Download the example


Example 4

Example 4 is the same example as Example 3, except that the projects are separate. The class is compiled to a DLL, and the client makes a reference to the DLL at design time. The example also demonstrates how to merge two projects together into a single solution in order to debug the client and/or the class.

Download the example


Assignment

Build the a class and a client in a single project 

The class should represent a Rectangle object. The rectangle object has the following interface:

Properties and their types

ImageColor  Color  (Write Only)
Height Decimal  
Width Decimal  
Area Decimal (Read Only)
Perimeter  Decimal (Read Only)

  

Methods and their arguments

Constructor None, default to length and width of 10
Constructor Length (Decimal), Width (Decimal)
Image Image (Control)

Here is some code that you can use to implement the Image method

Public Function Image(ByVal pb As Control)

    Dim myPen As System.Drawing.Pen = New System.Drawing.Pen(Color.Aqua)
    Dim myBrush As New System.Drawing.SolidBrush(gcolColor)
    Dim gr As Graphics = pb.CreateGraphics()

    gr.Clear(System.Drawing.SystemColors.Control)
    gr.FillRectangle(myBrush, 1, 1, gdecWidth, gdecHeight)

End Function

gdecWidth and gdecHeight are the private variables behind the Width and Height property procedures

 

In the client form, you need to allow the user to select the color and dimensions of the rectangle. Color may be selected using radio buttons and the dimensions may be adjusted using scroll bars. If you don't want to use radio buttons and scroll bars, that is fine. You may use your preferred method of selection using any controls that you like.

 

Everything in the client is driven using the change to the height or width.

All of this can be done by 'wiring' a single subroutine to the respective change events using AddHandler

The same type of update can be fired when the color changes. 

As we have not discussed the persistence of the class or application generated graphics, place this code in the main forms paint event.
This assumes that your main form is called frmMain and that it contains a picture box called picImage.

Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
                                        Handles MyBase.Paint

    picImage.Refresh()
    cRectangle.Image(picImage)

End Sub

Download the .EXE

This program is due BEFORE class next week as we will review the source at the start of class.


Return to Home Page