Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Visual Programming

Similar presentations


Presentation on theme: "Introduction to Visual Programming"— Presentation transcript:

1 Introduction to Visual Programming
XIV Lecture 4: Methods Introduction to Visual Programming Lecture 4 – Methods XIV – Ian elcoate Ian Elcoate - May 2007

2 Methods by Structure Two basic types of structured methods Sub Method
Can be used for any standalone block of code Header uses keyword: Sub Footer is always: End Sub Function Method Used when we know that the standalone block of code must calculate and return a result Header uses keyword: Function Footer is always: End Function Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

3 Methods by Generation Event Method Intrinsic Method (Built-in methods)
Created by double-clicking on a control at design-time E.g. btnExit_Click Structure: Usually a Sub Method (99.9%) Into whose body we write relevant code Intrinsic Method (Built-in methods) Already created and defined as part of VB.NET Structure: Can be either Sub or Function Methods We do not code them, but use them by calling E.g. CStr() User Defined Methods (UD) Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

4 User Defined Methods User Defined Methods (UD) Sub Methods
XIV Lecture 4: Methods User Defined Methods User Defined Methods (UD) Are created, coded and called by programmer Structure: Can be either Sub or Function Methods Sub Methods Also known as Subs or Procedures Self contained block of code Function Methods Simply known as Functions Basically a Sub with a hole in it i.e. should always returns a result Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - Ian Elcoate - May 2007

5 User Defined Sub Method
XIV Lecture 4: Methods User Defined Sub Method Format: Private Sub MethodName() ‘statements go here – in body End Sub Note: The MethodName should start in Uppercase Besides this, follow the variable naming rules / style If header is correctly typed, VBN will auto complete the footer for you, after pressing the Enter key Each word in Method name should start with a Uppercase letter Method name should not be same as a keyword or start with an number Where to declare method Avoid using same identifier already used, e.g. control or variable name. Type header and footer automatically follows Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - Ian Elcoate - May 2007

6 User defined Function Method
XIV Lecture 4: Methods User defined Function Method Format: Private Function MethodName() As Return-Type ‘Optional Statements . . . Return Return-Value End Function Note: Inside the function some return-Value is always returned, using the keyword Return Thus the function header must specify the data-type of this Return-Value, i.e. the Return-Type e.g. As Integer If you type the header correctly, VBN will auto complete the footer for you, after pressing the Enter key Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - Ian Elcoate - May 2007

7 Sub and Function examples
Sub Method Private Sub ShowCircleArea() lblArea.Text = CStr(Math.PI * 10 * 10) End Sub Function Method Private Function GetCircleArea() As Double Return (Math.PI * 10 * 10) Note the result of calculation is the return-value Thus return-type is specified as Double Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

8 Calling Subs and Functions
Both Subs and Functions are called by name A Sub Method call is a full statement in its own right ShowCircleArea() A Function Method call is an expression, i.e. it should be one part of a statement Usually the RHS of an assignment statement area = GetCircleArea() lblArea.Text = CStr(GetCircleArea()) When using functions, the LHS should be the same data-type as the RHS If not then remember to Convert the Function result Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

9 Parameters Parameters are the means by which a method or function receives extra information Parameters can be considered to be valve in the top of the code-block The data will only be allowed to enter, if it satisfies certain conditions Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

10 Parameter List Parameters use memory-space, thus need to be defined
This is done by specifying a parameter list Inside the round brackets of a function or method header Format: Private Sub MethodName(paramList) Private Function FunctionName(paramList) As Return-Type The paramList is simply a list of parameters There can be none, one or more parameters If there are no parameters then the round brackets are left empty Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

11 Defining Parameters Within the Parameter list, each parameter need to be formally defined On the most part this definition is very similar to variable declarations Except we do not use the Dim (or Private) Keywords Examples: Private Sub Display(pBalance As Double) Private Function Cube(pNum As Integer) As Integer Parameter names are prefixed with a lowercase p Besides this standard variable name rules apply By default VBN will precede each parameter with the ByVal keyword Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

12 Arguments For any parameter specified by a method / function
XIV Lecture 4: Methods Arguments For any parameter specified by a method / function The call must supply a corresponding argument Arguments are specified within the round brackets of the method / function calls The argument must be of same type as parameter Arguments must be provided in correct order Any value can be used as an argument Variables, e.g. result = Cube(num) Literal values, e.g. Display(7) Expressions, e.g. Cube(CInt(txtNum.Text)) Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - Ian Elcoate - May 2007

13 ByVal and ByRef Did you notice that we did not use the Dim keyword when declaring the parameter pRadius Instead parameters require either ByVal - Data Passed By Value ByRef - Data Passed By Reference If we use neither keyword then VBN assumes ByVal I.e. the parameter is assigned the value of the argument For a variable argument this means: The value stored in the variable argument will be passed to the parameter Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

14 Passing By Reference (1)
A ByRef parameter is not passed the (value of the) data stored by a variable argument Instead it is passed the memory address of the argument This is why ByRef arguments must be variables This means that the parameter and argument will both share the same memory address Note: Memory Addresses can only store one item of data Thus whatever is the value of the parameter at the end of the method Will also be the new value of the variable argument Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

15 Passing By Reference (2)
ByRef is a difficult concept to appreciate and as such is used in very specific circumstances When a method needs to return two or more results Customised validation Manipulating arrays For the time being lets consider the first option Functions return a result But only one value can be returned Making use of ByRef parameters allows for more than one value to be returned Note: to avoid confusion I will use ByRef parameters with Sub Methods only Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

16 Example use of ByRef A method to calculate and return the area and circumference of a circle The radius is provided as a ByVal parameter To return two results the area and circumference are provided as ByRef parameters Private Sub CalcCircle(pRadius As Double, ByRef pArea as Double, ByRef pCir As Double) pArea = Math.PI * pRadius * pRadius pCir = 2 * Math.Pi * pRadius End Sub Remember in the call, variable arguments must be used to match up with the ByRef parameters CalcCircle(radius, area, circumference) Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -

17 Summary Modular programming creates faster and more efficient applications This requires blocks of code to be written as distinct methods We have already used methods (i.e. Intrinsic and Event methods) Now we will also use Sub Methods and Function Methods We pass variable data to methods by means of an argument within the method call To receive this data, matching parameters must be declared within header of method Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid -


Download ppt "Introduction to Visual Programming"

Similar presentations


Ads by Google