You are on page 1of 15

introduction to virtual basic

Visual Basic (VB) is a simple, modern, object-oriented and type-safe programming language. Visual
Basic language has its roots in the family of C languages such as C, C++ and it is mostly similar to
Java programming.
 
Visual Basic (VB) Programming language will allow developers to build a variety of secure and robust
applications such as windows applications, web applications, database applications, etc. which will
run on .NET Framework.
 
Visual Basic (VB) programming language has built on .NET Framework to run the VB applications;
we are required to install a .NET Framework component on our machines.
 
.NET Framework is a development platform for building apps for windows, web, azure, etc. by using
programming languages such as C#, F# and Visual Basic (VB). It consists of two major components
such as Common Language Runtime (CLR), it’s an execution engine that handles running apps and
the .NET Framework Class Library, which provides a library of tested and reusable code that
developers can use in their applications.

Overview of Visual Basic (VB)


 Visual Basic (VB) is an object-oriented programming language, and it supports the concepts
of encapsulation, abstraction, polymorphism, etc.
 In VB, all the variables, methods, and application entry points are encapsulated within the
class definitions.
 Visual Basic (VB) is explicitly developed for .NET Framework, and it enables programmers to
migrate from C/C++ and Java easily.
 Visual Basic is a fully Event-driven and visual programming language.
 Microsoft provided an IDE (Integrated Development Environment) tool called Visual Studio to
implement VB programs quickly.

Features of Visual Basic (VB)


 Visual Basic (VB) contains various features similar to other programming languages such as C,
C++, and Java.
 VB is a modern programming language, and it is very powerful, simple for building
applications.
 Visual Basic (VB) is useful in developing windows, web, and device applications.
 Visual Basic provides automatic memory management by clearing unused objects.
 VB is a type-safe programming language, and it won't allow performing unchecked type
casts.
 VB provides a structured and extensible approach for error detection and recovery.

 
Visual Basic is a structure-oriented programming language, and the compilation, execution of VB
applications are faster due to automatic scalability.

History of Visual Basic (VB)


The initial release of Visual Basic programming language was in 2002 with .NET Framework 1.0, and
it’s more like Java programming. The latest version of Visual Basic is 15.8, and it was released with
Visual Studio 2017 with a lot of new features.
As discussed in VB Introduction, Visual Basic (VB) programming language has been built on .NET
Framework to build various secure and robust applications such as windows, web, or database
applications based on our requirements.
 
To run Visual Basic (VB) applications, we require to install a .NET Framework component on our
machines. If you are using a Windows operating system, the .NET Framework is installed on your
machine by default. To know more about the .NET Framework versions installed on your machine,
check this .NET Framework Versions.

Visual Studio IDE


Microsoft has provided an IDE (Integrated Development Environment) tool called Visual Studio to
build applications using programming languages such as C#, F#, Visual Basic, etc., based on our
requirements.
 
The Visual Studio IDE will provide a common user interface with various development tools to build
applications with different programming languages such as C#, F#, Visual Basic, etc.
 
To install and use Visual Studio for a commercial purpose, we need to buy a license from Microsoft. If
you want to use Visual Studio for learning (non-commercial) purposes, Microsoft provided a free
Visual Studio Community version.
 
You can download and install a Visual Studio Community version from visualstudio.com. In our Visual
Basic (VB) tutorial, we will use the Visual Studio 2017 community version.
 
Now we will see how to create a console application using visual studio in a visual basic
programming language.
Create a Project in Visual Studio
After completing a visual studio installation, open visual studio and it will be prompted to sign in for
the first time. The sign-in step is optional, so you can skip it, and in the next step, the dialog box will
appear and ask you to choose your Development Settings and color theme. Once you select the
required options, click on the Start Visual Studio.
After the visual studio launch, create a new console application using a visual basic programming
language; for that, Go to File  New  select a Project
Once you click on Project, a new popup will open in that select Visual Basic from the left pane and
choose Console App. In the Name section, give any name for your project and select an
appropriate Location path to save your project files, and click OK
Once we click on the OK button, a new console application will be created 
This is how we can create a console application in a visual basic programming language using a
visual studio based on our requirements.

Using Visual Studio, we can easily create a Hello World Program or Console Application in Visual
Basic based on our requirements.
 
