SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright ©...
-
Upload
valerie-reeves -
Category
Documents
-
view
212 -
download
0
Transcript of SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright ©...
1
SSYYSSTTEEMMSS
DDEESSIIGGNN
AANNAALLYYSSIISS OO: Chapter 9OO: Chapter 9
Visual Basic: Building Components
Jerry Post
Copyright © 1999
2
SSYYSSTTEEMMSS
DDEESSIIGGNN
ComponentsComponents
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
3
SSYYSSTTEEMMSS
DDEESSIIGGNN
Creating a Component in VBCreating 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!
4
SSYYSSTTEEMMSS
DDEESSIIGGNN
VB Component PropertiesVB 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
5
SSYYSSTTEEMMSS
DDEESSIIGGNN
VB Property CodeVB Property Code
Option ExplicitPrivate m_InterestRate
Public Property Get InterestRate() As Variant InterestRate = m_InterestRateEnd Property
Public Property Let InterestRate(ByVal vNewValue As Variant) m_InterestRate = vNewValueEnd 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.
6
SSYYSSTTEEMMSS
DDEESSIIGGNN
VB Methods (Function or Sub)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_YearsEnd Sub
7
SSYYSSTTEEMMSS
DDEESSIIGGNN
VB Testing and Building ComponentsVB 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 componentPrivate 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.PresentValueEnd Sub
8
SSYYSSTTEEMMSS
DDEESSIIGGNN
Finalizing VB ComponentsFinalizing 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