SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright ©...

8
1 S S Y Y S S T T E E M M S S D D E E S S I I G G N N A A N N A A L L Y Y S S I I S S OO: Chapter 9 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999

Transcript of SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright ©...

Page 1: SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.

1

SSYYSSTTEEMMSS

DDEESSIIGGNN

AANNAALLYYSSIISS OO: Chapter 9OO: Chapter 9

Visual Basic: Building Components

Jerry Post

Copyright © 1999

Page 2: SYSTEMSDESIGNANALYSIS 1 OO: 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

Page 3: SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.

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!

Page 4: SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.

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

Page 5: SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.

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.

Page 6: SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.

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

Page 7: SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.

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

Page 8: SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.

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