 2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Procedures Outline 6.1Introduction 6.2 Modules, Classes and Procedures 6.3 Sub Procedures 6.4.

Slides:



Advertisements
Similar presentations
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Apply Sub Procedures/Methods and User Defined Functions
1 Lecture 3 Part 1 Functions with math and randomness.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
 2002 Prentice Hall. All rights reserved. 1 Week 2: Methods Questions about last week’s revision –C#.NET –.NET Framework –sequence, selection, repetition.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
6.1 Introduction Divide and Conquer –The best way to develop and maintain a large program is to construct it from small, manageable pieces. BZUPAGES.COM.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
Jozef Goetz,  2002 Prentice Hall. All rights reserved. Credits:  Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
 2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Procedures Outline 6.1Introduction 6.2 Modules, Classes and Procedures 6.3 Sub Procedures 6.4.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Chapter 6- Procedures. 6.1Introduction 6.2 Modules, Classes and Procedures 6.3 Sub Procedures 6.4 Function Procedures 6.5 Methods 6.6 Argument Promotion.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Controlling Program Flow with Decision Structures.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Subs and Functions Ch 6. Introduction VB.NET Procedures Sub Procedures Function Procedures Outline.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Functions Course conducted by: Md.Raihan ul Masood
Procedures Outline 6.1 Introduction 6.2   Modules, Classes and Procedures 6.3   Sub Procedures 6.4   Function Procedures 6.5   Methods 6.6   Argument Promotion.
Methods Chapter 6.
Subs, Functions and Events
JavaScript: Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 – Control Structures: Part 2
Chapter 10 - JavaScript: Functions
Chapter 5 - Functions Outline 5.1 Introduction
Variables and Arithmetic Operations
Chapter 6 - Functions Outline 5.1 Introduction
Program Modules in C# Modules The .NET Framework Class Library (FCL)
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Methods.
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Procedures Outline 6.1Introduction 6.2 Modules, Classes and Procedures 6.3 Sub Procedures 6.4 Function Procedures 6.5 Methods 6.6 Argument Promotion 6.7 Option Strict and Data Type Conversions 6.8 Value Types and Reference Types 6.9 Passing Arguments: Pass-by-Value vs. Pass-by-Reference 6.10 Duration of Identifiers 6.11 Scope Rules 6.12 Random-Number Generation 6.13 Example: Game of Chance 6.14 Recursion 6.15 Example Using Recursion: The Fibonacci Series 6.16 Recursion vs. Iteration

 2002 Prentice Hall. All rights reserved. 2 Outline 6.17 Procedure Overloading and Optional Arguments Procedure Overloading Optional Arguments 6.18 Modules

 2002 Prentice Hall. All rights reserved Introduction Divide and Conquer –The best way to develop and maintain a large program is to construct it from small, manageable pieces.

 2002 Prentice Hall. All rights reserved Modules, Classes and Procedures Framework Class Library –Provides a rich collection of “prepackaged” classes and methods for performing many operations Mathematical calculations String manipulations Character manipulations Input/output operations Error checking

 2002 Prentice Hall. All rights reserved Modules, Classes and Procedures Programmer-defined procedures –FCL cannot provide every conceivable feature that a programmer could want –Three types of procedures Sub procedures Function procedures Event procedures –A procedure is invoked by a procedure call

 2002 Prentice Hall. All rights reserved Modules, Classes and Procedures Fig. 6.1Hierarchical boss procedure/worker procedure relationship. Boss Worker1Worker2Worker3 Worker4Worker5

 2002 Prentice Hall. All rights reserved Modules, Classes and Procedures Division of code into procedures –Several motivations to divide code into procedures Divide-and-conquer approach makes program development more manageable Software reusability Avoids the repetition of code in a program

 2002 Prentice Hall. All rights reserved Modules, Classes and Procedures Earlier programs had only one procedure that called FCL methods Next program contains two customized procedures

 2002 Prentice Hall. All rights reserved. Outline 9 Payment.vb Program Output 1 ' Fig. 6.2: Payment.vb 2 ' Sub procedure that prints payment information. 3 4 Module modPayment 5 6 Sub Main() 7 8 ' call Sub procedure PrintPay 4 times 9 PrintPay(40, 10.5) 10 PrintPay(38, 21.75) 11 PrintPay(20, 13) 12 PrintPay(50, 14) Console.ReadLine() ' prevent window from closing 15 End Sub ' Main ' print amount of money earned in command window 18 Sub PrintPay(ByVal hours As Double, ByVal wage As Decimal) ' pay = hours * wage 21 Console.WriteLine("The payment is {0:C}", hours * wage) 22 End Sub ' PrintPay End Module ' modPayment The payment is $ The payment is $ The payment is $ The payment is $ PrintPay executes when it is invoked by Main PrintPay receives the values of each argument and stores them in the parameters variables hours and wage Notice that PrintPay appears within modPayment. All procedures must be defined inside a module or a class

 2002 Prentice Hall. All rights reserved Sub Procedures Format of a procedure definition Sub procedure-name(parameter-list) declarations and statements End Sub Procedure header –The first line is known as the procedure header Procedure-name –Directly follows the Sub keyword –Can be any valid identifier –It is used to call this Sub procedure within the program Procedure body –The declarations and statements in the procedure definition form the procedure body

 2002 Prentice Hall. All rights reserved Function Procedures Similar to Sub procedures One important difference –Function procedures return a value to the caller

 2002 Prentice Hall. All rights reserved. Outline 12 SquareInteger.vb 1 ' Fig. 6.3: SquareInteger.vb 2 ' Function procedure to square a number. 3 4 Module modSquareInteger 5 6 Sub Main() 7 Dim i As Integer ' counter 8 9 Console.WriteLine("Number" & vbTab & "Square" & vbCrLf) ' square numbers from 1 to For i = 1 To Console.WriteLine(i & vbTab & Square(i)) 14 Next End Sub ' Main ' Function Square is executed 19 ' only when the function is explicitly called. 20 Function Square(ByVal y As Integer) As Integer 21 Return y ^ 2 22 End Function ' Square End Module ' modSquareInteger The For structure displays the results of squaring the Integer s from 1-10 Square is invoked with the expression Square(i) The Return statement terminates execution of the procedure and returns the result of y ^ 2

 2002 Prentice Hall. All rights reserved. Outline 13 SquareInteger.vb Program Output Number Square

 2002 Prentice Hall. All rights reserved Function Procedures Format of a Function procedure definition Function procedure-name(parameter-list) As return-type declarations and statements End Function Return-type –Indicates the data type of the result returned from the Function to its caller Return expression –Can occur anywhere in a Function –It returns exactly one value –Control returns immediately to the point at which that procedure was invoked

 2002 Prentice Hall. All rights reserved Methods Definition of method –A method is any procedure that is contained within a class FCL methods Custom methods in programmer-defined classes

 2002 Prentice Hall. All rights reserved. Outline 16 Maximum.vb 1 ' Fig. 6.4: Maximum.vb 2 ' Program finds the maximum of three numbers input. 3 4 Public Class FrmMaximum 5 Inherits System.Windows.Forms.Form 6 7 ' prompts for three inputs 8 Friend WithEvents lblOne As System.Windows.Forms.Label 9 Friend WithEvents lblTwo As System.Windows.Forms.Label 10 Friend WithEvents lblThree As System.Windows.Forms.Label ' displays result 13 Friend WithEvents lblMaximum As System.Windows.Forms.Label ' read three numbers 16 Friend WithEvents txtFirst As System.Windows.Forms.TextBox 17 Friend WithEvents txtSecond As System.Windows.Forms.TextBox 18 Friend WithEvents txtThird As System.Windows.Forms.TextBox ' reads inputs and calculate results 21 Friend WithEvents cmdMaximum As System.Windows.Forms.Button ' Visual Studio.NET generated code ' obtain values in each text box, call procedure Maximum 26 Private Sub cmdMaximum_Click(ByVal sender As System.Object, _ 27 ByVal e As System.EventArgs) Handles cmdMaximum.Click 28 These are declarations of all the controls used in the GUI. Create these components visually, using the Toolbox Remember that all forms inherit from class System.Windows.Forms.Form Event handler cmdMaximum_Click Handles the event in which Button cmdMaximum is clicked

 2002 Prentice Hall. All rights reserved. Outline 17 Maximum.vb Program Output 29 Dim value1, value2, value3 As Double value1 = txtFirst.Text 32 value2 = txtSecond.Text 33 value3 = txtThird.Text lblMaximum.Text = Maximum(value1, value2, value3) 36 End Sub ' cmdMaximum_Click ' find maximum of three parameter values 39 Function Maximum(ByVal valueOne As Double, _ 40 ByVal valueTwo As Double, ByVal valueThree As Double) Return Math.Max(Math.Max(valueOne, valueTwo), valueThree) 43 End Function ' Maximum End Class ' FrmMaximum The values in the three TextBox es are retrieved using the Text property Call to methods defined in the class that contains the method call need only specify the method name Call to methods that are defined in a class in the FCL must include the class name and the dot (. ) operator

 2002 Prentice Hall. All rights reserved Methods Fig. 6.5Parameter Info feature of the Visual Studio.NET IDE. Parameter Info window

 2002 Prentice Hall. All rights reserved Methods Fig. 6.6IntelliSense feature of the Visual Studio.NET IDE.

 2002 Prentice Hall. All rights reserved Methods Fig. 6.7Math class methods.

 2002 Prentice Hall. All rights reserved Methods Fig. 6.7Math class methods.

 2002 Prentice Hall. All rights reserved Argument Promotion Coercion of arguments –The forcing of arguments to be appropriate data type so that they can be passed to a procedure Widening conversion –Occurs when a type is converted to another type without losing data Narrowing conversion –Occurs when there is potential for data loss during the conversion –Ex., double to single, double to integer

 2002 Prentice Hall. All rights reserved Argument Promotion Fig. 6.8Widening conversions. Mixed-type expressions: singleNumber1 + integerNumber2

 2002 Prentice Hall. All rights reserved Option Strict and Data-Type Conversions Option Explicit –Set to On by default –Forces the programmer to declare explicitly all variables before they are used Option strict –Set to Off by default –When set to On, it forces the programmer to perform an explicit conversion for all narrowing conversions Class Convert –The methods in class Convert change data types explicitly –Ex., Convert.ToDouble(“4”)

 2002 Prentice Hall. All rights reserved Option Strict and Data-Type Conversions Fig. 6.9Property Pages dialog with Option Strict set to On.

 2002 Prentice Hall. All rights reserved Value Types and Reference Types Variable of a value type –Contains the actual data –Used for a single piece of data Integer, Double Variable of a reference type –Contains a location in memory where data is stored –Known as objects Literals –Values typed directly in program code –Corresponds to one of the primitive data types User defined Types: –Value type: Enum, Structure –Reference type: class, interface, delegate

 2002 Prentice Hall. All rights reserved Value Types and Reference Types Fig. 6.10Visual Basic primitive data types.

 2002 Prentice Hall. All rights reserved Value Types and Reference Types Fig. 6.11Literals with type characters.

 2002 Prentice Hall. All rights reserved Passing Arguments: Pass-by-Value vs. Pass-by-Reference Pass-by-value –The program makes a copy of the argument’s value and passes that copy to the called procedure Pass-by-reference –The caller gives the called procedure the ability to access and modify the caller’s original data directly.

 2002 Prentice Hall. All rights reserved. Outline 30 ByRefTest.vb 1 ' Fig. 6.12: ByRefTest.vb 2 ' Demonstrates passing by reference. 3 4 Module modByRefTest 5 6 ' squares three values ByVal and ByRef, displays results 7 Sub Main() 8 Dim number1 As Integer = Console.WriteLine("Passing a value-type argument by value:") 11 Console.WriteLine("Before calling SquareByValue, " & _ 12 "number1 is {0}", number1) 13 SquareByValue(number1) ' passes number1 by value 14 Console.WriteLine("After returning from SquareByValue, " & _ 15 "number1 is {0}" & vbCrLf, number1) Dim number2 As Integer = Console.WriteLine("Passing a value-type argument" & _ 20 " by reference:") 21 Console.WriteLine("Before calling SquareByReference, " & _ 22 "number2 is {0}", number2) 23 SquareByReference(number2) ' passes number2 by reference 24 Console.WriteLine("After returning from " & _ 25 "SquareByReference, number2 is {0}" & vbCrLf, number2) Dim number3 As Integer = 2 28 When number1 is passed, a copy of the value is passed to the procedure A reference to the value stored in number2 is being passed

 2002 Prentice Hall. All rights reserved. Outline 31 ByRefTest.vb 29 Console.WriteLine("Passing a value-type argument" & _ 30 " by reference, but in parentheses:") 31 Console.WriteLine("Before calling SquareByReference " & _ 32 "using parentheses, number3 is {0}", number3) 33 SquareByReference((number3)) ' passes number3 by value 34 Console.WriteLine("After returning from " & _ 35 "SquareByReference, number3 is {0}", number3) End Sub ' Main ' squares number by value (note ByVal keyword) 40 Sub SquareByValue(ByVal number As Integer) 41 Console.WriteLine("After entering SquareByValue, " & _ 42 "number is {0}", number) 43 number *= number 44 Console.WriteLine("Before exiting SquareByValue, " & _ 45 "number is {0}", number) 46 End Sub ' SquareByValue ' squares number by reference (note ByRef keyword) 49 Sub SquareByReference(ByRef number As Integer) 50 Console.WriteLine("After entering SquareByReference" & _ 51 ", number is {0}", number) 52 number *= number 53 Console.WriteLine("Before exiting SquareByReference" & _ 54 ", number is {0}", number) 55 End Sub ' SquareByReference End Module ' modByRefTest ByVal indicates that value-type arguments should be passed by value ByRef gives direct access to the value stored in the original variable Enclosing arguments in parenthesis forces pass-by-value

 2002 Prentice Hall. All rights reserved. Outline 32 Program Output Passing a value-type argument by value: Before calling SquareByValue, number1 is 2 After entering SquareByValue, number is 2 Before exiting SquareByValue, number is 4 After returning from SquareByValue, number1 is 2 Passing a value-type argument by reference: Before calling SquareByReference, number2 is 2 After entering SquareByReference, number is 2 Before exiting SquareByReference, number is 4 After returning from SquareByReference, number2 is 4 Passing a value-type argument by reference, but in parentheses: Before calling SquareByReference using parentheses, number3 is 2 After entering SquareByReference, number is 2 Before exiting SquareByReference, number is 4 After returning from SquareByReference, number3 is 2

 2002 Prentice Hall. All rights reserved Duration of Identifiers Identifier’s duration –Period during which the identifier exists in memory Identifier’s scope –Portion of a program in which the variable’s identifier can be referenced Automatic duration –Identifiers that represent local variables in a procedure have automatic duration. They are called local variables. Variables declared in a module exists thru a program’s execution Instance variable –A variable declared in a class –They exist as long as their containing class is loaded in memory

 2002 Prentice Hall. All rights reserved Scope Rules Possible scopes –Class scope Begins at the class identifier after keyword Class and terminates at the End Class statement –Module scope Variable declared in a module have module scope, which is similar to class scope –Namespace scope Procedures defined in a module have namespace scope, which generally means that they may be accessed throughout a project, which is done by declaring with Public, instead of Dim –Block scope Identifiers declared inside a block, such as the body of a procedure definition or the body of an If/Then selection structure, have block scope, which begins at declaration, and ends at END

 2002 Prentice Hall. All rights reserved. Outline 35 Scoping.vb 1 ' Fig. 6.13: Scoping.vb 2 ' Demonstrates scope rules and instance variables. 3 4 Public Class FrmScoping 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents lblOutput As System.Windows.Forms.Label 8 9 ' Windows Form Designer generated code ' instance variable can be used anywhere in class 12 Dim value As Integer = ' demonstrates class scope and block scope 15 Private Sub FrmScoping_Load(ByVal sender As System.Object, _ 16 ByVal e As System.EventArgs) Handles MyBase.Load ' variable local to FrmScoping_Load hides instance variable 19 Dim value As Integer = lblOutput.Text = "local variable value in" & _ 22 " FrmScoping_Load is " & value MethodA() ' MethodA has automatic local value 25 MethodB() ' MethodB uses instance variable value 26 MethodA() ' MethodA creates new automatic local value 27 MethodB() ' instance variable value retains its value lblOutput.Text &= vbCrLf & vbCrLf & "local variable " & _ 30 "value in CScoping_Load is " & value 31 End Sub ' FrmScoping_Load ' automatic local variable value hides instance variable 34 Sub MethodA() 35 Dim value As Integer = 25 ' initialized after each call This variable is hidden in any procedure that declares a variable named value Automatic variable value is destroyed when MethodA terminates None of the method calls modifies this variable – both methods refer to variables in other scopes

 2002 Prentice Hall. All rights reserved. Outline 36 Scoping.vb lblOutput.Text &= vbCrLf & vbCrLf & "local variable " & _ 38 "value in MethodA is " & value & " after entering MethodA" 39 value += 1 40 lblOutput.Text &= vbCrLf & "local variable " & _ 41 "value in MethodA is " & value & " before exiting MethodA" 42 End Sub ' MethodA ' uses instance variable value 45 Sub MethodB() 46 lblOutput.Text &= vbCrLf & vbCrLf & "instance variable" & _ 47 " value is " & value & " after entering MethodB" 48 value *= lblOutput.Text &= vbCrLf & "instance variable " & _ 50 "value is " & value & " before exiting MethodB" 51 End Sub ' MethodB End Class ' FrmScoping When MethodB procedure refers to variable value, the instance variable value (line 12) is used.

 2002 Prentice Hall. All rights reserved Random-Number Generation Random class –Produces values at random –Keyword New creates an object of a specified type and returns the object’s location in memory –Next Method Generates a positive Integer value between zero and the constant Int32.MaxValue (2,147,483,647) The current time of day becomes the seed value for the calculation When a single argument is passed to Next, the values returned will be in the range from 0 to the value of that argument –Scaling By passing two arguments, the programmer is allowed to specify the bottom range too

 2002 Prentice Hall. All rights reserved. Outline 38 RandomInteger.vb 1 ' Fig. 6.14: RandomInteger.vb 2 ' Generating random integers. 3 4 Imports System.Windows.Forms 5 6 Module modRandomInteger 7 8 Sub Main() 9 Dim randomObject As Random = New Random() 10 Dim randomNumber As Integer 11 Dim output As String = "" 12 Dim i As Integer For i = 1 To randomNumber = randomObject.Next(1, 7) 16 output &= randomNumber & " " If i Mod 5 = 0 Then ' is i a multiple of 5? 19 output &= vbCrLf 20 End If Next MessageBox.Show(output, "20 Random Numbers from 1 to 6", _ 25 MessageBoxButtons.OK, MessageBoxIcon.Information) 26 End Sub ' Main End Module ' modRandomInteger Note that we must use 7 as the second argument to produce integers in the range from 1-6 Go to the next line every time five numbers are generated

 2002 Prentice Hall. All rights reserved. Outline 39 RollDice.vb 1 ' Fig. 6.15: RollDice.vb 2 ' Rolling four dice. 3 4 Imports System.IO 5 6 Public Class FrmRollDice 7 Inherits System.Windows.Forms.Form 8 9 ' button for rolling dice 10 Friend WithEvents cmdRoll As System.Windows.Forms.Button ' labels to display die images 13 Friend WithEvents lblDie1 As System.Windows.Forms.Label 14 Friend WithEvents lblDie2 As System.Windows.Forms.Label 15 Friend WithEvents lblDie3 As System.Windows.Forms.Label 16 Friend WithEvents lblDie4 As System.Windows.Forms.Label ' Visual Studio.NET generated code ' declare Random object reference 21 Dim randomNumber As Random = New Random() ' display results of four rolls 24 Private Sub cmdRoll_Click(ByVal sender As System.Object, _ 25 ByVal e As System.EventArgs) Handles cmdRoll.Click ' method randomly assigns a face to each die 28 DisplayDie(lblDie1) 29 DisplayDie(lblDie2) 30 DisplayDie(lblDie3) 31 DisplayDie(lblDie4) 32 End Sub ' cmdRoll_Click 33 Event-handling method cmdRoll_Click, executes whenever the user clicks cmdRoll RandomNumber is an instance variable of FrmRollDice. This allows the same Random object to be used each time DisplayDie executes

 2002 Prentice Hall. All rights reserved. Outline 40 RollDice.vb 34 ' get a random die image 35 Sub DisplayDie(ByVal dieLabel As Label) ' generate random integer in range 1 to 6 38 Dim face As Integer = randomNumber.Next(1, 7) ' load corresponding image 41 dieLabel.Image = Image.FromFile( _ 42 Directory.GetCurrentDirectory & "\Images\die" & _ 43 face & ".png") 44 End Sub ' DisplayDie End Class ' FrmRollDice Image property displays an image on the labelClass Image is contained in the System.Drawing namespace, which is imported by default in all Windows Applications Method Directory.GetCurrentDirectory returns the location of the folder in which the current project is located, including bin

 2002 Prentice Hall. All rights reserved. Outline 41 RollTwelveDice.v b 1 ' Fig. 6.16: RollTwelveDice.vb 2 ' Rolling 12 dice with frequency chart. 3 4 Imports System.IO 5 6 Public Class FrmRollTwelveDice 7 Inherits System.Windows.Forms.Form 8 9 ' labels to display die images 10 Friend WithEvents lblDie1 As System.Windows.Forms.Label 11 Friend WithEvents lblDie2 As System.Windows.Forms.Label 12 Friend WithEvents lblDie3 As System.Windows.Forms.Label 13 Friend WithEvents lblDie4 As System.Windows.Forms.Label 14 Friend WithEvents lblDie5 As System.Windows.Forms.Label 15 Friend WithEvents lblDie6 As System.Windows.Forms.Label 16 Friend WithEvents lblDie7 As System.Windows.Forms.Label 17 Friend WithEvents lblDie8 As System.Windows.Forms.Label 18 Friend WithEvents lblDie9 As System.Windows.Forms.Label 19 Friend WithEvents lblDie10 As System.Windows.Forms.Label 20 Friend WithEvents lblDie11 As System.Windows.Forms.Label 21 Friend WithEvents lblDie12 As System.Windows.Forms.Label ' displays roll frequencies 24 Friend WithEvents displayTextBox As _ 25 System.Windows.Forms.TextBox ' Visual Studio.NET generated code ' declarations 30 Dim randomObject As Random = New Random() 31 Dim ones, twos, threes, fours, fives, sixes As Integer 32 We declare counters for each of the possible rolls The TextBox is used to display the cumulative frequencies of each face

 2002 Prentice Hall. All rights reserved. Outline 42 RollTwelveDice.v b 35 Private Sub cmdRoll_Click _ 34 (ByVal sender As System.Object, _ 35 ByVal e As System.EventArgs) Handles cmdRoll.Click ' assign random faces to 12 dice using DisplayDie 38 DisplayDie(lblDie1) 39 DisplayDie(lblDie2) 40 DisplayDie(lblDie3) 41 DisplayDie(lblDie4) 42 DisplayDie(lblDie5) 43 DisplayDie(lblDie6) 44 DisplayDie(lblDie7) 45 DisplayDie(lblDie8) 46 DisplayDie(lblDie9) 47 DisplayDie(lblDie10) 48 DisplayDie(lblDie11) 49 DisplayDie(lblDie12) Dim total As Integer = ones + twos + threes + fours + _ 52 fives + sixes Dim output As String ' display frequencies of faces 57 output = "Face" & vbTab & vbTab & _ 58 "Frequency" & vbTab & "Percent" output &= vbCrLf & "1" & vbTab & vbTab & ones & _ 61 vbTab & vbTab & String.Format("{0:P}", ones / total) output &= vbCrLf & "2" & vbTab & vbTab & twos & vbTab & _ 64 vbTab & String.Format("{0:P}", twos / total) output &= vbCrLf & "3" & vbTab & vbTab & threes & vbTab & _ 67 vbTab & String.Format("{0:P}", threes / total) The “P” format code is used to display the frequency of each roll as percentages

 2002 Prentice Hall. All rights reserved. Outline 43 RollTwelveDice.vb output &= vbCrLf & "4" & vbTab & vbTab & fours & vbTab & _ 70 vbTab & String.Format("{0:P}", fours / total) output &= vbCrLf & "5" & vbTab & vbTab & fives & vbTab & _ 73 vbTab & String.Format("{0:P}", fives / total) output &= vbCrLf & "6" & vbTab & vbTab & sixes & vbTab & _ 76 vbTab & String.Format("{0:P}", sixes / total) & vbCrLf displayTextBox.Text = output 79 End Sub ' cmdRoll_Click ' display a single die image 82 Sub DisplayDie(ByVal dieLabel As Label) Dim face As Integer = randomObject.Next(1, 7) dieLabel.Image = _ 87 Image.FromFile(Directory.GetCurrentDirectory & _ 88 "\Images\die" & face & ".png") ' maintain count of die faces 91 Select Case face Case 1 94 ones += Case 2 97 twos += Case threes += Select Case is used to calculate the frequency

 2002 Prentice Hall. All rights reserved. Outline 44 RollTwelveDice.vb 102 Case fours += Case fives += Case sixes += End Select End Sub ' DisplayDie End Class ' FrmRollTwelveDice

 2002 Prentice Hall. All rights reserved Example: Game of Chance Craps –The next application simulates one of the most popular games of chance –The player must roll two dice on the first and all subsequent rolls

 2002 Prentice Hall. All rights reserved. Outline 46 CrapsGame.vb 1 ' Fig 6.17: CrapsGame.vb 2 ' Playing a craps game. 3 4 Imports System.IO 5 6 Public Class FrmCrapsGame 7 Inherits System.Windows.Forms.Form 8 9 Friend WithEvents cmdRoll As Button ' rolls dice 10 Friend WithEvents cmdPlay As Button ' starts new game ' dice displayed after each roll 13 Friend WithEvents picDie1 As PictureBox 14 Friend WithEvents picDie2 As PictureBox ' pointDiceGroup groups dice representing player's point 17 Friend WithEvents pointDiceGroup As GroupBox 18 Friend WithEvents picPointDie1 As PictureBox 19 Friend WithEvents picPointDie2 As PictureBox Friend WithEvents lblStatus As Label ' Visual Studio.NET generated code ' die-roll constants 26 Enum DiceNames 27 SNAKE_EYES = 2 28 TREY = 3 29 CRAPS = 7 30 YO_LEVEN = BOX_CARS = End Enum 33 A GroupBox is a container used to group related components Enum erations are used to define groups of related constants

 2002 Prentice Hall. All rights reserved. Outline 47 CrapsGame.vb 34 ' file-name and directory constants 35 Const FILE_PREFIX As String = "/images/die" 36 Const FILE_SUFFIX As String = ".png" Dim myPoint As Integer 39 Dim myDie1 As Integer 40 Dim myDie2 As Integer 41 Dim randomObject As Random = New Random() ' begins new game and determines point 44 Private Sub cmdPlay_Click(ByVal sender As System.Object, _ 45 ByVal e As System.EventArgs) Handles cmdPlay.Click ' initialize variables for new game 48 myPoint = 0 49 pointDiceGroup.Text = "Point" 50 lblStatus.Text = "" ' remove point-die images 53 picPointDie1.Image = Nothing 54 picPointDie2.Image = Nothing Dim sum As Integer = RollDice() ' check die roll 59 Select Case sum Case DiceNames.CRAPS, DiceNames.YO_LEVEN ' disable roll button 64 cmdRoll.Enabled = False 65 lblStatus.Text = "You Win!!!" 66 Keyword Const creates a single constant identifier in which values cannot be modified after they are declared Keyword Nothing can be used with reference-type variables to specify that no object is associated with the variable Setting the Image property to Nothing causes the PictureBoxes to appear blank The Select structure analyzes the roll returned by RollDice to determine how play should continue

 2002 Prentice Hall. All rights reserved. Outline 48 CrapsGame.vb 67 Case DiceNames.SNAKE_EYES, _ 68 DiceNames.TREY, DiceNames.BOX_CARS cmdRoll.Enabled = False 71 lblStatus.Text = "Sorry. You Lose." Case Else 74 myPoint = sum 75 pointDiceGroup.Text = "Point is " & sum 76 lblStatus.Text = "Roll Again!" 77 DisplayDie(picPointDie1, myDie1) 78 DisplayDie(picPointDie2, myDie2) 79 cmdPlay.Enabled = False 80 cmdRoll.Enabled = True End Select End Sub ' cmdPlay_Click ' determines outcome of next roll 87 Private Sub cmdRoll_Click(ByVal sender As System.Object, _ 88 ByVal e As System.EventArgs) Handles cmdRoll.Click Dim sum As Integer = RollDice() ' check outcome of roll 93 If sum = myPoint Then 94 lblStatus.Text = "You Win!!!" 95 cmdRoll.Enabled = False 96 cmdPlay.Enabled = True 97 ElseIf sum = DiceNames.CRAPS Then 98 lblStatus.Text = "Sorry. You Lose." 99 cmdRoll.Enabled = False 100 cmdPlay.Enabled = True 101 End If Disabling a Button causes no action to be performed when the Button is clicked

 2002 Prentice Hall. All rights reserved. Outline 49 CrapsGame.vb End Sub ' cmdRoll_Click ' display die image 106 Sub DisplayDie(ByVal picDie As PictureBox, _ 107 ByVal face As Integer) ' assign die image to picture box 110 picDie.Image = _ 111 Image.FromFile(Directory.GetCurrentDirectory & _ 112 FILE_PREFIX & face & FILE_SUFFIX) 113 End Sub ' DisplayDie ' generate random die rolls 116 Function RollDice() As Integer 117 Dim die1, die2 As Integer ' determine random integer 120 die1 = randomObject.Next(1, 7) 121 die2 = randomObject.Next(1, 7) ' display rolls 124 DisplayDie(picDie1, die1) 125 DisplayDie(picDie2, die2) ' set values 128 myDie1 = die1 129 myDie2 = die Return die1 + die2 132 End Function ' RollDice End Class ' FrmCrapsGame RollDice generates two random numbers and calls method DisplayDie, which loads an appropriate die image on the PictureBox passed to it.

 2002 Prentice Hall. All rights reserved. Outline 50 CrapsGame.vb GroupBoxPictureBoxes (displaying images)

 2002 Prentice Hall. All rights reserved Recursion Recursive procedure –It is a procedure that calls itself either directly or indirectly –It is called to solve a problem –The procedure knows how to solve only the simples case (base case) –For complex problems, the procedure divides the problem into a piece that it can perform and a piece that it does not know how to perform –Recursive call The procedure invokes a fresh copy of itself to work on the smaller problem

 2002 Prentice Hall. All rights reserved Recursion Fig. 6.18Recursive evaluation of 5!. 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Final value = 120 5! = 5 * 24 = 120 is returned 4! = 4 * 6 = 24 is returned 3! = 3 * 2 = 6 is returned 2! = 2 * 1 = 2 is returned 1 returned (a) Procession of recursive calls(b) Values returned from each recursive call

 2002 Prentice Hall. All rights reserved. Outline 53 Factorial.vb 1 ' Fig. 6.19: Factorial.vb 2 ' Calculating factorials using recursion. 3 4 Public Class FrmFactorial 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents lblEnter As Label ' prompts for Integer 8 Friend WithEvents lblFactorial As Label ' indicates output 9 10 Friend WithEvents txtInput As TextBox ' reads an Integer 11 Friend WithEvents txtDisplay As TextBox ' displays output Friend WithEvents cmdCalculate As Button ' generates output ' Visual Studio.NET generated code Private Sub cmdCalculate_Click(ByVal sender As System.Object, _ 18 ByVal e As System.EventArgs) Handles cmdCalculate.Click Dim value As Integer = Convert.ToInt32(txtInput.Text) 21 Dim i As Integer 22 Dim output As String txtDisplay.Text = "" For i = 0 To value 27 txtDisplay.Text &= i & "! = " & Factorial(i) & vbCrLf 28 Next End Sub ' cmdCalculate_Click Conversion from String to Integer

 2002 Prentice Hall. All rights reserved. Outline 54 Factorial.vb ' recursively generates factorial of number 33 Function Factorial(ByVal number As Long) As Long If number <= 1 Then ' base case 36 Return 1 37 Else 38 Return number * Factorial(number - 1) 39 End If End Function ' Factorial End Class ' FrmFactorial If number is greater than 1, a recursive call to Factorial is made with a slightly simpler problem Forgetting to return a value from a recursive procedure can result in logic errors

 2002 Prentice Hall. All rights reserved Example Using Recursion: Fibonacci Series The Fibonacci series –It begins with 0 and 1 and defines each subsequent Fibonacci number as the sum of the previous two Fibonacci numbers Golden ratio –The ratio of successive fibonacci numbers converges on a constant value near Fibonacci method –Each invocation of the method that does not match one of the base cases results in two additional recursive calls to the method –Fibonacci value of 30 requires 2,692,537

 2002 Prentice Hall. All rights reserved. Outline 56 Fibonacci.vb 1 ' Fig. 6.20: Fibonacci.vb 2 ' Demonstrating Fibonacci sequence recursively. 3 4 Public Class FrmFibonacci 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents lblPrompt As Label ' prompts for input 8 Friend WithEvents lblResult As Label ' displays result 9 10 Friend WithEvents cmdCalculate As Button ' calculates result Friend WithEvents txtInputBox As TextBox ' reads an Integer ' Visual Studio.NET generated code ' displays Fibonacci number in txtInputBox 17 Private Sub cmdCalculate_Click(ByVal sender As System.Object, _ 18 ByVal e As System.EventArgs) Handles cmdCalculate.Click ' read input 21 Dim number As Integer = Convert.ToInt32(txtInputBox.Text) lblResult.Text = "Fibonacci Value is " & Fibonacci(number) 24 End Sub ' cmdCalculate_Click ' calculate Fibonacci value recusively 27 Function Fibonacci(ByVal number As Integer) As Long ' check for base cases 30 If number = 1 OrElse number = 0 Then 31 Return number 32 Else 33 Return Fibonacci(number - 1) + Fibonacci(number - 2) 34 End If 35 This call to Fibonacci is not a recursive callIf number is greater than 1, the recursion step generates two recursive calls

 2002 Prentice Hall. All rights reserved. Outline 57 Fibonacci.vb 36 End Function ' Fibonacci End Class ' FrmFibonacci

 2002 Prentice Hall. All rights reserved Example Using Recursion: Fibonacci Series Fig. 6.21Recursive calls to method Fibonacci (abbreviated as F). Fibonacci( 3 ) Fibonacci( 2 )Fibonacci( 1 ) Fibonacci( 0 )Fibonacci( 1 )return 1 return return 1 return 0 return

 2002 Prentice Hall. All rights reserved Recursion vs. Iteration Iteration –Involves an explicit repetition structure –Uses a repetition structure For, While or Do / Loop Until Recursion –Achieves repetition through repeated procedure calls –Uses a selection structure If / Then, If / Then / Else or Select –Recursive calls take time and consume additional memory

 2002 Prentice Hall. All rights reserved Procedure Overloading and Optional Arguments Overloading –Allows multiple procedures with the same name, but differing numbers and types of arguments –The overloading of procedures that perform closely related tasks can make programs more readable and understandable Optional arguments –Defining an argument as optional allows the calling procedure to decide what arguments to pass

 2002 Prentice Hall. All rights reserved. Outline 61 Overload.vb 1 ' Fig. 6.22: Overload.vb 2 ' Using overloaded methods. 3 4 Public Class FrmOverload 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents outputLabel As Label 8 9 ' Visual Studio.NET generated code Private Sub FrmOverload_Load(ByVal sender As System.Object, _ 12 ByVal e As System.EventArgs) Handles MyBase.Load outputLabel.Text = "The square of Integer 7 is " & _ 15 square(7) & vbCrLf & "The square of Double " & _ 16 "7.5 is " & square(7.5) 17 End Sub ' FrmOverload_Load Function Square(ByVal value As Integer) As Integer 20 Return Convert.ToInt32(value ^ 2) 21 End Function ' Square Function Square(ByVal value As Double) As Double 24 Return value ^ 2 25 End Function ' Square End Class ' FrmOverload The compiler uses a logical name to differ between the two Square methods The compiler might use the logical name “ Square of Integer ” “ Square of Double ” for the Square method that specifies a Double parameter

 2002 Prentice Hall. All rights reserved. Outline 62 Overload2.vb 1 ' Fig. 6.23: Overload2.vb 2 ' Using overloaded procedures with identical signatures and 3 ' different return types. 4 5 Public Class FrmOverload2 6 Inherits System.Windows.Forms.Form 7 8 Friend WithEvents outputLabel As Label 9 10 ' Visual Studio.NET generated code Private Sub FrmOverload2_Load(ByVal sender As System.Object, _ 13 ByVal e As System.EventArgs) Handles MyBase.Load outputLabel.Text = "The square of Integer 7 is " & _ 16 square(7) & vbCrLf & "The square of Double " & _ 17 "7.5 is " & square(7.5) 18 End Sub ' FrmOverload2_Load Function Square(ByVal value As Double) As Integer 21 Return Convert.ToInt32(value ^ 2) 22 End Function ' Square Function Square(ByVal value As Double) As Double 25 Return value ^ 2 26 End Function ' Square End Class ' FrmOverload2 Procedure calls cannot be distinguished by return type

 2002 Prentice Hall. All rights reserved. Outline 63 Overload2.vb Program Output The creating of overloaded procedures with identical parameter lists and different return types produces a syntax error

 2002 Prentice Hall. All rights reserved Optional Arguments Optional –Optional arguments are specified in the procedure header with keyword Optional Syntax errors –Not specifying a default value for an Optional parameter is a syntax error –Declaring a non- Optional parameter to the right of an Optional parameter is a syntax error

 2002 Prentice Hall. All rights reserved. Outline 65 Power.vb 1 ' Fig 6.24 Power.vb 2 ' Calculates the power of a value, defaults to square. 3 4 Public Class FrmPower 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents txtBase As TextBox ' reads base 8 Friend WithEvents txtPower As TextBox ' reads power 9 10 Friend WithEvents inputGroup As GroupBox Friend WithEvents lblBase As Label ' prompts for base 13 Friend WithEvents lblPower As Label ' prompts for power 14 Friend WithEvents lblOutput As Label ' displays output Friend WithEvents cmdCalculate As Button ' generates output ' Visual Studio.NET generated code ' reads input and displays result 21 Private Sub cmdCalculate_Click(ByVal sender As System.Object, _ 22 ByVal e As System.EventArgs) Handles cmdCalculate.Click Dim value As Integer ' call version of Power depending on power input 27 If Not txtPower.Text = "" Then 28 value = Power(Convert.ToInt32(txtBase.Text), _ 29 Convert.ToInt32(txtPower.Text)) 30 Else 31 value = Power(Convert.ToInt32(txtBase.Text)) 32 End If lblOutput.Text = Convert.ToString(value) 35 End Sub ' cmdCalculate_Click Determines whether txtPower contains a value

 2002 Prentice Hall. All rights reserved. Outline 66 Power.vb ' use iteration to calculate power 38 Function Power(ByVal base As Integer, _ 39 Optional ByVal exponent As Integer = 2) As Integer Dim total As Integer = 1 42 Dim i As Integer For i = 1 To exponent 45 total *= base 46 Next Return total 49 End Function ' Power End Class ' FrmPower When omitted, the Optional argument defaults to the value 2

 2002 Prentice Hall. All rights reserved Modules Modules –Used to group related procedures so that they can be reused in other projects –Similar in many ways to classes –Should be self-contained

 2002 Prentice Hall. All rights reserved. Outline 68 DiceModule.vb 1 ' Fig. 6.25: DiceModule.vb 2 ' A collection of common dice procedures. 3 4 Imports System.IO 5 6 Module modDice 7 8 Dim randomObject As Random = New Random() 9 10 ' rolls single die 11 Function RollDie() As Integer 12 Return randomObject.Next(1, 7) 13 End Function ' RollDie ' die summation procedure 16 Function RollAndSum(ByVal diceNumber As Integer) _ 17 As Integer Dim i As Integer 20 Dim sum As Integer = For i = 1 To diceNumber 23 sum += RollDie() 24 Next Return sum 27 End Function ' RollAndSum modDice groups several dice-related procedures into a module for reuse in other programs that use dice

 2002 Prentice Hall. All rights reserved. Outline 69 DiceModule.vb ' returns die image 30 Function GetDieImage(ByVal dieValue As Integer, _ 31 Optional ByVal baseImageName As String = "die") _ 32 As System.Drawing.Image Return Image.FromFile( _ 35 Directory.GetCurrentDirectory & _ 36 "\Images\" & baseImageName & dieValue & ".png") 37 End Function ' GetDieImage End Module ' modDice Optional parameter baseImageName represents the prefix of the image name to be used

 2002 Prentice Hall. All rights reserved. Outline 70 DiceModuleTest.v b 1 ' Fig. 6.26: DiceModuleTest.vb 2 ' Demonstrates modDiceModule procedures 3 4 Imports System.Drawing 5 6 Public Class FrmDiceModuleTest 7 Inherits System.Windows.Forms.Form 8 9 Friend WithEvents lblSum As Label ' displays 10-roll sum Friend WithEvents diceGroup As GroupBox ' dice images 14 Friend WithEvents picDie1 As PictureBox 15 Friend WithEvents picDie2 As PictureBox Friend WithEvents cmdRollDie1 As Button ' rolls blue die 18 Friend WithEvents cmdRollTen As Button ' simulates 10 rolls 19 Friend WithEvents cmdRollDie2 As Button ' rolls red die ' Visual Studio.NET generated code Private Sub cmdRollDie1_Click(ByVal sender As System.Object, _ 24 ByVal e As System.EventArgs) Handles cmdRollDie1.Click picDie1.Image = modDice.GetDieImage(modDice.RollDie()) 27 End Sub ' cmdRollDie1_Click Private Sub cmdRollDie2_Click(ByVal sender As System.Object, _ 30 ByVal e As System.EventArgs) Handles cmdRollDie2.Click picDie2.Image = modDice.GetDieImage(modDice.RollDie(), _ 33 "redDie") 34 End Sub ' cmdRollDie2_Click 35 cmdRollDie2_Click uses the Optional argument to prefix the image name and select a different image We call procedures contained in modDice by following the module name with the dot (. ) operator and the procedure name

 2002 Prentice Hall. All rights reserved. Outline 71 DiceModuleTest.v b 36 Private Sub cmdRollTen_Click(ByVal sender As System.Object, _ 37 ByVal e As System.EventArgs) Handles cmdRollTen.Click lblSum.Text = Convert.ToString(modDice.RollAndSum(10)) 40 End Sub ' cmdRollTen_Click End Class ' FrmDiceModuleTest This procedure sets the Text property of lblSum to the result of 10 rolls