Programming language Visual Basic 6 (Chapter VIII - Create Object)

Ever, our programming VB6 Forms by design and write codes to handle Events of the controls on a Form Button or click the Users Listbox. Etc. 

Overall, he is also an effective way to deploy, but if we can enjoy the following benefits more the better:
  1. Reusable code was written before in another project
  2. Easy to identify an error (error) come from
  3. Easy to implement a major project by distribution into many small projects
  4. Easy maintenance
Every time you use the code, if for medical reasons is the ideal cement. That thing is called  reusability .Say it right out,  reusable  really is when you just use  the object code , that code has been  compiled , then, that is completely untouched  source code . Hể because it allows users to edit source code is the opportunity for  bugs to appear, then back to debug again.
The main challenge of deploying a large software project is implemented on time (on time), not irresponsible fiscal (within budget) and ease of maintenance (EASE of maintenance). Want to achieve goals which he will be deployed and how quickly the program are less bugs, easier to maintain.

Assuming you to host a wedding. Imagine how many things to do: from the guest list, invitations, food, cars, photography, film, literature procedures for the ceremony, reception, etc. .. disturbing activities members. If you only have to worry about really do not know how to remember for all. Fortunately, the restaurant will always undertake the printing invitations, art and band members both disturbing activities.Ceremonial procedures, no one gained through your Friday, and I have accepted the invitation to buy gifts, concerned about the reception, vehicles and procedures and rituals. She also will contact the pastor told the ceremony to the church and set the ringer and man. English words have a friend who owns the shop, I take responsibility for hiring people taking pictures, filming. How you organize the wedding date is now reduced to compose a list of the guests, speeches, told about seating and room for the new couple went on their honeymoon.

The reason you feel the responsibility to organize non heavy on restaurants, doctors, and he gained six words themselves take on the stages of trouble. The beauty here is that these people themselves to decide every detail of what needs to be done in their stages. Only when we do, they communicate to your opinion.They like those  bidding  for you. Sure you note that the example these wedding shows in general want to implement major projects we need to ask the contractor for help. Indeed, that is how the administrator has made these works from the past to present.

Now back to programming stories, if only we can organize how to deploy software projects like organizing the wedding mentioned above is good too. Actually, not all software theorists do not even think about it before, but to accomplish things we need to deploy the means and instruments appropriate. Only in the last 15 years, new things become more concrete through sophisticated Operating Systems, particularly Windows, and programming languages like Eiffel, Smalltalk, C + +. Etc.

Object-oriented programming (Object Oriented Programming)

Roughly speaking, object-oriented programming is the design of parts of the software program, called Objects  so that each department can take care of it work like a  contractor -life way. Perhaps you will then ask the  Sub  or  Function  that you wrote to handle every stage of the program can undertake the role of a contractor does not?
Contractors who not only can do the work (Subs and Functions), but also what are all responsible for everything necessary objects (data) to work anymore.
Have a different definition for an Object Object data structure and includes Subs / Functions that work on the data. Usually, when we use Objects rarely supervised them, whereas if you have any problems, then we want them to know the report.

In VB6, the Forms, ActiveX Controls or Objects that are still used all along. Take for example Listbox. A self-management Listbox displays the items inside it. Listbox List1 know how many items are by asking List1.ListCount. Just know what item is selected by asking List1.ListIndex. I add an item to the listbox by calling the AddItem method of List1, .. etc.. Telling fact, Object is an instance of a Class. If the Listbox is a Class List1, List2 is the entity of the Listbox. She is like going from the pan and cake Long Range is the entity's Class Chefs.
Even a form name that is written in VB6 frmMyForm such, it is also a Class. Usually we use straight frmMyForm as follows:
In this case though is actually a Class frmMyForm but is used like an object. Do not if you want, we can create two, three Objects of Class frmMyForm same time as the following examples:
Dim firstForm As frmMyForm 
Dim secondForm As frmMyForm 
Set firstForm = New frmMyForm 
Set secondForm = New frmMyForm 
In the above example we declare firstForm and are the Objects of Class secondFormfrmMyForm. Then we do it (instantiate) the statements by Objects firstForm andsecondForm Set ... = New ...
secondForm firstForm and called the instances of Class frmMyForm. Class-like mold,Objects also like cake made from molds that. Maybe you've noticed since taking twofrom the VB6 Object Class and mixed together. These are not important, as long as youunderstand their meaning.

