Beruflich Dokumente
Kultur Dokumente
Chapter Four
Data
Procedures
Dear Student, when adding a new form window, it creates a new (Class) with the
same form name .Within the scope of this (Class), we can declare Event
Procedures as well as variables and constants, that can be declared either within
the scope of the (Event procedures) or within the (Class), also we can declare
what so-called (Block construct), such as (If ... then) statement, and (For ... next)
statement, and others.
(84)
Chapter Four
Data
Procedures
There are two types of procedures in Visual Basic .NET: Sub procedures and
Functions procedures. Sub procedures do not return a value, while Functions
return a value.
You can declare a Sub procedure in a class; if we had a code that will be
repeated in more than one place in this Class; as well as for the organization of
this code, and so it will be easy to read and understand. And then modify it if
necessary.
Sub Name(Parameters)
Code
End Sub
(85)
Chapter Four
Data
Procedures
The Code written within each event procedure for (Button1_Click) and
(Button2_Click) is repetitive ; except for the starting value written in
each repetition ;where the value in the ( )button starts by (2), and
in the ( )button starts by (1) as shown in figure (4-1).
Dear student, use the Sub procedure to avoid code duplication as shown in
figure (4-3)
(86)
Chapter Four
Data
Procedures
(87)
Chapter Four
Data
Procedures
Declaring a Parameter
Using a Parameter
(88)
Chapter Four
Data
Procedures
(89)
Chapter Four
Data
Procedures
Dear student, you can develop the sub Procedure of name (ShowOddOrEven)
that receives the start value and end value in the iteration (repetition); as
shown in figure (4-10).
Declaring a Parameter
Using a Parameter
(90)
Chapter Four
Data
Procedures
You can also adjust the display of odd and even numbers; as well
as setting their range. For example, if the given values are (5, 35)
the odd numbers from 5 to 35 will be displayed .If the given values
are (8, 45) then the even numbers from 8 to 45 will be displayed
as shown in figure (4-12).
(91)
Chapter Four
Data
Procedures
(92)
Chapter Four
Data
Procedures
Figure (4-14) The code window where the function (sum) is declared
(93)
Chapter Four
Data
Procedures
3- Create an event procedure for Button1, and then type the code as shown
in figure (4-15).
The variables (x) and (y) have been declared and the input values
from a user are assigned to each of them in (TextBox).
The value of the (Sum) function has been assigned to the Property
(Text) of (Label4) control after receiving the two values (x), (y).
(94)
Chapter Four
Data
Procedures
4- Press (F5) key then input the values as shown in figure (4-16).
(95)
Chapter Four
Data
Procedures
(96)
Chapter Four
Data
Procedures
You call the function (Area); to calculate the area of a circle as shown in figure
(4-18).
(97)
Chapter Four
Data
Procedures
When calling the function; the displayed result will be (False) as the value
assigned to it is Five and cannot be converted to a numeric value.
(98)
Chapter Four
Data
Procedures
First: State whether the following statements are true ( ) or false (X)
1- The Procedure is declared once, while can be called any number
of times.
6- The Sub Procedure doesnt return any value, while the function
returns a value.
Function
Variables
Constants
Serial
B
Is used on the right side of
the assignment statement
and does not have any
value.
Is used on the right side of
the assignment statement
and a value is assigned to it
in declaration only
Is never used in the
assignment statement
Is used on both sides of the
assignment statement
(99)
Chapter Four
Data
Procedures
1- Procedure Name:
2- Parameters given to this Procedure and their types:
Fourth: From the demonstrated code extract the following:
1234-
Procedure Name:
Parameters given to this Procedure:
The return value:
The Data type for:
a- A function
b- Parameter given to a function c- the return value
1234-
Procedure Name:
Parameters given to this Procedure:
The return value:
The Data type for:
a- A function
b- Parameter given to a function c- the return value
(100)
Chapter Four
Data
Procedures
.....................................................
B-
.....................................................
C-
.....................................................
Seventh: Extract the names of the Functions from the following code:
.....................................................
.....................................................
(101)