0% found this document useful (0 votes)
36 views8 pages

فيجول٢

Uploaded by

karrarid900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

فيجول٢

Uploaded by

karrarid900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Basrah

Collage of Engineering
Department of Petroleum
Computer Programming

Fundamentals of programming in
Visual Basic

First Year

By

Assist Lect. Haider S. Mohammed


Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

2. Fundamentals of programming in Visual Basic


2.1 Managing VB2010 Data
In our daily life we come across many types of data. For example, we need to handle data
such as names, addresses, money, dates, stock quotes, statistics and more everyday.
Similarly, in Visual Basic 2010, we have to deal with all sorts of data; some are numeric
in natrure while some are in the form of text or other forms. VB2010 divides data into
differenttypes so that it is easier to manage when we need to write the code involving those
data. Visual Basic classifies the information mentioned above into two major data types;
namelythe numeric data types and the non-numeric data types.

2.1.1 Numeric Data Types


Numeric data types are types of data that consist of numbers, which you can compute
them mathematically with various standard operators such as add, minus, multiply, divide
and so on. Examples of numeric data types are your examination marks, your height and
your weight, the number of students in a class, share values, price of goods, monthly bills,
fees and more. In Visual Basic 2010, we divide numeric data into more than five,
depending on the range of values they can store. Calculations that only involve round
figures or data that do not need precision can use Integer or Long integer in the
computation. Programs that require high precision calculation need to use Single and
Double decision data types, we also call them floating-point numbers. For currency
calculation, you can use the currency data types. Lastly, if even more precision is requires
which involve many decimalpoints, we can use the decimal data types.

1. Integer: A variable of type integer requires 2 bytes of memory and can hold the
whole numbers from -32,768 to 32,767.
2. Long: A variable of type Long requires 4 bytes of memory and can hold the whole
numbers from -2x109 to 2x109.
3. Single: A variable of type Single requires 4 bytes of memory and can hold 0, the
numbers from 1.40129x10-45 to 3.40283x1038 with the most seven significant digits,
and the negatives of these numbers.

1
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

4. Double: A variable of type Double requires 8 bytes of memory and can hold 0, the
numbers from 4.94065x10-324 to 1.7976x10308 with at most 14 significant digits and
the negatives of these numbers.
5. Currency: The currency data type is particularly useful for calculations involving
money. A variable of type Currency requires 8 bytes of memory and can hold any
number from -9x1014 to 9x1014.

2.1.2 Non-Numeric Data Types


Nonnumeric data types are data that cannot be manipulated mathematically using
standard arithmetic operators. The non-numeric data comprises text or string data types,
the Date data types, the Boolean data types that store only two values (true or false),
Object data type and Variant data type.

1. Boolean: A variable of type Boolean requires 2 bits of memory and holds either the
value True or False. If boolVar is a Boolean variable, then the statement Print
boolVar displays (1) when the value is True and displays (0) when the value is False.
2. Date: A variable of type Date requires 8 bytes of memory and holds numbers
representing dates from January 1St 100 To December 31St 9999. Values of dateVar
are displayed in the form month/day/year (for example, 5/12/1996).
3. String: A variable of type string requires 1 byte of memory per character and can
hold a string of up to 32,767 characters, string values are enclosed in quotes.
4. Variant: A variable of type variant can be assigned numbers, Strings and several
other types of data. A variable of type variant requires 16 bytes of memory and can
hold any type of data. When values are assigned to a variant variable, Visual Basic
keeps track of the "type" of data that has been sorted. By default, Visual Basic uses
the variant data type.

2
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

2.2 Variables
Variables are like mail boxes in the post office. The contents of the variables changes
every now and then, just like the mail boxes. In term of VB2010, variables are areas
allocated by the computer memory to hold data. Like the mail boxes, each variable
must be given a name. To name a variable in Visual Basic 2010, you have to follow a
set of rules.

Variable Names

The following are the rules when naming the variables in Visual Basic 2010

 It must be less than 255 characters


 No spacing is allowed
 It must not begin with a number
 They can’t be the same as restricted keywords (a restricted keyword is a word that
Visual Basic uses as part of its language. This includes predefined statements such
as “If and Loop”, functions such as “Len and Abs”, and operators such as “Or and
Mod”).

Examples of valid and invalid variable names are displayed in Table 2

Valid Name Invalid Name


My_Car My.Car
ThisYear 1NewBoy
Long_Name_Can_beUSE He&HisFather *& is not acceptable
Table 2: Valid and Invalid Names

2.2.1 Declaring Variables

In Visual Basic 2010, one needs to declare the variables before using them by assigning
names and data types. If you fail to do so, the program will show an error. They are
normally declared in the general section of the codes' windows using the Dim statement.

The format is as follows:

Dim Variable Name As Data Type

3
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Example 2.1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

Dim password As String

Dim yourName As String

Dim firstnum As Integer

Dim secondnum As Integer

Dim total As Integer

Dim doDate As Date

End Sub

 You may also combine them in one line, separating each variable with a comma, as
follows:

Dim password As String, yourName As String, firstnum As Integer,.............

2.3 Constants

Constants are different from variables in the sense that their values do not change
during the running of the program.

2.3.1 Declaring a Constant

The format to declare a constant is

Const Constant Name As Data Type = Value

4
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Example 2.3

Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

Const Pi As Single=3.142

Const Temp As Single=37

Const Score As Single=100

End Sub

2.4 Assignment Statement


(Variable-Name=Expression): Expression may include constant, character, variable
(or variables), operators and functions. For example
City=”Baghdad”
Age=29
X=2*X
Z= A+B
m= Sin (d)

Suffix for Literals


Literals are values that you assign to a data. In some cases, we need to add a suffix
behind a literal so that VB2010 can handle the calculation more accurately. For
example,we can use num=1.3089# for a Double type data. Some of the suffixes are
displayed in Table 1.

Suffix Data Type Examples


% integer Dim A%
& Long Dim A&
! Single Dim A!
# Double Dim A#
@ Currency Dim A@
$ String Dim A$
6
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

3. Visual Basic Operators

1. The simplest operators carry out arithmetic operations. These operations in their
order of precedence are:

Operation Code Operation


^ Exponent
*, / Multiplication and division
\ Integer division
Mod Modulus-rest of division
-,+ Subtraction and addition

2. To Concatenate two strings, use the & symbol or the + symbol


3. There are six Comparison operators in Visual Basic.

Operation Code Comparison


> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
< > or > < Not equal to

4. There are three logical operators:

Operation Code Operation


Not Logical not
And Logical and
Or Logical or

Note: Logical operators follow arithmetic operators in precedence.

7
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Examples:

Public Class Form1

Private Sub Button1


Label1.Text = 3 / 2

Label2.Text = 3 \ 2

Label3.Text = 3 Mod 2

Label4.Text = 3 * 2

Label5.Text = 3 + 2

Label6.Text = 3 - 2

Label7.Text = 3 \ 2 * 4

Label8.Text = 3 * 2 \ 4

Label9.Text = (3 / 2) * 2

Label10.Text = 3 * 2 ^ 2

End Sub

End Class

You might also like