Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,833 members, 7,810,203 topics. Date: Friday, 26 April 2024 at 11:35 PM

Introduction To Programming Using Microsoft Visual Basic – Tutorial 2. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Introduction To Programming Using Microsoft Visual Basic – Tutorial 2. (1131 Views)

Learn The Fundamental Of Programming Using The Zoom App. / Learn Python- Introduction To Programming / Introduction To Programming Using Microsoft Visual Basic – Tutorial 01. (2) (3) (4)

(1) (Reply) (Go Down)

Introduction To Programming Using Microsoft Visual Basic – Tutorial 2. by binoosmart(m): 6:36pm On Apr 07, 2017
[img]https://4.bp..com/-6IKHxZs2sLg/WNlCNzk5VdI/AAAAAAAAEZI/lqFJluFP6ukb2aeKnGpgXWmNLJ1SWyiuQCK4B/s320/Introduction%2BTo%2BMicrosoft%2BVisual%2BBasic.jpg[/img]
welcome to Introduction To Programming Using Microsoft Visual Basic – Tutorial 2.

Introduction To Programming Using Microsoft Visual Basic – Tutorial 1
http://icict..com.ng/2017/03/introduction-to-programming-using.html

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 need variables. A variable is a storage container 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 Integer category 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 handy sometimes 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:

http://zetcode.com/lang/visualbasic/strings/
http://www.vbtutor.net/vb6/lesson13.html


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
Save the project as NameAnalizer under Lab02 Solution.

I tried as much as possible to give solution to the exercise for you in The first tutorial here ( http://icict..com.ng/2017/03/introduction-to-programming-using.html ), to read it now.

icict..com.ng/2017/03/introduction-to-programming-using_27.html

you can read the 3rd tutorial here http://icict..com.ng/2017/04/what-is-operators-in-microsoft-visual.html

1 Like 1 Share

Re: Introduction To Programming Using Microsoft Visual Basic – Tutorial 2. by Bros1: 8:04pm On Apr 07, 2017
Nice tutorials. Please add screenshot images and illustrations if you can next time.
Thanks

1 Like

Re: Introduction To Programming Using Microsoft Visual Basic – Tutorial 2. by samijay8(m): 8:57am On Apr 08, 2017
Don't know another way to say you're blessed

1 Like

Re: Introduction To Programming Using Microsoft Visual Basic – Tutorial 2. by codro: 10:39pm On Apr 09, 2017
nice

(1) (Reply)

I Need A Business Software Developed. Anyone? Can You Recommend Anyone? / Programmers In Awka / What Do You Guys Think Of This Website Design ?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.