0% found this document useful (0 votes)
2 views

VB.NET Part2.ppt

Uploaded by

georgecrust93
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

VB.NET Part2.ppt

Uploaded by

georgecrust93
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Visual Basic.

NET
J.V. Joshua
Type of Applications
• Types of Applications
• Console Application – Usually Text only application. Runs from a DOS or
COMMAND PROMPT screen.
• Windows Application – Graphical Interface user application. Typical
Windows application that you normally use, such as MS WORD etc.
• Web Based Windows Application – Web application that uses a Browser
such as Internet Explorer, Mozilla Firefox, chrome etc.
Type of Applications cont.
• Console application:
• Output is text based
• No graphics or forms
• Require little or no user interaction
• Can call window forms
• Lighter, less overhead than standard windows because of absence of graphics
• Good for login scripts, device drivers, backend processes, test programs etc
• Placed or control from command prompt window
Type of Applications cont.

Fig. Console application window


Type of Applications cont.
• Windows application:
• Standard graphical applications commonly use
• Use graphical entities such as
• Forms, webform, dialog boxes, buttons etc.
Type of Applications cont.

Fig. window application


Type of Applications cont.
• Web-based Application:
• applications created for the World-Wide-Web or Internet
• run from a Browser
• the actual program reside on a Web Server
• They are distributed to any client or browser which makes the request.
Type of Applications cont.

Fig. Web based application


Creating VB.NETProject
• Steps in creating project
• Open the Visual Studio IDE and invoke the Start Page:
• In the Start Page, select New Project
• In the New Project Dialog select Application Type
• In the Project Types box select: “Visual Basic Projects”
• In the Template box select: “Windows Applications” or “Console Application”
• Enter the project name into the “Name:” text box
• Set the project path or location in the “Location:” text box. You can also
browse for the path using the “Browse” button.
• Click OK
Console Application
Main Screen for a Console
Application:
VB.NET - PROGRAM STRUCTURE
• A VB.NET program basically consists of the
following components:
• Namespace declaration
• A class or module
• One or more procedures
• Variables
• The Main procedure
• Statements & Expressions
• Comments
VB.NET Sample Program
• Look at a simple code that would print the words “VB.NET CLASS!":

Imports System ‘ default namespace for 2012 VB.NET


