Introduction
Introduction
NET
Visual Basic .NET (VB.NET) is an object-oriented computer programming language implemented on
the .NET Framework. Although it is an evolution of classic Visual Basic language, it is not backwards-
compatible with VB6, and any code written in the old version does not compile under VB.NET.
Like all other .NET languages, VB.NET has complete support for object-oriented concepts. Everything
in VB.NET is an object, including all of the primitive types (Short, Integer, Long, String, Boolean, etc.)
and user-defined types, events, and even assemblies. All objects inherits from the base class Object. The
following reasons make VB.Net a widely used professional language −
Modern, general purpose.
Object oriented.
Component oriented.
Easy to learn.
Structured language.
It produces efficient programs.
It can be compiled on a variety of computer platforms.
Part of .Net Framework
o Operator
o Get
o Set
o AddHandler
o RemoveHandler
o RaiseEvent
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("Hello World") WriteLine is a method of the Console class defined in
the System namespace. This statement causes the message "Hello, World!" to be displayed on
the screen.
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.
1
CBool(expression)
Converts the expression to Boolean data type.
2
CByte(expression)
Converts the expression to Byte data type.
3
CChar(expression)
Converts the expression to Char data type.
4
CDate(expression)
Converts the expression to Date data type
5
CDbl(expression)
Converts the expression to Double data type.
6
CDec(expression)
Converts the expression to Decimal data type.
7
CInt(expression)
Converts the expression to Integer data type.
8
CLng(expression)
Converts the expression to Long data type.
9
CObj(expression)
Converts the expression to Object type.
10
CSByte(expression)
Converts the expression to SByte data type.
11
CShort(expression)
Converts the expression to Short data type.
12
CSng(expression)
Converts the expression to Single data type.
13
CStr(expression)
Converts the expression to String data type.
14
CUInt(expression)
Converts the expression to UInt data type.
15
CULng(expression)
Converts the expression to ULng data type.
16
CUShort(expression)
Converts the expression to UShort data type.
Example
Module DataTypes
Sub Main()
Dim n As Integer
Dim da As Date
Dim bl As Boolean = True
n = 1234567
da = Today
Console.WriteLine(bl)
Console.WriteLine(CSByte(bl))
Console.WriteLine(CStr(bl))
Console.WriteLine(CStr(da))
Console.WriteLine(CChar(CChar(CStr(n))))
Console.WriteLine(CChar(CStr(da)))
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
True
-1
True
12/4/2012
1
1
Variables
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable
in VB.Net has a specific type, which determines the size and layout of the variable's memory.
Example
Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10
b = 20
c=a+b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine()
End Sub
End Module
Module variablesNdataypes
Sub Main()
Dim message As String
Console.Write("Enter message: ")
message = Console.ReadLine
Console.WriteLine()
Console.WriteLine("Your Message: {0}", message)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result (assume the user inputs
Hello World) −
Enter message: Hello World
Your Message: Hello World
Constants
The constants refer to fixed values that the program may not alter during its execution.
These fixed values are also called literals. 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 −
[ < attributelist > ] [ accessmodifier ] [ Shadows ]
Const constantlist
Where,
attributelist − specifies the list of attributes applied to the constants; you can provide
multiple attributes separated by commas. Optional.
accessmodifier − specifies which code can access these constants. Optional. Values
can be either of the: Public, Protected, Friend, Protected Friend, or Private.
Shadows − this makes the constant hide a programming element of identical name in a
base class. Optional.
Constantlist − gives the list of names of constants declared.
For example,
'The following statements declare constants.'
Const maxval As Long = 4999
Public Const message As String = "HELLO"
Private Const piValue As Double = 3.1415
Example
The following example demonstrates declaration and use of a constant value –
Module constantsNenum
Sub Main()
Const PI = 3.14149
Dim radius, area As Single
radius = 7
area = PI * radius * radius
Console.WriteLine("Area = " & Str(area))
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
Area = 153.933
Modifiers
The modifiers are keywords added with any programming element to give some especial
emphasis on how the programming element will behave or will be accessed in the program.
For example, the access modifiers: Public, Private, Protected, Friend, Protected Friend, etc.,
indicate the access level of a programming element like a variable, constant, enumeration or a
class.
Public Specifies that one or more declared programming elements have no access
restrictions.
Private Specifies that one or more declared programming elements are accessible only
from within their declaration context, including from within any contained types.
Protected Specifies that one or more declared programming elements are accessible only
from within their own class or from a derived class.
Static Specifies that one or more declared local variables are to continue to exist and
retain their latest values after termination of the procedure in which they are
declared.
ReadOnly Specifies that a variable or property can be read but not written.
Friend Specifies that one or more declared programming elements are accessible
from within the assembly that contains their declaration, not only by the
component that declares them.
Default Identifies a property as the default property of its class, structure, or interface.
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. VB.Net is rich in built-in operators and provides following types of commonly
used operators −
Arithmetic Operators
Comparison Operators
Logical/Bitwise Operators
Bit Shift Operators
Assignment Operators
Miscellaneous Operators
This tutorial will explain the most commonly used operators.
Miscellaneous Operators
There are few other important operators supported by VB.Net.