VB6 has supported  class  that can instantiate the deployment and use of its Objects. A Class in VB6 that contains  data  of its own, with the  Subs  and  Functions  that can be called. Also can Raise Class Events , ie let us know when something happens within it. Like the CommandButton Click Event when the User clicks on button Click Event to Raise it to us in the Sub myCommandButton_Click handle (), for example. Classtrong VB6 does not support Visual components, ie not contain controls such as TextBox, Label. Etc. However, we can take control available outside of class Object and give it to use.

Now let's start writing a Class. Open a new Project type Visual Basic Standard EXE. Then use the Command Menu select  Add Class Module :

Khi Add Class Module dialog hiện ra chọn Class Module và click Open.

You will see an open frame with white and Project Explorer Properties Window. In the Properties window, edit the Name property of the Class of clsBox as below:
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (791x427)
Then type in the code below, or  download the source code of the program , including performances using clsBox Class.

Option Explicit 
Private mX As Integer 
Private mY As Integer 
Private mWidth As Integer 
Private mHeight As Integer 

Public Property Let X(ByVal vValue As Integer) 
   mX = vValue 
End Property 

Public Property Get X() As Integer 
   X = mX 
End Property 

Public Property Let Y(ByVal vValue As Integer) 
   mY = vValue 
End Property 

Public Property Get Y() As Integer 
   Y = mY 
End Property 

Public Property Let Width(ByVal vValue As Integer) 
   mWidth = vValue 
End Property 

Public Property Get Width() As Integer 
   Width = mWidth 
End Property 

Public Property Let Height(ByVal vValue As Integer) 
   mHeight = vValue 
End Property 

Public Property Get Height() As Integer 
   Height = mHeight 
End Property 

Public Sub DrawBox(Canvas As Object) 
   Canvas.Line (mX, mY)-(mX + mWidth, mY + mHeight), , B 
End Sub 

Public Sub ClearBox(Canvas As Object) 
   Canvas.Line (mX, mY)-(mX + mWidth, mY + mHeight), Canvas.BackColor, B 
End Sub 
Class clsBox have 4 Properties: X, Y, Width and Height. I will instantiate a Box fromclsBox. Each Box has coordinates (X, Y) and the width and height (width, height) of it.Actually we can use the Public statement to declare the variables X, Y, Width andHeight. But here we declare them as Private intentional, as the MX, MY, and mHeightmWidth. When we want to change our values, we will use the same code as normalwriting style (eg myBox.X = 80). But when the program was handled Assignmentstatement, it will execute a method (as sub) called Property Let X (vValue). I see here isgán vValue for MX (MX = vValue ie), the private variable of X. Thus this work is no different than modifying a public variable X. However, here we can write code in theProperty Let X to do it also.
Do you remember when designing a label, every time you use the Property Window to edit the font size, backcolor forcolor or she is not only the properties of the label change, but the result of the change is effective immediately, Label that is displayed again with the new value of the property. This is because the method is both Property insurance Label redisplay code.
Conversely, when we use the Object property myBox X, not just read the numbers alone, but also a method to execute the  Property Get X . In summary, our Property for the opportunity to execute a method every user to read or write that variable value.
For example, if we want this control to accept only the new X coordinate value when it is not negative.Property Let X we will correct as follows:

Public Property Let X(ByVal vValue As Integer) 
   If (vValue >= 0) Then 
      mX = vValue 
   End If 
End Property 
Property có thể là Read Only hay Write Only. Nếu muốn một Property là Read Only thì ta không cung cấp Property Let. Nếu muốn một Property là Write Only thì ta không cung cấp Property Get. Ngoài ra nếu làm việc với Object, thay vì Data type thông thường, thì ta phải dùng Property Set, thay vì Property Let.
For example we clsBox a new Property called for objects of class Font stdFont of VB6. In clsBox mFont we declare a variable and write a Private  Property Set Font  as follows:

Private mFont As StdFont 
Public Property Set Font(ByVal newFont As StdFont) 
   Set mFont = newFont 
End Property 
We will use the Font property myBox (under Class clsBox) as follows:

' Declare an object of Class StdFont of VB6
Dim myFont As StdFont 
Set myFont = New StdFont 
myFont.Name = "Arial" 
myFont.Bold = True 
Dim myBox As clsBox 
Set myBox = New clsBox 
Set myBox.Font = myFont  ' Call the Property Set method
Public Class clsBox two Subs, and ClearBox DrawBox. ClearBox well as DrawBoxdraw a box, but it uses the BackColor of the screen (canvas), so as to delete the box isavailable. Therefore, if you wish, you can edit Sub DrawBox a bit to get a draw Optionalcolor as follows:

Public Sub DrawBox(Canvas As Object, Optional fColor As Long) 
   If IsMissing(fColor) Then 
      Canvas.Line (mX, mY)-(mX + mWidth, mY + mHeight), , B 
      Canvas.Line (mX, mY)-(mX + mWidth, mY + mHeight), fColor, B 
   End If 
End Sub 
In the above example, Optional parameter to be Tested by fColor IsMissing function. IffColor the BackColor of the canvas, then we will have the effect of ClearBox.
In the form of the program used to test clsBox, whenever we refer to an object of class clsBox, IDE Intellisense will display the Properties and Subs / Functions of clsBox as in the image below:

In this program, every time I click the button  Draw  a Box is instantiate it, the coordinates X, Y and Width and Height, then drawn on the form. The word  Me  in the code refers to the very form  frmClass .

In order to program more interesting, when the user clicks the button  animate , I will give a red box running from left to right.
When the user clicks the button  Two Boxes  will draw two boxes, blue boxes, red boxes outside, and let us run from left to right. Here we show shows how many boxes you want to instantiate from clsBox also, and of course, each box has a set of properties with its own value.

Can be programmed to report to Object program when its host is an event (Event) occurred within the Class.
I try to declare an Event of clsBox Draw names, and write code for each Sub DrawBoxexecutes the Class will Raise a Draw event.

Public Event Draw(X As Integer, Y As Integer) 
Public Sub DrawBox(Canvas As Object, Optional fColor As Long) 
   If IsMissing(fColor) Then 
      Canvas.Line (mX, mY)-(mX + mWidth, mY + mHeight), , B 
      Canvas.Line (mX, mY)-(mX + mWidth, mY + mHeight), fColor, B 
   End If 
   RaiseEvent Draw(mX, mY) 
End Sub 

Now, instead of just declare Dim frmClass myBox as clsBox, we will declare as clsBoxmyBox Private WithEvents. Soon after, word myBox will appear in the list of Object'sEvent frmClass supports. Then we'll write code to handle the myBox Draw Event, ie weprovide code for Private Sub myBox_Draw (X as Integer, Y as Integer). Here we only display a message box reporting a newly drawn somewhere.

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (593x150)

When running the program, each object executes a Sub clsBox DrawBox frmClassdisplay you will see a message like this.

Remember, we declare an object with the WithEvents when we want to handle all of its Events. In the above example is frmClass myBox and it handles all of the myBox Draw Event. Similarly, even within a class, if he is assigned to a Class Object can Raise Events (such as TextBox, ListBox, Timer. Etc.), You can also declare WithEvents object so that it was Events can handle the object.
In the example below we write this in a Class codes are assigned to a textbox when the form calls for Object Sub InitObject to put a TextBox:

Private WithEvents mTextBox As TextBox 

Public Sub InitObject(givenTextBox As TextBox) 
   Set mTextBox = givenTextBox 
End Sub 

Private Sub mTextBox_KeyPress(KeyAscii As Integer) 
   ' Place your code here to handle this event within the Class Object
End Sub 
To learn more about how to use the Class, please read through the classes in the  sample VB6 Source Code


Post a Comment