Module Module1
'This program will display VB.NET CLASS
Sub Main()
Console.WriteLine(“VB.NET CLASS!")
Console.ReadKey()
End Sub
End Module

When the above code is compiled and executed, it produces the following
result:
VB.NET CLASS!
VB.NET Sample cont.

• The first line of the program Imports System is used to include the
System namespace in the program.
• The next line has a Module declaration, the module Module1.
VB.Net is completely object oriented, so every program must contain
a module of a class that contains the data and procedures that your
program uses.
• Classes or Modules generally would contain more than one
procedure. Procedures contain the executable code, or in other
words, they define the behavior of the class. A procedure could be
any of the following: Function, Sub, Operator, Get, Set etc
VB.NET Sample cont.
• The next line( 'This program) will be ignored by the compiler and it
has been put to add additional comments in the program.
• The next line defines the Main procedure, which is the entry point for
all VB.Net programs. The Main procedure states what the module or
class will do when executed.
• The Main procedure specifies its behavior with the statement
• Console.WriteLine(“VB.NET CLASS")
• WriteLine is a method of the Console class defined in the System
namespace. This statement causes the message
• “VB.NET CLASS!" to be displayed on the screen.
VB.NET Sample cont.
• The last line Console.ReadKey() is for the VS.NET Users. This will
prevent the screen from running and closing quickly when the
program is launched from Visual Studio .NET.
Visual Basic Standard Modules
• A module is a global Visual Basic file that contains only code
– General purpose procedures, functions, and declarations of
variables and constants
– Can be accessed by all code in the same project
– No event handlers
– Stored in files that end with the .vb extension
– Appears in the Solution Explorer window along with entries
for the project’s form files
Visual Basic Standard Modules cont.
• The Module has the following characteristics:
• Modules
• contain a method named Sub Main() that can be used as the starting
point of the application. Note this applies to both Console Applications
and Windows Applications.
• contain variable declarations, functions & procedures or source code
ONLY! No forms!
• are a good place to put program code that may be common to several
forms or other modules.
• are like forms but without the visual!
• When you create a Console Application, Visual Basic automatically
creates a Module where you can begin entering your code:
Code Editor for Console
Application
• For a Console Application, the code editor is invoked immediately to
allow you to enter code in the Module.
• Simply begin entering code in the Module Document.
• The Code Editor screen contains two drop-down list boxes, one for
the Object you are coding and the other for the Methods & Event-
Procedures associated with the object:
Code Editor for Console
Application cont.
Main Screen for Windows
Application
Module Names and Module Files
• A module
– begins with a Module statement
– ends with an End Module statement
• Here is the general format:

• ModuleName is the name of the module


– For e.g Module1 above is the default module name
– Can be any valid identifier that describes its purpose
• Code is stored in a file that is named with the .vb extension
• Normally, the name of the file is the same as the name of the module
Module Example

• The following code shows the contents of a module named


RetailMath

• Procedures, functions, and declarations can be declared as Private,


which means that they can be accessed only by code in the same
module
How VB Organise Program or
Application Files
• When you create a VB program a Folder is automatically
created in you computer which contains the files of the
program.
• A Visual Basics application is called a Solution. The Solution
is composed of one or more Project.
• The table below lists several important files and their file
extensions:
How VB Organise Program or Application Files
cont.
File Explanation Example
Extension
.sln Solution – File that holds information VideoManagement.sln
about all the projects in the application

.vb Object File – Contain definition and code frmLoginScreen.vb


for Forms, Modules, Class Modules etc. MainModule.vb

Resource File – This is a resource file for


.resx the Forms in the project. It contains frmLoginScreen.resx
information about all resources used by
the form.

Project File – This file describes the


project and lists the files included in
.vbproj the project. VideoManagement.vbproj
• Visual Basic has three distinct modes:
• Design Time – When you design the User Interface (GUI) and writing the
code.
• Run Time – When you execute and test the program
• Break Time – When you have Run time errors and you stop execution
Programming Errors
• There are three varieties of programming errors:
• Syntax Errors – Errors generated due to the code not following the
rules of the language
• These errors usually involve improper punctuation, spelling or format
• These problems are easier to solve since the compiler identifies the
erroneous statement and you just have to review
• the rules of the language to make sure you are writing the statement properly
Programming Errors cont.
• Run Time Errors – Program halts during execution. The program
passed the syntax test but fails during execution. .
• These problems are usually caused by improper arithmetic execution,
attempting to access recourses that are not available etc
• These problems are difficult to solve since they only show up when the
program runs.
Programming Errors cont.
• Logical Error – The program is not doing what is supposed to do. .
• The algorithm fails to solve the problem the program was written to resolve
• These problems are even more difficult to solve since you need to re-think
and go back to the planning phase and review the Algorithm.
Build or Compile the Program
• The steps to build are as follows:
• 1) In the Menu Bar select Build|Build Solution, this will
invoke the Output Window
• 2) The Output Window displays the results of the
Compiler process. It shows if the results of the compiler
were successful or failed. For a fully compiled program, all
compiler errors must equal to 0
Execute or Run the Program
• Console Application where Output Window stays displayed:
• To run or execute the program do any of the following:
• The three methods to executes are:
• 1) In the Menu Bar Debug|Start Without Debugging.
• 2) Or using the keyboard use the Crtl-F5.
• 3) Or navigate to the project folder\bin directory and double-click on the
executable file
Data types
• Data type is a set of values together with a set of operations on those
values.
• The following are VB data types:
Data type Size
Byte 1 byte
Integer 2 bytes
Long 4 bytes
Single 4 bytes
Double 8 bytes
Data types cont.
Currency 8 bytes
Decimal 12 bytes
String 16 bytes (Length of string)
Date 8 bytes
Boolean 2 bytes True or False
Object 4 bytes
Variant 16 bytes Any value as large as Double
Variable
• Variable: a memory location whose contents may changed during program
execution
• Variable Names
• The following are the rules when naming the variables in Visual Basic
• It must be less than 255 characters
• No spacing is allowed
• It must not begin with a number
• Period is not permitted
• Not case sensitive
• It should not be a Reserved keyword( e.g. Sub, End, for, Case, While, etc.)
• No special character is allow (e.g. ? - +! @ # % ^ & * ( ) [ ] { } . ; : " ' / \)
• Begin each variable and constant name with a letter or underscore. This is a Visual Basic
requirement. Variable and constant names can contain only letters, underscores, and
numbers.
Variable cont.
• Example of Valid names:
• intFeb_Income
• decSales2014
• dblEastRegion
• StrName
• Example of invalid names:
• 4thQuarter- The name must begin with a letter or underscore
• dblWest Region -The name cannot contain a space
• StrFirst.Name - The name cannot contain punctuation
• decSales$East - The name cannot contain a special character
Variable Declaration
• In Visual Basic, one needs to declare the variables before using them
by assigning names and data types.
• They are normally declared in the general section of the codes'
windows using the Dim statement.
• The format is as follows:

Dim variableName As Datatypes


Variable Declaration cont.
• Examples:
• Dim password As String
• Dim yourName As String
• Dim firstnum As Integer
• Dim secondnum As Integer
• Dim total As Integer
• Dim doDate As Date
Variable Declaration cont.
• 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,.............

• You can omit the Dim keyword if you specify Public, Protected,
Friend, Protected Friend, Private, Static, or Read Only.
• If you do not specify otherwise, variables you declare using a Dim
statement are Private. The following two statements are equivalent:

Dim num_ people As Integer


Private num_people As Integer
Variable Declaration cont.
• Variable Initialization in VB.Net
• Variables are initialized (assigned a value) with an equal sign followed by a
constant expression.
• The general form of initialization is:
variable_name = value;
• for example,
• Dim pi As Double
• pi = 3.14159
• You can initialize a variable at the time of declaration as follows:
• Dim pi As Double = 3.14159
• Dim StudentID As Integer = 100
• Dim StudentName As String = “Tosin Smith"
Variable Declaration cont.

• If no value is given to variable at declaration, each type assign default


value as follows:
• Numeric variables are set to 0
• Boolean variables are set to False
• Object and String variables are set to Nothing
• Date variables are set to 1/1/0001 12:00:00AM
Variable Behaviour
• Factors that determines variable behaviour:
• Data type
• Kind of data it can hold e.g.: integer, string, character etc.
• Scope
• Code that can access the variable
• Accessibility
• Code in other module that can access the variable
• Lifetime
• How long the variable value is valid
• Visibility of a variable is determine by the above factors(scope,
accessibility and lifetime)
Performing INPUT and OUTPUT in
VB.NET
• Accepting Values from User:
• The Console class in the System namespace provides two input
functions
• Read()
• Reads the next character from the standard input stream
• Readline()
• Reads a line of characters from the standard input stream
• For example:
Dim message As String
message = Console.ReadLine()
surname = Console.Read()
Performing INPUT and OUTPUT in VB.NET cont.

• Displaying output to the user:


• The Console class in the System namespace also provides two functions for
displaying output:
• write()
• Writes the specified data to the standard output stream
• WriteLine()
• Writes the specified data followed by a new line to the standard output stream
• Examples:
• writeLine(“COSC 202 CLASS”)
• Writeline(surName)
• Write(amount)
Declaring Constants:
• In VB.Net, constants are declared using the Const statement. The Const
statement is used at module, class, structure, procedure, or block level for
use in place of literal values.
• The syntax for the Const statement is:
Const constantName [As datatype] = initializer
• Examples:
• ' The following statements declare constants.
Const maxval As Long = 4999
Public Const message As String = "HELLO"
Private Const piValue As Double = 3.1415
Operators
• Operator perform operations on value(s) to produce result(s)
• VB.NET support the following category of operators

a)Arithmetic operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Floating Division
\ Integer (whole number) division
Mod Remainder division
^ Exponentiation (raising to a power)
& String concatenation (combination)
Operators cont.
• B)Relational operators
• Combine value(s) to returns Boolean value(true or false)
operator meaning
> Greater than
< Less than
= Equal to
<> Not equal to
>= Greater than or equal to
<= Less than or equal to
• All relational operator are binary
Operators cont.
• Logical operators
• combine multiple Boolean expressions into a compound Boolean expression
operator description
And combine two expression into one. Returns true if both expression are true
Or combine two expression into one. if both expression evaluate to false, Or returns
false, otherwise it returns true
Not negates an expression
Xor logically joins two expressions. If both expression evaluate to true or both
expression evaluate to false, Xor returns false; otherwise it returns true
Operators cont.
• Exercise:
• If n = 4, answer = “Y” Are the following expressions true or false?
1. Not (n < 6) = False
2. (answer = "Y") Or (answer = "y") = T
3. (answer = "Y") And (answer = "y") =f
4. Not (answer = "y") =T
5. (answer = “y”) Xor (answer = “y”) = T
Naming convention
• Naming convention
• The following standard naming convention are adopted
• The first three letter should be lowercase prefix indicating the control
type, e.g:
• lbl indicate label control
• txt indicate text control
• btn indicate button control

• The first letter after the prefix should be uppercase e.g:


• txtSurname

• The part of control name after the three letter prefix should describe the control
purpose
Naming convention cont.
• Below are lists of prefixes of the common data types
Data Type Prefix Sample Value
Boolean bln blnLoggedIn
Byte byt bytAge
Char chr chrQuantity
Date dte dteBirthday
Decimal dec decSalary
Double dbl dblCalculatedResult
Integer int intLoopCounter
Long lng lng CustomerID
Object obj objWord
Short sho shoTotalParts
string str strName

You might also like