Wednesday, December 12, 2012

class module and standard module in visual basic


The Class Module will completely hide the implementation details. It will expose its functionality and the developer will never see or edit its source code. Class modules can immensely improve your productivity, help you solve many common and intricate programming problems, and even permit you to perform tasks that would be extremely difficult, if not impossible, otherwise.
Even if Visual Basic isn’t a full-fledged object-oriented programming language, you can still use its classes to better organize your code into truly reusable modules and design your applications entirely using concepts derived from the Object-Oriented Design discipline.

Steps to create a new Class:
1.  In the New Project add a Class Modules folder in the Project Explorer window and a Class Module under it. The Class Module is named Class1 by default.
2.  Change the Class Module’s Name property to CTimer. The Class Module doesn’t have a visible interface, so the Code window for the new component will be displayed.
3.  Enter the lines in the Class Module’s Code window.

Code : The CTimer Class’ Code
Dim totalInterval As Double
Dim T1 As Double

Public Sub StartCounting()
    T1 = Time
End Sub

Public Sub StopCounting()
    totalInterval = totalInterval + Time - T1
End Sub

Property Get ElapsedTime() As Double
    ElapsedTime = totalInterval
End Property

Public Sub ResetTimer()
    totalInterval = 0
End Sub
The code of the Class Module is quite similar to the Module’s code, but it doesn’t have a public variable. The totalInterval variable can’t be accessed directly from any procedure outside the Class Module. It can only be read through the ElapsedTime() procedure. Notice that this is a special type procedure called Property Get. Every time an application attempts to read the value of the totalInterval variable, the ElapsedTime() procedure is invoked. Its code reads the value of the totalInterval local variable and assigns it to the ElapsedTime() procedure. This value is then passed to the calling application.
When an application attempts to set a property value, a similar procedure is invoked, only it’s a Property Let procedure. This Class doesn’t have any properties that can be set, so there are no Property Let procedures in it. We’ll discuss both Property procedures after looking at this simple example. The Property Let and Property Get procedures act like buffers between the Class and the application that uses it. For one thing, the application can’t set the variable directly.
Testing the CTimer Class
1.  Add a new project with the File -> Add Project command. Visual Basic will add a new folder to the Explorer window, the Project1 folder, and it will place a Form under it. 
2.  In order to use the CTimer Class in the test project, we must first add a reference to the Class. Open the Project menu and select References. In the References dialog box select the entry TimerProject by checking it. The References dialog box displays the name of the project, not the name of the Class.
Then, open the Form’s Code window and enter the lines in Code:
Dim TMR As New CTimer
 
Private Sub Command1_Click()
    If Command1.Caption = “Start Timing” Then
        TMR.StartCounting
        Command1.Caption = “Stop Timing”
    Else
        TMR.StopCounting
        Command1.Caption = “Start Timing”
    End If
End Sub
 
Private Sub Command2_Click()
    ETime = TMR.ElapsedTime
    MsgBox “I’ve been counting for ” & vbCrLf & _
        Hour(TMR.ETime) & “ hours” & vbCrLf & _
        Minute(TMR.ETime) & “ minutes and ” & vbCrLf & _
        Second(TMR.ETime) & “ seconds” & vbCrLf
End Sub
 
Private Sub Command3_Click()
    End
End Sub

Standard module.

                        1. When using multiple forms in a project, it is important for you to consider each sub procedure or function procedure that you create. If the procedure will be used in only one form, then it should be included in the code for that form module. If, in fact, you will need to use the procedure in multiple forms, write the procedure in standard code module.
                        2. A standard code module is a Visual Basic file with extension .bas, which is added to project. Standard code module does not contain a form, only code.
                        3. Create a new standard code module by selecting the Add Module command on the project menu. Select module on the new tab of the add module dialog box and click open. A new code window titled module1 opens on the screen. Module is also added to project explorer.
                        4. Standard code module has a general declarations section and procedure, just like form module. You can declare a variable and procedure as you declare in form module.