In the previous chapter, we learned how to Download and Install Visual Studio on Windows Machine.
In case if you don't install a visual studio, then follow the instructions to install a visual
studio otherwise open your visual studio.

Create Visual Basic Console Application


To create a new application in visual studio, go to the Menu bar, select File  New  select
a Project
Once you click on Project, a new popup will open in that select Visual Basic from the left pane and
choose Console App. In the Name section, give any name for your project, select the
appropriate Location path to save your project files, and click OK
Once we click on OK button, a new console application will be created like as shown below. If
the Module1.vb file is not opened in your code editor, open the Solution Explorer menu on the
right side and double click on your Module1.vb file.
If you observe the above image, by default, the application contains a Main() method because the
console applications in visual basic will always start from the Main() method of program class.

Visual Basic Data Types


n Visual Basic, Data Types are useful to define a type of data the variable can hold, such as integer,
float, string, etc., in our application.
 
Visual Basic is a Strongly Typed programming language. Before we perform any operation on a
variable, it’s mandatory to define a variable with a required data type to indicate what type of data
the variable can hold in our application.
Syntax of Defining Visual Basic Data Types
Following is the syntax of defining data types in visual basic.
 
Dim [Variable Name] As [Data Type]
Dim [Variable Name] As [Data Type] = [Value]
If you observe the above syntax, we added a required data type after the variable name to tell the
compiler about the type of data the variable can hold.

Visual Basic Variables


In Visual Basic, Variables will represent storage locations, and each variable will have a
particular data type to determine the type of values the variable can hold.
 
Visual Basic is a Strongly Typed programming language. Before we perform any operation on
variables, it’s mandatory to define the variable with a required data type to indicate the type of data
the variable can hold in our application.

Syntax of Visual Basic Variables Declaration


Following is the syntax of declaring and initializing variables in visual basic.
 
Dim [Variable Name] As [Data Type]
Dim [Variable Name] As [Data Type] = [Value]

If you observe the above variable declarations, we added a required data type after the variable
name to tell the compiler about the type of data the variable can hold.
In visual basic, Keywords are the predefined set of reserved words that have special meaning for the
compiler. So the keywords in visual basic cannot be used as identifiers such as variable name, class
name, etc., in our applications.

Visual Basic Keyword Types


In visual basic, Keywords are differentiated into two types those are
 

 Reserved Keywords
 Unreserved Keywords

Visual Basic Reserved Keywords


Generally, the reserved keywords will not allow us to use them as names for programming elements
such as variables, class, etc. In case, if we want to bypass this restriction, we need to define the
variable name in brackets ([]).

Visual Basic Unreserved Keywords


In visual basic, unreserved keywords are not reserved keywords which means we can use them as
names for programming elements such as variables, class, etc. However, it’s not recommended to
use the keywords as programming elements because it will make your code hard to read and lead to
subtle errors that can be difficult to find.
 

Visual Basic Use Keywords as Variable Names


In case you want to use Keywords as variable names (identifiers), then we need to enclose the
variable name in brackets ([]). For example, [Class] is a valid identifier but Class is not because it’s
a keyword and has a special meaning for the compiler.

Visual Basic Comments


In Visual Basic, Comments are the self-explanatory notes to provide detailed information about the
code which we wrote in our applications.
 
By using comment symbol ('), we can comment on the code in our Visual Basic programming. The
comment symbol (') will tell the Visual Basic compiler to ignore the text following it, or the comment.
 
It’s always a good practice to include the comments in your Visual Basic code to provide detailed
information about the functionality like what the specific block or line of code can do and it will be a
benefit for anyone else who examines the code.
 
In Visual Basic, we can include the comments anywhere in the program without effecting our code
and the comments in Visual Basic do not affect the performance of an application because the
comments won’t be compiled and executed by the compiler.
 
Following is the example of defining the comments in Visual Basic programming language.
 
' Show Greet Messaging
Console.WriteLine("Welcome to Tutlane.com")
Console.ReadLine() ' This method to read the commands from a console
In case, if you want to comment more than one line, use the comment symbol ( ') on each line like as
shown below.
 
' Show Greet Messaging
' Multiple line comments
This is how we can use the comments to provide detailed information about the functionality of our
Visual Basic application.
 
In visual studio, we can use the following keyboard shortcuts to comment/uncomment the selected
lines of code based on our requirements.
 
To comment keyboard shortcut is Ctrl + K, C and to uncomment keyboard shortcut is Ctrl + K, U

Visual Basic Operators


In Visual Basic, Operator is a programming element that specifies what operation needs to perform
on operands or variables. For example, an addition (+) operator in Visual Basic is used to perform the
sum operation on operands.

Visual Basic Operator Types


In Visual Basic different types of operators are available; those are
 

 Arithmetic Operators
 Assignment Operators
 Logical/Bitwise Operators
 Comparison Operators
 Concatenation Operators

Now, we will learn each operator in a detailed manner with examples in the Visual Basic
programming language.

Visual Basic Arithmetic Operators


In Visual Basic, Arithmetic Operators are useful for performing basic arithmetic calculations like
addition, subtraction, division, etc., based on our requirements.

Visual Basic Assignment Operators


In Visual Basic, Assignment Operators are useful to assign a new value to the operand.

Visual Basic Logical / Bitwise Operators


In Visual Basic, Logical / Bitwise Operators are useful to perform the logical operation between two
operands like AND, OR, etc., based on our requirements. The Logical / Bitwise Operators will always
work with Boolean expressions (true or false) and return Boolean values.

Visual Basic Comparison Operators


In Visual Basic, Comparison Operators are useful to determine whether the defined two operands
are equal, greater than or less than, etc., based on our requirements.

Visual Basic Concatenation Operators


In Visual Basic, Concatenation Operators are useful to concatenate defined operands based on our
requirements.

Introduction to GUI
A GUI (graphical user interface) is a system of interactive visual
components for computer software. A GUI displays objects that convey
information, and represent actions that can be taken by the user. The
objects change color, size, or visibility when the user interacts with them.

The GUI was first developed at Xerox PARC by Alan Kay, Douglas


Engelbart, and a group of other researchers in 1981.
Later, Apple introduced the Lisa computer with a GUI on January
19, 1983.

GUI overview
A GUI includes GUI objects like icons, cursors, and buttons. These
graphical elements are sometimes enhanced with sounds, or visual
effects like transparency and drop shadows. Using these objects, a
user can use the computer without having to know commands.

Below is a picture of the Windows 7 desktop and an example of a


GUI operating system. In this example, you could use a mouse to
move a pointer and click a program icon to start a program.
What are the elements of a GUI?
To make a GUI as user-friendly as possible, there are different
elements and objects that the user use to interact with the software.
Below is a list of each of these with a brief description.

 Button - A graphical representation of a button that


performs an action in a program when pressed
 Dialog box - A type of window that displays additional
information, and asks a user for input.
 Icon - Small graphical representation of a program,
feature, or file.
 Menu - List of commands or choices offered to the user
through the menu bar.
 Menu bar - Thin, horizontal bar containing the labels of
menus.
 Ribbon - Replacement for the file menu and toolbar that
groups programs activities together.
 Tab - Clickable area at the top of a window that shows
another page or area.
 Toolbar - Row of buttons, often near the top of an
application window, that controls software functions.
 Window - Rectangular section of the computer's display
that shows the program currently being used.

How does a GUI work?


A GUI uses windows, icons, and menus to carry out commands, such
as opening, deleting, and moving files. Although a GUI operating
system is primarily navigated using a mouse, a keyboard can also be
used via keyboard shortcuts or the arrow keys.

As an example, if you wanted to open a program on a GUI system, you


would move the mouse pointer to the program's icon and double-
click it. With a command line interface, you'd need to know the
commands to navigate to the directory containing the program, list the
files, and then run the file.

What are the benefits of GUI?


A GUI is considered to be more user-friendly than a text-
based command-line interface, such as MS-DOS, or the shell of Unix-
like operating systems.

Unlike a command-line operating system or CUI, like Unix or MS-DOS,


GUI operating systems are easier to learn and use because commands
do not need to be memorized. Additionally, users do not need to know
any programming languages. Because of their ease of use and more
modern appearance, GUI operating systems have come to dominate
today's market.

What are examples of a GUI operating system?


 Microsoft Windows
 Apple System 7 and macOS
 Chrome OS
 Linux variants like Ubuntu using a GUI interface.

Are all operating systems GUI?


No. Early command line operating systems like MS-DOS and even
some versions of Linux today have no GUI interface.

What are examples of a GUI interface?


1. Apple macOS

2. Microsoft Windows

3. GNOME
4. KDE

5. Any Microsoft program, including Word, Excel,


and Outlook.

6. Internet browsers, such as Internet


Explorer, Chrome, and Firefox.

How does the user interact with a GUI?


A pointing device, such as the mouse, is used to interact with nearly
all aspects of the GUI. More modern (and mobile) devices also utilize
a touch screen.

Does a GUI require a mouse?


No. Nearly all GUI interfaces, including Microsoft Windows, have
options for navigating the interface with a keyboard, if you know
the keyboard shortcuts.

introduction to visual basic controls


An object is a type of user interface element you create on a Visual Basic form by
using a toolbox control. In fact, in Visual Basic, the form itself is an object. Every Visual
Basic control consists of three important elements −
 Properties which describe the object,
 Methods cause an object to do something and
 Events are what happens when an object does something.

Control Properties
All the Visual Basic Objects can be moved, resized or customized by setting their
properties. A property is a value or characteristic held by a Visual Basic object, such as
Caption or Fore Color.
Properties can be set at design time by using the Properties window or at run time by
using statements in the program code.
Object. Property = Value

Where
 Object is the name of the object you're customizing.
 Property is the characteristic you want to change.
 Value is the new property setting.
For example,
Form1.Caption = "Hello"

You can set any of the form properties using Properties Window. Most of the properties
can be set or read during application execution. You can refer to Microsoft
documentation for a complete list of properties associated with different controls and
restrictions applied to them.

Control Methods
A method is a procedure created as a member of a class and they cause an object to
do something. Methods are used to access or manipulate the characteristics of an
object or a variable. There are mainly two categories of methods you will use in your
classes −
 If you are using a control such as one of those provided by the Toolbox, you can
call any of its public methods. The requirements of such a method depend on
the class being used.
 If none of the existing methods can perform your desired task, you can add a
method to a class.

Control Events
An event is a signal that informs an application that something important has occurred.
For example, when a user clicks a control on a form, the form can raise a Click event
and call a procedure that handles the event. There are various types of events
associated with a Form like click, double click, close, load, resize, etc.
ere, Handles MyBase.Load indicates that Form1_Load() subroutine
handles Load event. Similar way, you can check stub code for click, double click. If you
want to initialize some variables like properties, etc., then you will keep such code
inside Form1_Load() subroutine. Here, important point to note is the name of the event
handler, which is by default Form1_Load, but you can change this name based on your
naming convention you use in your application programming.

Basic Controls
VB.Net provides a huge variety of controls that help you to create rich user interface.
Functionalities of all these controls are defined in the respective control classes. The
control classes are defined in the System.Windows.Forms namespace.
Understanding Visual Basic Variable and
Constant Types
A variable is a location in memory where a value can be stored during the execution
of a Visual Basic application. Visual Basic variables are assigned names by the
programmer when they are declared so that they can easily be referenced in other
places in the application code.
These values may either be declared as variable (in that once the value has been
assigned it can be changed later in the Visual Basic code) or as constant (in that
once the value has been assigned, it cannot be changed elsewhere in the code).
In the next chapter (Declaring Visual Basic Variables and Constants) we will look at
how to create Visual Basic variables and assign values to them. Before doing that,
however, we need to look at the various types of variables that are available.

Boolean Variable
The Visual Basic Boolean variable type holds a value of true or false. Internally
these are actually stored as the numbers 1 and 0
representing true and false respectively. Boolean values are used in conditional
statements to decide whether particular parts of Visual Basic code should be
executed or not (see Visual Basic Flow Control for more details on conditional
statements).

Char Variable
The Char variable type holds a single character (such as the letter 'B'). The value of
a Char can be any character. Internally, a number in the range 0 to 65,553 is used to
represent the character value as defined by the ASCII table. As an example when
the letter 'Z' is assigned to a Visual Basic Char variable, the number 90 is actually
stored in the variable location.

Byte Variable
A Byte variable holds a positive number in the range 0 to 255. Because of the limited
range, Byte variables should be used with caution. An attempt to assign a negative
value or a value greater than 255 to a Visual Basic Byte variable will result in an
error.

Date Variable
The Visual Basic Date variable type holds a date and time value. Dates are declared
in the form #mm/dd/yyyy#. For example, May 23, 2007 would be declared as
#5/23/2007#. Visual Basic provides a number of mechanisms for working with Date
variables (such as calculating a date six months from today's date). These are
covered in the Working with Dates and Times in Visual Basic section of this book.

ecimal Variable
The Decimal variable type can store either a whole number or a decimal number up
to 29 decimal places. When working with whole numbers, however, it is
recommended that the Integer variable type be used as this is more memory
efficient than the Decimal variable type.

Double Variable
The Visual Basic Double variable is used to store either very large numbers, or small
numbers requiring more than 28 decimal places. To be precise Visual basic Double
variables can store positive numbers in the range 4.94065645841246544E-324 to
1.79769313486231570E+308 and negative numbers from -
1.79769313486231570E+30 to -4.94065645841246544E-324.
Double variables are typically used when developing scientific applications using
Visual Basic and, as such, are generally not used by the average developer.

Integer Variable
The Visual Basic Integer variable type is used to store whole numbers between -2,147,483,648
and 2,147,483,648. The Integer variable cannot be used to store numbers containing decimals.

Object Variable
The Object variable is a catch-all variable type which can be used to store a value of any data
type. Because of this flexibility, an Object variable will typically reserve more memory than is
needed for most variable types. For this reason, it is more efficient to use the correct variable
type for the value you need to store (such as Char for a character or Integer for a whole number)
rather than use the Object variable type.

Long Variable
The Visual Basic Long variable type is used to store whole numbers ranging from -
9,233,372,036,854,775,808 to 9,233,372,036,854,775,807.

Short Variable
A Short variable in Visual Basic stores a whole number in the range -32,768 to 32,767.

String Variable
The String variable type stores multiple characters that make up words or
sentences. String variables are encapsulated in double-quotation marks ("). For example "Hello"
is a string, as is "Hello, this is a test".
Visual Basic provides a number of mechanisms for working with and manipulating strings as
covered in the Working with Strings in Visual Basic chapter of this book.

Sub procedures (Visual Basic)

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End


Sub statements. The Sub procedure performs a task and then returns control to the
calling code, but it does not return a value to the calling code.

Each time the procedure is called, its statements are executed, starting with the first
executable statement after the Sub statement and ending with the first End Sub, Exit Sub,
or Return statement encountered.

You can define a Sub procedure in modules, classes, and structures. By default, it


is Public, which means you can call it from anywhere in your application that has access
to the module, class, or structure in which you defined it. The term method describes
a Sub or Function procedure that is accessed from outside its defining module, class, or
structure. For more information, see Procedures.

A Sub procedure can take arguments, such as constants, variables, or expressions, which


are passed to it by the calling code.

Parameter declaration
You declare each procedure parameter similarly to how you declare a variable,
specifying the parameter name and data type. You can also specify the passing
mechanism, and whether the parameter is optional or a parameter array. If the
parameter is optional, you must also supply a default value as part of its declaration.

Parameters as local variables

When control passes to the procedure, each parameter is treated as a local variable. This
means that its lifetime is the same as that of the procedure, and its scope is the whole
procedure.
Calling syntax
You invoke a Sub procedure explicitly with a stand-alone calling statement. You cannot
call it by using its name in an expression. You must provide values for all arguments that
are not optional, and you must enclose the argument list in parentheses. If no
arguments are supplied, you can optionally omit the parentheses. The use of
the Call keyword is optional but not recommended.

You can call a Sub method from outside the class that defines it. First, you have to use
the New keyword to create an instance of the class, or call a method that returns an
instance of the class. For more information, see New Operator. Then, you can use the
following syntax to call the Sub method on the instance object:

Illustration of declaration and call


The following Sub procedure tells the computer operator which task the application is
about to perform, and also displays a time stamp. Instead of duplicating this code at the
start of every task, the application just calls tellOperator from various locations. Each call
passes a string in the task argument that identifies the task being started.

You might also like