0% found this document useful (0 votes)
23 views32 pages

VB Lect 02 - Intro To VB

Uploaded by

syabilmuqri8
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)
23 views32 pages

VB Lect 02 - Intro To VB

Uploaded by

syabilmuqri8
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/ 32

FBT0025 VISUAL PROGRAMMING

INTRO TO VISUAL BASIC


PROGRAMMING
Learning Outcomes
1. Explain and apply programming concepts using VB code.
2. Understand VB language
3. Design and write a basic VB command line application.
How to learn programming??
msdn.microsoft.com
https://msdn.microsoft.com/en-
us/library/aa716285(v=vs.60).aspx
Design-Code – Debug -Run
tutorialspoint.com &
IDE Reference etc

You
Listen & understand

Get your hands dirty with codes

Solve problem little by little, systematically


Introduction

• In this chapter we introduce


• Visual Basic programming
• We present examples that illustrate several important features of the language
• Console applications
• Applications that contain only text output
• Output is displayed in a command window
Simple Program: Printing a Line of Text

• Simple program that displays a line of text


• When the program is run
• output appears in a command window
• It illustrates important Visual Basic features
• Comments
• Modules
• Sub procedures
Single-quote character (') indicates
that the remainder of the line is a
comment

Visual Basic console applications


The Main consist of pieces called modules
procedure is
the entry point
of the
program. It is
present in all
console The Console.WriteLine
applications statement displays text
output to the console

Welcome1.vb

Program Output
A few Good Programming Practices

• Comments
• Every program should begin with one or more comments

– Modules
• Begin each module with mod to make modules easier to identify

– Procedures
• Indent the entire body of each procedure definition one “level” of indentation
Steps

1. Create the console application


 Select File > New > Project…
 In the left pane, select Visual Basic Projects
 In the right pane, select Console Application
 Name the project Welcome1
 Specify the desired location
2. Change the name of the program file
 Click Module1.vb in the Solution Explorer window
 In the Properties window, change the File Name property to
Welcome1.vb
Creating a Console Application with the New
Project dialog

Project
name

File
location
Editor window
(containing
program code)

• IDE with an open console application

IDE with an open console application


Code Skeleton

Module Module1
Sub Main() Code
‘START HERE
End Sub
End Module
Solution Explorer

Click
Module1.vb
to display its
properties

Properties window

File Name
property

Renaming the program file in the Properties window.


Simple Program: Printing a Line of Text

3. Change the name of the module


 Module names must be modified in the editor window
 Replace the identifier Module1 with modFirstWelcome
4. Writing code
 Type the code contained in line 7 of Fig. 3.1 between Sub Main() and
End Sub
 Note that after typing the class name and the dot operator the IntelliSense is
displayed. It lists a class’s members.
 Note that when typing the text between the parenthesis (parameter), the
Parameter Info and Parameter List windows are displayed
5. Run the program
 To compile, select Build > Build Solution
 This creates a new file, named Welcome1.exe
 To run, select Debug > Start Without Debugging
IntelliSense feature of the Visual Studio

Partially-typed member
Member list

Description of
highlighted member
IDE indicating a syntax error

Omitted parenthesis
character (syntax error)

Red underline
indicates a syntax
error

Error
description(s
Write() & WriteLine

Method Write does not position the output


cursor at the beginning of the next line

Method WriteLine positions the output


cursor at the beginning of the next line

Program
Output
Read(), ReadKey() & ReadLine()

• The above mentioned three methods are mainly used in Console


application and these are used for return the different values .
• If we use Read line or Read() we need press Enter button to come
back to code.
• If we using Read key() we can press any key to come back code in
application
Console – Basic Input
Imports System

Module Module1
Sub Main()
Console.ReadLine()
End Sub
End Module

ReadKey()
ReadLine()
Console – Basic Output

Module Module1
Sub Main()
System.Console.WriteLine("Hello world")
End Sub
End Module

System.Console
Console – Basic Output
Imports System

Module Module1
Sub Main()
Console.WriteLine("Hello
world")
End Sub
End Module

System.Console
Console – Basic Input & Store in Memory

Imports System

Module Module1
Sub Main()
Dim answer as integer
Console.Write ("Enter a number:")

answer = Console.ReadLine()
Console.Write ("You entered " & answer)
Memory name
Console.ReadKey()
End Sub
End Module
Console - Input Output (I/O)
Imports System

Module Module1
Sub Main()
Dim inputName as string

Console.WriteLine("Enter your name:")

inputName = Console.ReadLine()

Console.WriteLine("You have input {0}", inputName)


inputName ?
Console.ReadKey()
End Sub
End Module
{0}
Console Output - &

Imports System

Module Module1
Sub Main()
Console.Write ("Hello" & " " & "World.")

Console.ReadKey()
End Sub
End Module
Console Output – Clearing Screen
Imports System

Module Module1
Sub Main()
Console.Write ("Hello World.")
Console.Write ("Welcome ")
Console.Write ("to the bizarre world of programming.")

Console.ReadKey()

Console.Clear()

Console.ReadKey()
End Sub
End Module
Activity - Console Output
Sub Main()
Console.Beep()
Console.Write("Welcome to the world, ")
Console.Write("Adam." & Environment.NewLine)
Console.Write("As for you Eve, ")
Console.WriteLine("welcome to the bizarre world of VB programming.")
Console.Write("================================================")
Console.ReadKey()
Console.Clear()
Console.WriteLine("So, there you go...")
Console.ReadKey()
Console.Beep()
End Sub
Variables

x 1
y 2

Dim x As Integer = 1 z ?
Dim y As Integer = 2
Dim z As Integer
z = x + y
Console.WriteLine(z)
‘Console.WriteLine("Answer = {0}", z)
‘Console.WriteLine("Answer = " & z)
Text Size (How many characters)
Imports System

Module Module1
Sub Main()
Console.Write ("Enter your name:")

Dim inputName = Console.ReadLine()


Dim charSize = inputName.Length

Console.WriteLine("Your name {0} has {1} characters.", inputName,


charSize)

Console.ReadKey()
End Sub
End Module

String variable has Length property to tell the number of characters


in the String value.
Activity – Problem Solving
Write a program that prompts for two integers AND Algorithm (Flow chart):
display the average.

Analyze: IPO
I = two integers
P = find average equation Get integer 1,
O = average (float/double) x and integer
2, y

Algorithm (Pseudocode):
1. Start
2. Get integer 1 and store in variable x. Avg=(x+y)/2
3. Get integer 2 and store in variable y.
4. Find the average, avg = (x + y) / 2
5. Display avg.
6. End Display avg

Output
Coding
Declaration with keyword Dim
These variables store integers value

First value entered by user is assigned


to variable x

assigns result to variable avg

Format indicates that the argument


after the string will be evaluated and
incorporated into the string
Exercise

Write a Program to enable :


• User input two integers
(Whole numbers)
• Program computes the average
• Display result

You might also like