Professional Documents
Culture Documents
Content
Page
1
Chapter I :Data
Preface
1-1 Data Types
1-2 Variables
1-3 Assignment statement
1-4 Constants
1-5 The Errors
1-6 Priority rules for Arithmetic operations
Questions
Chapter II :Branching
Preface
2
3
5
8
15
21
27
28
33
34
34
38
40
45
48
52
55
59
60
60
70
75
78
81
83
Chapter IV:Procedures
Preface
84
4-1 Procedures
85
85
INDEX
Content
Page
92
93
97
Questions
100
103
106
109
ii
Data
By the end of this chapter, student will be able to:
Chapter One
Data
Data
(2)
Chapter One
Data
Data
Each data type has a way in which data are stored in computer
memory (RAM).
This is mainly for using the computer memory optimally; in terms of
storage space and, arithmetic and logical operations that take place
on these values.
Visual Basic.Net Language provides many data types used to store input values
from the user, or values resulting from the execution of instructions and
commands of the a program.
Data Types provided by (Visual Basic.Net) Language are:
1- Numerical Data Types; that can be used to store numerical data and are
divided into:
a- Integral Numeric Types (such as: Byte- Long Integer Short).
b- Nonintegral Numeric Types (namely: Double- Single Decimal).
2- Character Data Types (namely: String Char).
3- Miscellaneous Data Types are those data that; do not fall under the
Numeric or the Character types (namely: Object Date Boolean).
(3)
Chapter One
Data
Data
(Visual Basic. NET) supports types,other than the standard Data Types.
As shown; the statement for setting the Property (BackColor) of the control
(Label1); is displayed in figure (1-3).
Object
Property
Value
(4)
Chapter One
Data
Data
Use the (Dim) keyword in combination with the variable name and data type;
write (Dim) then the name of the Variable then the word (As) followed by the
Data Type as shown:
Dim
Variable
As
Data Type
Dear student, when naming Variables, you must note the following:
1. Variable names must begin with a letter or underscore (_).
2. Variable names Should not contain symbols or special characters (e.g.:
(5)
Chapter One
Data
Data
Dear student, when choosing a Data Type, you must note the following:
1. The size of data that will be stored in the variable.
2. Identify the Data Type of the values stored in the variable; based on the
arithmetic and logical operations, expected to take place on the variable.
(6)
Chapter One
Data
Data
Expected values entered from the user and proposed names and, types as
shown in the table (1-1):
Value
Data Type
Proposed names
)String(
U_Name
)Date(
U_B_D
)Boolean(
U_Gender
)Integer(
U_C_F
(7)
Chapter One
Data
Data
Is a statement that consists of two sides (right hand side and left hand side)
separated by the assignment operator (=); (which doesnt mean the arithmetic
equality). It consists of taking the value on the right side of the assignment
operator (=) and storing it in the element on the left, as in the following
example.
Left side
Assignment
operator
Right side
(Variable)
Abstract value or
or
(Property)
The first line declares the variable (Number); specifying the type (Integer)
to be the data type.
The second line assigns the abstract value (5) to the variable (Number).
Dim Number1 As Integer
Dim Number2 As Integer
Number1 = 5
Number2 = Number1
The first two lines declare the variable (Number1) and (Number2);
specifying the (Integer) to be the data type.
The third line assigns the abstract value (5) to the variable (Number1).
The fourth line assigns the value of variable (Number1) to the variable
(Number2).
Dim Name As String
Name = Label1.Text
(8)
Chapter One
Data
Data
The first line declares the variable (Name); specifying the type (String) as
data type.
The second line assigns the value of the (Text) property of (Label1)
control; to the variable (Name).
Dim Number As Single
Number = 7
Number = Number / 3
The first line declares the variable (Number); specifying the (Single) data
type.
The second line assigns the abstract value (7) to the variable (Number).
The third line reassigns the variable (Number) using an Expression which
is: (Number /3).
Dim Number As Single = 5.6
You can declare the variable (Number) in one statement; specifying the
(Single) data type and a value of (5.6).
(9)
Chapter One
Data
Data
U_Name
U_B_D
U_Gender
U_C_F
In this case we will use Controls properties; that receive inputs from the user in
the assignment operation, where we do not change the values of the property
(Name) for these controls as illustrated in table (1-2).
Control
Property
TextBox1
Text
TextBox2
Text
RadioButton1
Checked
TextBox3
Text
(10)
Chapter One
Data
Data
Property
Variable
TextBox1.Text
String
U_Name
String
TextBox2.Text
String
U_B_D
Date
TextBox3.Text
String
U_C_F
Integer
properties
Table (1-3) illustrating the difference between Properties data types and
Variables data types used.
This difference in type is not allowed when the Assignment is done, but (Visual
Basic.Net) can convert the values to the data type that is compatible with the
variable or, the property to which the value will be assigned. This is called
Implicit conversions.
Dear student, if the variable is on the left side of the Assignment statement, this
indicates where values will be stored; if the variable is on the right side of the
Assignment statement this means that it reflects its value.
(11)
Chapter One
Data
Data
Dear student add the control (Label) after adjusting its properties as shown in
table (1-4) and figure (1-7).
Property
Value
AutoSize
False
Size,Location
BorderStyle
FixedSingle
(12)
Chapter One
Data
Data
Declaration of variables
Assignment values to
variables
Using variables
Figure (1-8) declaring variables and, assigning values then using them
(13)
Chapter One
Data
Data
After writing code, we press (F5); to perform (Start Debugging) and, we fill the
registration form then, we click the button ( ) as shown in figure (1-9).
(14)
Chapter One
Data
Data
They are places reserved in the (RAM) and, have data types; this is done during
the declaration: declaring constants then assigning values depending on their
data types. These values cannot be changed during program execution.
Use the (Const) keyword and, write the name of the Constant then, the word
(As) and constants Data Type then the (=) sign followed by the value of the
constant as shown:
Const
Constant name
As Data Type
= Value
Const pi As Single = 22 / 7
Where the constant (pi) was declared, of data type (Single) and, its numeric
value 22/7 is assigned during the declaration.
(15)
Chapter One
Data
Data
)
=22/7)
(16)
Chapter One
Data
Data
Dear student, we will write the code required to calculate the Area of a circle
using the Event Procedure (Button1_Click) and, the circumference of a circle
using the Event Procedure (Button2_Click).The results will be displayed in
control (Label2) when clicking on any of the two buttons.
In the scope of the Event Procedure (Button1_Click), we write the code as
shown in figure (1-13)
^ 2) is
(17)
Chapter One
Data
Data
Dear student, add the code required to calculate the circumference of a circle
in the scope of the Event Procedure (Button2_Click) as follows:
Radius = TextBox1.Text
Label2.Text = pi * Radius * 2
Knowing that the variable (Radius) and the constant (Pi) were
previously declared in the scope of the Event Procedure
(Button1_Click), accordingly messages inside a yellow rectangle
are displayed alerting that the (Radius) and the constant (Pi) are
not declared, although they are declared in the scope of the Event
Procedure (Button1_Click). Here it shows us that variables and
constants cannot be used except in the scope of their declaration
as illustrated in figure (1-15).
Figure (1-15) the code of calculating the area and circumference of a circle
(18)
Chapter One
Data
Data
The yellow rectangle appears when pointing with the mouse to the variable (Pi)
in the second line as shown in form (1-16):
Figure (1-17) the code after the declaration of the variable (Radius) and the
constant (Pi)
(19)
Chapter One
Data
Data
Figure (1-18) the declaration of the variable (Radius) and constant (Pi) on
the class level (Form3)
Where the declaration of the variable (Radius) and the constant (Pi) is done in
the scope of the class (Form3), therefore we can use them within the scope of
the Event Procedures (Button1_Click) and (Button2_Click) that are also declared
in the scope of the class (Form3).
After writing code, we press (F5) to perform (Start Debugging), then we input
the value of the Radius and, we click the button ( ) once and, the
button ( ) again as shown in figure (1-19).
(20)
Chapter One
Data
Data
Dear student, when writing code, it is a must to abide by the rules of the
programming language used; so when you write the code, several types of
errors occur including:
1- Syntax Error : happens when writing code incorrectly as illustrated in the
following examples
Examples
Din X As Single
The variable (X) was declared but there is a mistake in writing the word
(Dim)
Const X As Single
The constant (X) was declared but, its value is not assigned during the
declaration. To overcome this type of error, the (IDE) help us as it does
not allow any error of this type.
2- Logic Error: it leads to incorrect results when executing the program; and
happens if the expressions used in the assignment statement are built
improperly.
Example
When calculating the Area of a circle we used the following code:
(21)
Chapter One
Data
Data
When executing this code, it will run without giving any error messages and the
result will be incorrect, as it will perform the sum instead of the multiplication.
To overcome this type of error, you must review the written code, and test the
program with data already validated, to be sure there are no errors of this type
(Logic Error)
3- Runtime Error that appears during the execution
This type of error is found in lines of code, where the Assignment Statement is
written. For example when you assign a value greater than the Data Type used
or, assign a value that is not equivalent to the variables Data Type or constants
Data Type, and so the Compiler of the language cannot converted this Data
Type.
(22)
Chapter One
Data
Data
Write the following code to the (Click) event procedure of the Button ( )as
shown in figure (1-21)
Figure (1-21) the code written to calculate the sum of two numbers
We can declare the two Variables (Arabic) and (Computer) with Data
Type (Byte) which receives values starting from (0) and ends with
(255) .This means that the program user must comply with the limits
of those values and, in case of violation, error occurs during Runtime
(23)
Chapter One
Data
Data
The following error message appears to state that, it is not allowed to convert
the blank text value to the type (Byte) and, this occurs as we didnt enter the
degree of the (Arabic language) as shown in figure (1-23).
Figure (1-23) Error message during execution (when entering blank value)
The second case
Perform (Stop Debugging) by pressing the (
) icon, then (Start Debugging) by
pressing (F5), then click the ( )button after entering the text value
) ( in a text box.
The following error message appears which states that, it is not allowed to
convert the text value ( ) " to the type (Byte) and, this occurs as we
entered text values instead of numeric values as shown in figure (1-24).
Figure (1-25) Error message during execution (when entering value out of limit)
(24)
Chapter One
Data
Data
The oval shape on each error message indicates the type of error
which is not handled, and these (Runtime) errors can be overcome by
several ways including the following:
1. Visual Basic .Net provides the possibility of handling errors that likely
occur through the Try...Catch...End Try structure, written as follows.
Try
Catch ex As Exception
EndTry
The code that is most likely to show errors during the execution is placed
after the word (Try) and the error handling code is placed after the word
(Catch) as shown in figure (1-26).
(25)
Chapter One
Data
Data
2. Controlling the data input from the user, by placing controls and adjusting
their properties; through which user can input data correctly as described
in figure (1-27).
In the shown Form window, user can only choose one country from the
displayed list.
(26)
Chapter One
Data
Data
Label2.Text = x * Radius * 2
If there is more than one arithmetic operation such as:
5+3*2
Which is performed first the multiplication or addition?
The expression 5 + 3 * 2 is evaluated and the result equals 11. This was
computed by doing the multiplication first then the addition.
But if the parenthesis is used in the expression (5 + 3)* 2; expressions enclosed
in parentheses are evaluated first and the result equals 16.
The operations are executed upon the following order of evaluation:
1.
2.
3.
4.
(27)
Chapter One
Data
Data
First: In the following Form window, if it is required to store entries from the
user in variables. Define the corresponding Data Type for each input.
2
3
1.
2.
3.
4.
(28)
Chapter One
Data
Data
Second: In the following code, get the variable names and constants and
their Data Types.
.......................................................................................
.......................................................................................
.......................................................................................
.......................................................................................
Third: In the following code, determine the cause of the displayed error
.......................................................................................
.......................................................................................
.......................................................................................
.......................................................................................
(29)
Chapter One
Data
Data
Catch ex As Exception
EndTry
Fifth: Determine the type of error in the following, and then perform
the required error handling.
Code
error type
error
handling
Din x As Single
Const x As
Single
Sixth: When writing code the user needs to add specific comment that
will not be executed, so the code must be preceded by:
1. ...............................................................................
OR
2. ...............................................................................
(30)
Chapter One
Data
Data
Seventh: in the following code explain the lines of code, indicated by the
numbers as shown in the figure.
1) ...............................................................................
2) ...............................................................................
Eight: Write the order of execution of Arithmetic operations that follow:
Order
Operation
Exponentials.
(31)
Chapter One
Data
Data
Ninth: Evaluate the following Arithmetic expressions and get the results
(A) (5+3)*2
The result .
(B) 5+3*2
The result.
Tenth: State whether the following statements are true () or false (X)
1- One of the Rules for naming variables or constants in the
program is: variable names must begin with a letter or a number.
4- Variables of types (Integer & Long & Double) are used to store
integers only.
(32)
Branching
By the end of this chapter, student will be able to:
Use If Then statement.
Use If ThenElse statement.
Use If Then ElseIf statement.
Use Select Case statement.
Chapter Two
Data
Branching
End if
The (If.Then) structure tests a condition, or a conditional expression written
after (If); if the result is (True) it executes the statements that follow (Then) until
reaching (End if); and, if the result is (False) it executes the statements following
(End If) as shown in figure (2-1).
If X >= 50 Then
X>=50
Msgbox(")"
End if
End
(34)
Chapter Two
Data
Branching
Equal to
<>
Not Equal to
>
Greater than
<
Less than
>=
<=
False
True
as the two values are not equal
DEGREE=Textbox1.Text
True
(35)
Chapter Two
Data
Branching
Create a new project so that the Forms window will be as shown in figure
(2-2)
Start
Enter X
X>=50
Msgbox
""
End
(36)
Chapter Two
Data
Branching
When you enter any value less than 50, the MessageBox does
not appear because the result of the condition is (False), so the
statement after (End if) which is (End Sub) will be executed; to
terminate the procedure.
This (If) statement can be written, in one line without writing
(End if) as follows:
(37)
Chapter Two
Data
Branching
code
Else
End if
start
Enter X
X>=50
Msgbox
Msgbox
""
""
End
(38)
Chapter Two
Data
Branching
Get N
command Button
N
divisible
by 2
End
(39)
Chapter Two
Data
Branching
We tested the value of (N); through the conditional expression (N Mod 2); If it is
divisible by 2 without remainder. Where (Mod) is a mathematical operator that
computes the remainder of a division expression; when the remainder of a division by
2 equals zero, this means there is no remainder; therefore it is an even number.
The (If ThenElse) statement is used when a single condition is tested, the
results of the condition is either (True) or (False), but the (If ...ThenElseIf)
statement is used when you want to check more than one conditional
expression.
code
Else
code
End if
as
shown in figure (2-3).
(40)
Chapter Two
Data
Branching
The form accepts a users input (of a number of temperature degrees) in the
textbox (TextBox1). Then by clicking on the command button (), it shows
the messages or or in (Label1) according
to the flowchart shown in figure(2-4):
start
Get D
D =0
Output
""
Output
""
D<0
Output
""
end
(41)
Chapter Two
Data
Branching
(42)
Chapter Two
Data
Branching
(43)
Chapter Two
Data
Branching
The result
MsgBox IsNumeric("youssif")
(False)
Since the value between the
parentheses is non-numeric
MsgBox(IsNumeric(10))
(True)
Since the value between the
Parentheses is numeric
MsgBox IsNumeric(a)
(True)
If the value of the variable (a) is
numeric
MsgBox
IsNumeric(textbox3.text)
(True)
If (TextBox3) contains a numerical
value
Original code
(44)
Chapter Two
Data
Branching
We may need to test more than a conditional expression together in the same
(If) statement, in this case we link between extra conditions by using the logical
operators (And) or the logical Operator (Or).
The logical Operator (And) . If both conditional expressions are (True),
then the result is (True).
The logical Operator (Or) . If either conditional expression is (True), then
the result is (True).
Examples of using the logical Operator (And) assuming that variable (A) and
variable (B) are Boolean variables.
Conditional expression
Result
TRUE
FALSE
FALSE
FALSE
(45)
Chapter Two
Data
Branching
Examples of using the logical Operator (Or) assuming that variable (A) and
variable (B) are Boolean variables.
Conditional expression
Result
A= TRUE OR B= TRUE
TRUE
A=TRUE OR B=FALSE
TRUE
A= FALSE OR B= TRUE
TRUE
A= FALSE OR B= FALSE
FALSE
as
shown in figure (2-6).
(46)
Chapter Two
Data
Branching
One of the two degrees is greater than or equals 50 then the word is
displayed.
Both degrees are less than 50 then the word is displayed
The code becomes as follows
(47)
Chapter Two
Data
Branching
Case value2
code
Case value3
..
..
code
Case else
code
End Select
(48)
Chapter Two
Data
Branching
start
Get D
Case D
Output
""
Output "
"
Output
""
End
Figure (2-7) the Flowchart when using (SelectCase) for exercise (2-7)
Figure (2-7) illustrates more than two possible paths (branches) out of the
decision point; which is a comparison that evaluates a question about the
value of variable (D); and according to this value the branching of code
differs.
The code becomes as follows:
(49)
Chapter Two
Data
Branching
as
shown in figure (2-8).
Listbox1
Label1
Label5
Label2
Label6
Label3
Label7
Label4
(50)
Chapter Two
Data
Branching
(51)
Chapter Two
Data
Branching
1- In science, you have learned that for each element there is an atomic
number, a symbol and a mass number, create a (Project) containing a (Form)
as shown in figure (2-9):
Groupbox1
Label4
RadioButton1
RadioButton2
Label5
RadioButton4
Label6
RadioButton4
Selecting one of the elements shown and, clicking on the command button,
will display the corresponding atomic number, symbol and mass number of
this element.
Write the code in (Click) event procedure for the command button as
follows:
(52)
Chapter Two
Data
Branching
ComboBox1
Textbox1
(53)
Chapter Two
Data
Branching
1.
Set the value of the property (Multiline) for the (TextBox) to true.
2.
3.
We have assumed when writing (Code) , the organization of items within the
(ComboBox) where the first item has an (index) 0, the second item has an
(index) 1 and the third item has an (index) 2, ..and, so on.
(54)
Chapter Two
Data
Branching
First: State whether the following statements are true (T) or false (F):
(1)
)2(
In (IF) statement the code that follows (Else) is executed when the result
( )
)3(
( )
)4(
Using the comparison operator (Or), the results of all conditions must be
( )
)5(
The (Select Case) statement is used to test more than one conditional
( )
)6(
If the value of variable (X) is 15 and value of variable (Y) is 350 then the
( )
)7(
( )
)8(
The (and) operator requires that, the result of the two conditions must be
( )
)9(
statement is 2.
of the conditional expression is (True).
)10( The (Select..Case) statement is used if there are more than two possible
branches.
(55)
( )
( )
( )
Chapter Two
Data
Branching
Second:
The following code calculates the area of a circle of radius (R).Rewrite
this code in left column so that; the message will be
displayed; when you enter a negative number in (TextBox1); whose
value is assigned to variable (R).
(56)
Chapter Two
Data
Branching
Write down the result displayed in (Label1); when you enter the following
values (degrees) in (Textbox1).
Degree
30
Result
84
64.4
-33
zero
114.3
Fourth: The following code is written to solve the first degree equation (y = 3x
+ 2) and gives correct results when you enter numeric values in the (TextBox) ;
but an error occurred while running (Runtime Error) when you entered string
values in the (TextBox). Write the code after solving this problem using an
additional (If) statement, that displays the message " " in a
(label ) control.
(57)
Chapter Two
Data
Branching
The code
The result
a. One
b. Two
c. three
a. True
b. False
c. 15
a. Youssif
Ahmed
b. Ahmed
Youssif
c. Ahmed
a. AXB=63
b. 7X9=63
c. 7X9=A*B
a. First
b. Second
c. Forth
(58)
Chapter Three
Data
The (For...Next) statement should start with the Keyword (For) and end with the
Keyword (Next). A variable is declared in the loop (it is called counter variable);
it has a start value and end value. The statements are repeated inside the loop
until the counter value equals the end value.
We will be skilled on using the For...Next statement; through the following
progressives exercises:
(60)
Chapter Three
Data
as
shown in figure (3-1).
Start
M=1
Fals
e
M<=3
Tru
e
msgbox
M
M=M+1
End
(61)
Chapter Three
Data
Msgbox M
Next
Msgbox M
Next
0
1 (The starting value )
1
The increment of M with 1 (M=M+1)
is the value of M greater than the end value (the
result is, False)
2
The increment of M with 1 (M=M+2)
is the value of M greater than the end value (the
result is, False)
1
-
3
The increment of M with 1 (M=M+3)
is the value of M greater than the end value (the
result is, True)
3
-
2
-
(62)
Chapter Three
Data
(63)
Chapter Three
Data
Figure (3-4) the Form window after clicking the button in run mode
Click the (Button) again you will find that the numbers from 1 to 3 are
added once again as shown in figure (3-5).
Figure (3-5) Numbers in the TextBox after you click the button again
So this statement (Me.TextBox1.Text = "") is added before the loop to clear
the contents of the TextBox.
You can also display numbers within (TextBox) so that each number can
appear in a new line by following:
Change the property (Multiline) to (True) in design mode; to allow more
than one line.
Modify the code inside the loop by adding a key input as follows:
(64)
Chapter Three
Data
String constant used to add: the (enter key) symbol and a new line
vbCrLf=Visual Basic Carriage Return Line Feed
(65)
Chapter Three
Data
1. The
variable
name
(product)
is
declared;
to
store
the
4. You can write the code without using these variables as follows:
Start running the program and, click the command Button, the
multiplication table of number (3) will be displayed as shown in figure (37).
(66)
Chapter Three
Data
Figure (3-8) the multiplication table of any number you enter in a (TextBox)
(67)
Chapter Three
Data
Note that number 3 is replaced by the variable (NUM); which contains the
value of the (TextBox).
In previous exercises, the starting value of the counter was always smaller than
the end value, and the increment of the variable was always equal to 1. Using
the (Step) keyword, you can increment or decrement the counter through the
loop; by the value you specify.
To decrement the counter variable, use a negative (Step) value. When you do
this, you should specify an end value that is less than the start value.
The start or end or step values can be integers or decimal numbers or variables
and the syntax is as follows:
(68)
Chapter Three
Data
will be taken.
For example, if you do not write the keyword (Step), by default
To display the
odd numbers
from 1 to 10
To display the
even numbers
from 2 to 10
To display the
numbers
divisible by 3
from 3 to 20
To display the
even numbers
descending from
10 to 1
To display the
numbers from
1.50 to 0.5
decremented by
0.05 each time
To display the
numbers from 1
to the value of B
at increasing
value of C
(69)
Chapter Three
Data
We have learned how to repeat a set of statements many times using the
(ForNext) statement .But the (Do While...Loop) is used to execute statements
for an undefined number of times; until a certain condition is met. This is useful
if you do not know the number of iterations (repetitions) ahead. For example
the process of entering names in a (TextBox) is repeated a number of times
while; the exit from this loop can be done by entering a particular word in the
Textbox.
The (Do While . . . Loop) keeps executing the code; as long as the conditional
expression is (True).
We will practice how to use this statement; through the following exercises:
(70)
Chapter Three
Data
as shown
in figure (3-9).
Listbox1
Button1
(71)
Chapter Three
Data
Conditional expression is
False in the case of equality
(True) as long as the value of (X) does not equal the word
" .
(72)
Chapter Three
Data
The shown condition implies that: when the value of variable (X) equals the
word
then the statement (Exit Do) will be executed; which means the
immediate exit from the loop, without waiting for the test of conditional
expression in (Do..While).
Try running the program again to verify the results of execution.
(73)
Chapter Three
Data
You should write the word in the InputBox with the same
You can also add other words, for the exit of the loop , as we
already did e.g. :
Do While X< > " " And X< > " " And X < > " "And
X < > ""
word" "And, not equal to the word " " And, not equal to
the word " " And, not equal to the word " " .
(74)
Chapter Three
Data
Enabled
False
Interval
100
Label1
(75)
Chapter Three
Data
Label1 control
(76)
Chapter Three
Data
The result of the property (Now) is assigned directly to property (Text) of Label1.
Because the property (Now) contains The Date and Time of the computer.
Start running the program; the date and time appear on the form after
1000 millisecond or (1 second), and the new values of date and time
reappear after the next second and so on.
Then the code written for the event (Tick) displays the Date and Time, every
second, it shows the change in seconds and looks like a digital clock.
The (Label) control displays only the time, if the property (Now) is replaced by
the property (TimeOfDay) as shown:
(77)
Chapter Three
Data
1. Create a new project so that the form window contains a (Label) control
containing the text " "and two Command Buttons .When you run the
project; the (Label) appears for half a second then disappears for half a
second and so on. When you click the Button, the (Label) appears
permanently without disappearing. When you click the Button the
(Label) reappears and disappears again as shown in (3-14):
Figure (3-14) the user interface required to show or hide the (Label) control
Add to the Form the Timer control and adjust its properties as
required.
Insert the appropriate code in the Timers Tick event handler to
maintain the appearance and disappearance of the Timer as follows:
First Method
Second Method
(78)
Chapter Three
Data
First Method
Second Method
Write the appropriate code in the click event handler of the Button
that display the even numbers you can do it yourself.
3. Perform the modifications required on exercise 2 to display the numbers
in Descending order instead of Ascending.
(79)
Chapter Three
Data
the
sum
of
odd
Figure (3-16) the user interface to display the sum of odd or even numbers
First Method
Second Method
Write the appropriate code in the click event handler of the Button
(80)
Chapter Three
Data
(81)
Chapter Three
Data
Second: State whether the following statements are true () or false (X)
1- The command Msgbox.3 & X & 3 displays a message box
containing number 9.
2- The value of (M) after the execution of the loop (For M=3 to 1
Step -1) is 0.
5- You can exit from the loop (Do ...While) based on the conditional
expression.
Third: What will be the value of the variable (r) after the execution of each of
the following code?
( 0.5 1 1.5 )
( overflow 2
( 10 5
(82)
0 )
0 )
Procedures
By the end of this chapter, student will be able to:
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)
Chapter Five
Cyber bullying
By the end of this chapter, the student will be able to:
Chapter Five
Data
Cyberbulling
PREFACE
Ethics related to the Internet and all means of information and communication
become one of the topical issues of interest that concerns individuals,
communities and countries.
So in this chapter we look for raising the awareness of our sons towards the
importance of ethics dealing with the Internet, and provide them with
information and skills required to upgrade their personnel safety regarding
online cyber bulling.
Through the Internet we can learn ..., entertain...., communicate and talk
together.
But:
We can also acquire wrong information and know bad people.
An intentionally aggressive behavior using electronic media for the purpose of:
1. Harassment
2. Annoyance
3. Embarrassment
4. Intimidation
5. Threat
6. .etc. .
Electronic Mail
Electronic forums
Instant Message
Blogging
Social media sites such as Facebook
(2)
Chapter Five
Data
Cyberbulling
1- AMR said to Yasmin I want to send a message to a friend and I have no email account. Could you give me your user name and password so that I
can send the message?
In your opinion, what can she do?
Answer: She advises Amr not to share a password.
(3)
Chapter Five
Data
Cyberbulling
2- Amr has created his own e-mail account. Yasmin tried finding his
password by writing his name, age and, birthdate then the account was
opened.
In your opinion, what should he do?
Answer: He should set a new password that cannot be deduced.
4- A student complained to his teacher that his friend insulted him in an email message. The teacher said: show me that message; the student said
that he has deleted it. The teacher said: you lost your evidence.
In your opinion, what should he do?
Answer: He should not delete cyber bulling messages.
5- Amr said to his father I came to know someone in a chat room and he
asked me for a meeting.
In your opinion, what was his fathers answer?
Answer: Not to interview anyone you met through the Internet.
6- A dispute happened between two students, and then one of them was
very angry and sent an intimidated and threatening letter to the other.
In your opinion, what should he do?
Answer: He has to be more patient.
(4)
Chapter Five
Data
Cyberbulling
7- A student uses the Internet for doing school activities, through working in
groups with his colleagues and whenever he uses the net , he receives
abusive chat or instant messages .To protect himself from such attack, he
reduces the use of the Internet to avoid such abuse.
In your opinion, what he should do?
Answer: He has to ask his parents for helping him to overcome that
problem.
8- Yasmin has downloaded one of the free programs on her computer; as a
result, dangerous computer virus infections, to the data stored and
belong to Yasmins father, appeared.
In your opinion, what should she do?
Answer: She has to consult her father first, before installing any
program.
(5)
Chapter Five
Data
Cyberbulling
First: State whether the following statements are true () or false (X)
1- Cyber bullying is an intentionally, aggressive behavior using
electronic media for harassment, or aggravation, or
embarrassment, or to intimidate or threaten others.
Second: Put ( ) next to the statements that will help you protect yourself from
cyber bullying.
1- Do not share a password with others.
attackers.
5- Do not reply to cyber bullies, and do not believe what they
write online.
6- Report about cyber bullying to the competent formations.
(6)
Chapter Five
Data
Cyberbulling
Related topics that have already been studied and you can search for
them:
Identification.
Firewall.
Secret codes.
Request for assistance
(7)
Sample Tests
kl
First :State whether the following statements are true (T) or false (F):
1. The number of possible branches when using (If ...Then..Else)
3. If the value of variable (X) is 5 and value of variable (Y) is 7, then the
statement is 2.
a) 3.5 minutes
b) 3.5 seconds
c) 35 seconds
b) If End If
c) Do While...Loop
4- What is the value displayed in the Label control (LblOut) when running the
following code.
Dim i as Integer
For i = 2 To 10 Step 2
lblOut.Text = lblOut.Text & i & ","
Next
a) 2
b) 2,3,4,5,6,7,8,9,10
) 103 (
c) 2,4,6,8,10
Sample Tests
kl
1- Specify the value of each variable after executing the following Code.
Variable
Value
Dim N, K, C As Integer
C = 10
For N = 1 To 10 Step 5
K = K + N
Next
If N >= C Then
C = C + 1
End If
C
K
N
Order
Operation
Exponentials.
) 104 (
kl
Sample Tests
1-
2
) 105 (
Sample Tests
kl
First :State whether the following statements are true (T) or false (F):
1. Use the (If.Then..ElseIf) statement when testing more than one
2. Variables of types (Integer & Long & Double) are used to store
3. The (Or) operator requires that, the result of all conditions must be
4. The (Select Case) statement is used to test more than one conditional
conditional expression
integers only.
b) AddSum() = X + Y
c) X = AddSum()
2- For ending a loop; while the condition: (A = 2 ) is reached , the beginning of the
loop will be written as follows :
a) Do While a = 2
Dim J As Byte
For J=1 to 500 Step 3
ListBox1.Items.Add( J )
Next
a) You must use the Do. ..While rather than For ...Next
b) It is an endless loop
c) Error in variable type in the loop
) 106 (
Sample Tests
kl
4- The iteration in the For ..Next statement ( For m=10 to 1 Step -2 ) stops
when the value of the variable M:
a) Is greater than the end value
b) Is smaller than the end value
c) Is greater than or smaller than the end value
5- Select the result of the following Arithmetic expression: 2- 2 * (7+2)
a) -16
b) 14
c) 5
End If
If x > 0 Then
Label1.Text =
""
End If
If x < 0 Then
Label1.Text =
""
End If
End Select
) 107 (
Sample Tests
kl
Serial
Sub Procedure
Function
Variables
Constants
Serial
3- Select the type of error in the following Code and then correct as follows
Code
Error type
Dim x As Sangle
Const x As Integer
X=10
) 108 (
Correction
Sample Tests
kl
First :State whether the following statements are true (T) or false (F):
1. The property (SelectedIndex).determines the selected item in a
3. In the If statement (If A>B And B>C..Then ) the result is (True) when
5. The Variables of type (Integer & Long) are used to store integers only.
(Listbox).
2. The (Select Case) statement is used if there are more than two
possible branches.
(A=3,B=5,C=2).
Dim N As Integer
Dim S As Integer
For N = 1 to 100
S=S +N
Next
Label1.Text = S
a) Displaying the numbers from 1 to 100.
b) Displaying the sum of the numbers from 1 to 100.
c) Displaying the product of the numbers from 1 to 100.
2. What is the value displayed in (Label1) control; when executing the following
code:
b) 0
c) 4
) 109 (
Sample Tests
kl
3- For ending a loop; while the condition: (A < > 2 ) is reached , the start of the
loop will be written as follows :
a) Do While a = 2
.....................................................
.....................................................
Third: Answer the following questions
1- Specify the value of each variable after executing the following Code.
Bearning in mind that the primary
values for the variables are as
follows:
x = 0, z = 0, j = 1, k = 1, y = 2
Variable
Value
X
Y
If X<Y
Z = K
X = X
Y = Y
Else
Z = K
X = X
Y = Y
End If
Then
+ J
+ 1
- 1
- Y
+ 2
- 2
) 110 (
Sample Tests
kl
1- Procedure Name:
2- Parameters given to this Procedure:
3- The return value:
4- The Data type for:
a- A function b- Parameter given to a function c- The return value
.......................................................................................
.......................................................................................
.......................................................................................
.......................................................................................
) 111 (