Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.

Similar presentations


Presentation on theme: "CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011."— Presentation transcript:

1 CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011

2 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 2

3 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 3

4 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 4

5 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 5

6 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. 6

7 Declaring a Subroutine Sub methodName (parameterList) –Declarations and statements End Sub If you pass a parameter ByRef and if the value of that parameter is changed by the subroutine, that variable’s value also will be changed in the calling module 7

8 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 8

9 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 9

10 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 10

11 Functions Returns exactly one value through its name Name must have an associated type 11

12 .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 12

13 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) 13

14 See Recursion Examples 14

15 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 Function square(ByVal value As Integer) As Integer Return convert.toInt32(value ^ 2) End Function Function square(ByVal value As Double) As Integer Return value ^ 2 End Function 15

16 More Examples (Optional Parameters) May have one or more optional parameters (list after the required parameters) Specify default value in case not passed in Function Power(ByVal base As Integer, Optional ByVal exponent As Integer =2) As Integer Dim total As Integer =1 For i As Integer = 1 To exponent total *=base Next Return total End Function 16

17 17


Download ppt "CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011."

Similar presentations


Ads by Google