170 likes | 186 Vues
Explore the components of a module or class, learn the syntax and declaration of methods, understand subroutines and functions, and discover how to use recursions.
E N D
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011
Objectives • In this chapter, you will: • Explore the components of a module or a class • Get familiar with the syntax of methods • Learn the declaration of methods • Subroutines • Functions • Become aware of .Net Namespace • Discover how to use recursions
Modules, Classes and Methods • Modules and classes make up a program • Modules and classes are composed of smaller pieces called methods, instance variables and properties • Related classes are grouped into namespaces • Two types of methods exist: • Subroutines • Functions
Pre-Packaged and User Defined • .NET framework provides a class library that contains many pre-packaged classes: • Mathematical calculations, error checking, GUI & graphics, String and character manipulations, I/O operations, XML, database, web, etc. • Users can define their own classes • You can combine new modules and classes with pre-packaged ones
Method Basics • A method is invoked by a method call • Method call specifies the method name and provides information as arguments • When method is finished the control goes back to the caller • The method may or may not return results • Method provides for divide-and-conquer approach to software engineering • Methods can be used over and over again
Methods That Do Not Return a Value • Methods that only performs a task such as printing, but do not return a value • These methods are generally called Subroutines • The parameters passed into these methods are ByVal parameters.
Declaring a Subroutine • SubmethodName (parameterList) • Declarations and statements End Sub • If you pass a parameter ByRefand if the value of that parameter is changed by the subroutine, that variable’s value also will be changed in the calling module
Methods That Return A Value (Functions) • Functions must have a return statement followed by what is being returned • Declared as follows: Function Square(ByVal v As Integer) As Integer Return v^2 End function • Please pay attention to the type in the last portion of function heading
Shared Methods • Methods declared in a module are shared by default • If you want to share a method in a class, use the modifier Shared in the header of the method (instead of private or public) – write and writeline are shared methods • Class Math contains many shared methods
Subroutines • A Method that does not return a value • Pass values in and out through interfaces, ByVal/ByRef parameters • ByVal • A copy is passed • May pass actual values rather than variables • Original value is not changed • ByRef • Memory location is passed • Value stored in that location may change • Cannot pass a value, must pass a variable • Side effects may occur
Functions • Returns exactly one value through its name • Name must have an associated type
.Net Namespaces • System.windows.forms • Contains the classes required to create and manipulate GUIs • System.io • Contains classes that enable programs to input and output data • System.Data • Contains classes that enable program to access and manipulate databases • There are many others
Recursion • A method calling itself • The method only knows how to solve the simplest case (base case) • A more complex case is divided into two pieces, one base case and remaining as a complex case • Factorial of 5 is 5 times factorial of 4. • Factorial of 1 is 1 (base case)
Method Overloading • Multiple methods with the same name but different signatures • Meaning different numbers and types of parameters OR different ordering of parameters by type Functionsquare(ByVal value AsInteger) AsInteger Return convert.toInt32(value ^ 2) End Function Functionsquare(ByVal value AsDouble) AsInteger Return value ^ 2 EndFunction
More Examples (Optional Parameters) • May have one or more optional parameters (list after the required parameters) • Specify default value in case not passed in FunctionPower(ByVal base AsInteger,OptionalByVal exponent AsInteger =2) AsInteger Dim total AsInteger =1 For i AsInteger = 1 To exponent total *=base Next Return total EndFunction