welcome
to Introduction To Programming Using Microsoft Visual Basic –
Tutorial 2. did you miss the first Tutorial don't worry you can
click here to read it now below link:
Objectives,
Below are what i m expecting you to know by the end of this tutorial:
- Understand how to declare and initialize variables and constants
- Understand the different primitive data types that exist in Visual Basic
- Learn how to use Strings
- Learn how to write a program that receives input from the user
Variables
and Data Types in Visual Basic
In
order to make use of data in your program, you needvariables.
A variable is a storagecontainer for your data. Depending on the
purpose and size of information you want to work with, data are
classified into various categories called datatype.
Variables
are created to hold values from a specific data type. Here is an
example:
Dim
studentAge
As Integer
In
the statement above, studentAge
is a variable created as a container for Integercategory
of data. That means it is capable of accommodating whole numbers
only.
There
are 15 data types defined in Visual Basic. These data types are
called primitive
data types.
Boolean
|
Integer
|
Double
|
Byte
|
UInteger
|
Decimal
|
SByte
|
Long
|
Char
|
Short
|
ULong
|
String
|
UShort
|
Single
|
Date
|
For
more information, Visit the MSDN Microsoft site here
http://msdn.microsoft.com/en-us/library/47zceaw7.aspx
Declaring
Variables
To
make use of variables, you first have to create them. This process is
called declaration.
To create variables use statement like this:
Dim
varNameAsdatatype
Dim:
Short for Dimension. It is a keyword for creating variable.
varName:
Place holder for name of the variable. A variable name can be any
valid identifier.
An identifier is any series of characters consisting of letters,
digits and underscores (_). Identifiers cannot begin with a digit,
cannot contain spaces, cannot be a keyword, and cannot contain any
special character. Some valid examples of variable names are
studentName,
candidate1, second_semester.
But2Semester,
student Name, MyClass
are not valid variable names.
As
datatype:
Tells the computer what type of data will be stored in the variable.
Examples:
Dim
courseName As String
Dim
grade As Char
Dim
score As Double
Dim
isPromoted As Boolean
...
Note:
- Variables of different type can be declared in one statement with each variable declaration separated by comma. The last example can be written as:
Dim
courseName As String, grade As Char, score As Double, isPromoted As
Boolean
- Variables of the same type can be declared in one statement with each variable in the declaration separated by comma. For example:
Dim
score, cgpa As Double
Initializing
Variables
Variables
are not just declared (created). They are meant to contain some data
(values). Data are stored in variables by assignment
statements.
Here is an example:
Dim
regNo As String
regNo=”D13CS1003”
In
the statements above, the first statement declares a variable regNo
that is capable of holding String
data. The second statement (in bold) assigns the string “D13CS1003”
to the variable declared previously. The statement is read as regNo
gets the value “D13CS1003”.
Notice
the “=” binary operator. This is called the assignment operator
(more on this in the next Lab).
You
can initialize a variable at the same time you declare it. It’s
very handysometimes and always easy to do. For example the previous
example can be written as:
Dim
regNo As String=”D13CS1003”
Constants
Constants
are different from variables in the sense that their values do not
change during the running of the program.
The
format to declare a constant is:
ConstConstantNameAsDataType
= Value
Examples:
Const
Pi As Single=3.142
Const
Temp As Single=37
Const
Score As Single=100
Complete
the following table:
Variable
Declaration
Or
Assignments
|
Correct
or Incorrect?
If
Incorrect, why?
|
Correction
or Better Choice if any (whether correct or incorrect)
|
Dim
Name as Integer
|
||
Dim
m_FirstName as Integer
|
||
Dim
m_Money Earned as Integer
|
||
Dim
f_Currency as String
|
||
Dim
f_ Person as String
|
||
Dim
m_Name as String
|
||
Dim
GoldbachNo as Integer
|
||
Dim
m_PizzaOrder
|
||
Dim
!Factorial as Double
|
||
Dim
m_Chemicalppms as Double
|
||
Dim
x, y, z as Integer
|
||
Dim
m_Num1 as Integer, m_Num2 as Integer
|
||
txtCarPayment.txt
= m_CarPayment
|
||
TextOrderNum.Text
= m_OrderNum
|
||
txtCarPayment.txt
= m_CarPayment
|
||
Distance
= txtDistance
|
||
print
"Show me the $ "; m_Money "!!!"
|
||
m_FibonacciNum
= "45"
|
Strings
Strings
are the most important data types in computer languages. String (or
string literal) is one among the primitive data types in Visual
Basic. A string is a sequence of characters (including space
character) enclosed by double quotes (“ ”).
String
Concatenation Operator
Two
or more strings can be merged to form one string by using the string
concatenation operator (& or +). The concatenation operator can
also be used to join a string literals and variables that can be
converted to string.
For
example:
“Welcome
to ”&
”Visual Basic Programming”
becomes
“Welcome
to Visual Basic Programming”
“Welcome
to Visual Basic ”+regNobecomes
“Welcome
to Visual Basic D13CS1003”
Creating
the Greetings project
Let’s
put into practice what we have just learnt above by creating an
application that welcomes you to visual basic programming.
- Open Microsoft Visual Studio IDE if you have not yet done so.
- Create a project called Greeting under a solution called Lab02. Remember how you created a project onlab01.
- Rename Module1.vb file in the Solution Explorer to GreetMe.vb
- In the Main Subroutine, declare a variable called stdName that can hold String data.
- Initialize stdName to hold your name.
- Output a greeting message with the variable declared previously included in the output. (Hint use & operator).
- Save and execute your application.
String
Methods
There
are several String methods. Consider this code snippet:
Dim
stdName As String
stdName=”John
Doe”
From
the above statements, stdName is a variable of type String which is
initialized to “John Doe”.
To
convert all characters in stdName variable to lowercase, we write;
stdName.toLower()
To
convert all characters in stdName variable to uppercase, we write;
stdName.toUpper()
To
check if the variable contains the character ‘e’ we write;
stdName.contains(“e”)
this
will return true (Boolean) if stdName contains the string ‘e’
else it will return false.
To
check the position of a character say, “o”, we write;
stdName.indexOf(“o”)
this
will return 1. Why?
To
check the last position of a character say, “o”, if it occurs
more than once, we write;
stdName.lastIndexOf(“o”)
what
will be the output of above statement?
There
are other string methods available. You can explore some (but not
all) of them via these resource links:
Program
that receives input from user
So
far you have been writing programs that get its data from variables
declared within it. It is possible to get data from various source
including files, keyboard, network etc.
Let’s
rewrite the previous program but in this case, it will get your name
from user input via keyboard.
- Create a project called DynamicGreetings in Lab02 Solution.
- Rename the default module created by the IDE to GreetMe.vb
- In the Main Subroutine, enter the following code:
Dim
stdName As String
Console.WriteLine(“Please
enter your name: ”)
stdName=
Console.ReadLine()
Console.WriteLine(“Welcome
to Visual Basic Programming ” &stdName)
- Save the project using Save All command.
- Run the program.
Notice
the use of ReadLine() method of Console class. The ReadLine() method
causes the project to block until the user enters a value and press
enter key. ReadLine() returns the value entered as string datatype.
Exercises
- Write a program that prompt a user to enter the year of admission into Diploma program then outputs the expected year of graduation. For example if the user enters 2010, then the expected date of graduation will be 2012 (i.e. 2010 + 2).
Save
the project as GraduationCalculator
under Lab02 Solution
- Write a program that prompts a user for his/her name. it should the name in:
- Lower case
- Upper case and
- Length of the name (i.e. number of characters)
Hint:
use name.length().toString()
function
I tried as much as
possible to give solution to the exercise for you in The first
tutorial here click, to read it now. And to check if you are correct
please if you like this tutorial share with your friends and keep
coming back.
- Introduction To Programming Using Microsoft Visual Basic – Tutorial 2.
- Introduction To Programming Using Microsoft Visual Basic – Tutorial 3
Comments :
Post a Comment