Professional Documents
Culture Documents
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.
Visual Basic is a structure-oriented programming language, and the compilation, execution of VB
applications are faster due to automatic scalability.
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.
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.
Reserved Keywords
Unreserved Keywords
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.
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.
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.
2. Microsoft Windows
3. GNOME
4. KDE
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.
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.
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.
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: