Create a Class |
Objectives |
Discussion |
Custom Classes
Constructors
Public Sub New([parameters here])
[code goes here]
End Sub
OverLoading
Defining a Class in a Project
Demonstration |
In this demo we will create a Class for a rectangle called MyRectangle. Then we will instantiate objects of this class in another form.
1. Open your Unit 10 project. Add a new class file by selecting Project...Add Class. Name your class MyRectangle.
2. Now open the class file and copy and paste the following code into it.
Private iWidth As Int16
Private iHeight As Int16
Public Sub New()
iWidth = 0
iHeight = 0
End Sub
Public Sub New(ByVal width As Int16, ByVal height As Int16)
iWidth = width
iHeight = height
End Sub
Public Sub New(ByVal WidthandHeight As Int16)
iWidth = WidthandHeight
iHeight = WidthandHeight
End Sub
Public Function getWidth() As Int16
Return iWidth
End Function
Public Sub setWidth(ByVal width As Int16)
If width < 200 then
iWidth = width
Else
iWidth = 0
End If
End Sub
Public Function getHeight() As Int16
Return iHeight
End Function
Public Sub setHeight(ByVal height As Int16)
iHeight = height
End Sub
Public Function calcArea()
Return iHeight * iWidth
End Function
Public Sub DisplayRectangle(ByVal surface As Form, ByVal x As Int16, ByVal y As
Int16)
surface.CreateGraphics.DrawRectangle(New Pen(Color.Red), x, y,
iWidth, iHeight)
End Sub
3. Study the code carefully. Notice that there are three different constructors. Recall that this is an example of overloading. The first contructor assigns values to the width and height of 0. The second constructor allows the user to enter values for the width and height. The last constructor creates a square since it assigns the same value to both the width and height. Notice the Get and Set procedures for assigning and retrieving the properties. In the setWidth method we have included a condition to limit the size of the width. We did this just to show how you can use Set methods to control what goes into the properties. Finally notice the DisplayRectangle method which accepts a Form argument and then uses the built-in DrawRectangle method to draw a rectangle on the Form that is passed in.
4. Now we will use this class in another form. Create a new form and add a button (btnNewRectangle) and a textbox (txtSide ). At the top of the form code window under the Inherits statement add the following code to declare a rectangle object. We are declaring it at the top since we will use it in several different events.
Dim rectX As MyRectangle
5. In the click event for the button add the following code. This code creates an instance of MyRectangle and displays the area of it. We are going to create a rectangle with equal sides so we will call the constructor that accepts the one parameter. Also we will pass the value of the textbox into the constructor.
rectX = New MyRectangle(txtSide.Text)
MessageBox.Show(rectX.calcArea)
6. Now add the following to the MouseDown event for the form. This code will cause the rectangle to be drawn on the form where the user clicks.
rectX.DisplayRectangle(Me, e.X, e.Y)
7. Run the program and experiment.
Exercises |
1. We can create classes for almost anything. Create your own class. Make sure that it has the following:
2. Create a class for a "BullsEye". The class will be used for drawing a "BullsEye" target on a form. The class will be similar to the Rectangle class created in the demonstration. Your BullsEye will consist of two concentric circles (one inside the other with same centers). Your class should provide the following features:
3. Create a program that will use your class and allow the user to create and display BulleEyes.
4. What is "Overloading"?
5. What is a constructor?
Links & Help |