Вы находитесь на странице: 1из 9

CSEN 2303 Introduction to Computing Visual Basic and Excel

Fall 2009

Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 17-18 I Hear You Are Breaking Up, Talk to ME

Objectives
Create a sub procedure Call a sub procedure
Pass data by value to a procedure Pass data by reference to a procedure

Create a function procedure Call a function procedure

CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

Whats the Proper Procedure?


Sub procedures
Allow large and complex applications to be broken into small and manageable tasks

CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click Dim dblHoursWork As Double Dim dblRatePay As Double Dim dblGrossPay As Double

Double.TryParse(lstHours.SelectedItem.ToString, dblHoursWork) Double.TryParse(lstRate.SelectedItem.ToString, dblRatePay)


Call calc_GrossPay(dblHoursWork, dblRatePay, dblGrossPay) lblGross.Text = dblGrossPay.ToString("C2") End Sub Private Sub calc_GrossPay( ByVal dblHours As Double, ByVal dblRate As Double, _ ByRef dblGross As Double) dblGross = dblHours * dblRate If dblHours > 40 Then dblGross = dblGross + (dblHours - 40) * dblRate / 2 End If End Sub

CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

Whats the Answer?


Function procedure
Returns a value after performing its assigned task Can receive information either by value or by reference Information it receives is listed in the parameterList in the header

Sub procedure
Does not return a value

CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

Clearly Visual Basic: Programming with Visual Basic 2008 CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click Dim dblHoursWork As Double Dim dblRatePay As Double Dim dblGrossPay As Double Double.TryParse(lstHours.SelectedItem.ToString, dblHoursWork) Double.TryParse(lstRate.SelectedItem.ToString, dblRatePay) dblGrossPay = calc_GrossPay(dblHoursWork, dblRatePay, dblGrossPay) lblGross.Text = dblGrossPay.ToString("C2") End Sub

Private Function calc_GrossPay( ByVal dblHours As Double, ByVal dblRate As Double, _ ByVal dblGross As Double)
dblGross = dblHours * dblRate If dblHours > 40 Then dblGross = dblGross + (dblHours - 40) * dblRate / 2 End If Return dblGross End Function

CSEN2303 Introduction to Computing Using Visual Basic & Excel

Fall 2009

Вам также может понравиться