SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999
SYSTEMSDESIGN 2 Components A reusable class installed on Windows Can be used in any standard programming system: Visual Basic C++ Internet Information Server Excel, Access, … Class properties or attributes Class methods or functions
SYSTEMSDESIGN 3 Creating a Component in VB Start Visual Basic (might not exist on student version) File | New Project: Active X DLL Project | Properties Name, Description Class Module Properties: Name Warning: Pick simple, informative names!
SYSTEMSDESIGN 4 VB Component Properties Code: Create Variables to Hold Properties Option Explicit (catches typos by requiring all variables to be pre- defined) Declare property variables: Private m_InterestRate As Double Private m_Years As Integer Private m_FutureValue As Currency Private m_PresentValue As Currency Tools | Add Procedure Name Property check box Public
SYSTEMSDESIGN 5 VB Property Code Option Explicit Private m_InterestRate Public Property Get InterestRate() As Variant InterestRate = m_InterestRate End Property Public Property Let InterestRate(ByVal vNewValue As Variant) m_InterestRate = vNewValue End Property Retrieve a property value from the object and give it to caller. Accept a property value, test it, and store it in the object.
SYSTEMSDESIGN 6 VB Methods (Function or Sub) Tools | Add Procedure Name Property check box Public Write code Use property variables Assign property variables Public Sub NPV() m_PresentValue = m_FutureValue / (1 + m_InterestRate) ^ m_Years End Sub
SYSTEMSDESIGN 7 VB Testing and Building Components Save the project: File | Save Add a new project (for testing) File | Add Project: Standard EXE Make the Component visible Project | References Create a form for the user Add input boxes, and a command button (or other event) Code to activate and use the component Private Sub cmdCompute_Click() Dim obj As Compute Set obj = CreateObject("SimpleFinance.Compute") obj.FutureValue = txtFuture obj.InterestRate = txtInterestRate obj.Years = txtYears obj.NPV txt.PresentValue = obj.PresentValue End Sub
SYSTEMSDESIGN 8 Finalizing VB Components Add error handling and return messages/flags. Test it extensively. Build the DLL: File | Make ….dll Distribute the DLL and the applications. Register the DLL: regsvr32 SimpleFinance.dll To remove it: regsvr32 /u SimpleFinance.dll