0% found this document useful (0 votes)
89 views10 pages

VB.NET NOTES

notes

Uploaded by

Mario
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views10 pages

VB.NET NOTES

notes

Uploaded by

Mario
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

VB.

Net is a simple, modern, object-oriented computer programming


language developed by Microsoft to combine the power of .NET
Framework and the common language runtime with the productivity
benefits that are the hallmark of Visual Basic.
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.

Integrated Development Environment (IDE) For


VB.Net
 Visual Studio 2010 (VS)
 Visual Basic 2010 Express (VBE)
 Visual Web Developer

A VB.Net program basically consists of the following parts

 Namespace declaration
 A class or module
 One or more procedures
 Variables
 The Main procedure
 Statements & Expressions
 Comments
Files that make up u VB project
 Project (. vbp) file. One per project. ...
 Form (. frm) files. One for each form in your project. ...
 Binary data (. frx) files. ...
 Standard Module (. bas) files. ...
 Class Modules (. cls) files. ...
 Resource (. res) file. ...
 Windows Layout (. vbw) file.

simple code that would print the words "Hello World"
Imports System
Module Module1
'This program will display Hello World
Sub Main()
Console.WriteLine("Hello World")
Console.ReadKey()
End Sub
End Module

Parts of the above program


 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 −
o Function
o Sub
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.

BASIC SYNTAX

VB.Net is an object-oriented programming language. In Object-


Oriented Programming methodology, a program consists of various
objects that interact with each other by means of actions. The
actions that an object may take are called methods. Objects of the
same kind are said to have the same type or, more often, are said to
be in the same class.

When we consider a VB.Net program, it can be defined as a


collection of objects that communicate via invoking each other's
methods. Let us now briefly look into what do class, object, methods
and instance variables mean.

 Object − Objects have states and behaviors. Example: A dog


has states - color, name, breed as well as behaviors - wagging,
barking, eating, etc. An object is an instance of a class.
 Class − A class can be defined as a template/blueprint that
describes the behaviors/states that objects of its type support.
 Methods − A method is basically a behavior. A class can
contain many methods. It is in methods where the logics are
written, data is manipulated and all the actions are executed.
 Instance Variables − Each object has its unique set of
instance variables. An object's state is created by the values
assigned to these instance variables.
A Rectangle Class in VB.Net
Imports System
Public Class Rectangle
Private length As Double
Private width As Double

'Public methods
Public Sub AcceptDetails()
length = 4.5
width = 3.5
End Sub

Public Function GetArea() As Double


GetArea = length * width
End Function
Public Sub Display()
Console.WriteLine("Length: {0}", length)
Console.WriteLine("Width: {0}", width)
Console.WriteLine("Area: {0}", GetArea())

End Sub

Shared Sub Main()


Dim r As New Rectangle()
r.Acceptdetails()
r.Display()
Console.ReadLine()
End Sub
End Class

When the above code is compiled and executed, it produces the


following result

Length: 4.5
Width: 3.5
Area: 15.75

Data Types Available in VB.Net


Data types refer to an extensive system used for declaring variables
or functions of different types. The type of a variable determines
how much space it occupies in storage and how the bit pattern
stored is interpreted.

Data Storage
Value Range
Type Allocation

Depends on
Boolean implementin True or False
g platform

Byte 1 byte 0 through 255 (unsigned)

Char 2 bytes 0 through 65535 (unsigned)

0:00:00 (midnight) on January 1, 0001


Date 8 bytes through 11:59:59 PM on December 31,
9999

0 through +/-
79,228,162,514,264,337,593,543,950,33
5 (+/-7.9...E+28) with no decimal point; 0
Decimal 16 bytes
through +/-
7.9228162514264337593543950335
with 28 places to the right of the decimal

-1.79769313486231570E+308 through -
4.94065645841246544E-324, for
negative values
Double 8 bytes
4.94065645841246544E-324 through
1.79769313486231570E+308, for
positive values

-2,147,483,648 through 2,147,483,647


Integer 4 bytes
(signed)

-9,223,372,036,854,775,808 through
Long 8 bytes
9,223,372,036,854,775,807(signed)

Object 4 bytes on Any type can be stored in a variable of


32-bit type Object
platform
8 bytes on
64-bit
platform

SByte 1 byte -128 through 127 (signed)

Short 2 bytes -32,768 through 32,767 (signed)

-3.4028235E+38 through -1.401298E-45


for negative values;
Single 4 bytes
1.401298E-45 through 3.4028235E+38
for positive values

Depends on
0 to approximately 2 billion Unicode
String implementin
characters
g platform

UIntege
4 bytes 0 through 4,294,967,295 (unsigned)
r

0 through 18,446,744,073,709,551,615
ULong 8 bytes
(unsigned)

Each member of the structure has a


Depends on
User- range determined by its data type and
implementin
Defined independent of the ranges of the other
g platform
members

UShort 2 bytes 0 through 65,535 (unsigned)

Example
The following example demonstrates use of some of the types

Module DataTypes
Sub Main()
Dim b As Byte
Dim n As Integer
Dim si As Single
Dim d As Double
Dim da As Date
Dim c As Char
Dim s As String
Dim bl As Boolean
b = 1
n = 1234567
si = 0.12345678901234566
d = 0.12345678901234566
da = Today
c = "U"c
s = "Me"
If ScriptEngine = "VB" Then
bl = True
Else
bl = False
End If
If bl Then
'the oath taking
Console.Write(c & " and," & s & vbCrLf)
Console.WriteLine("declaring on the day of:
{0}", da)
Console.WriteLine("We will learn VB.Net
seriously")
Console.WriteLine("Lets see what happens to the
floating point variables:")
Console.WriteLine("The Single: {0}, The Double:
{1}", si, d)
End If
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the
following result

U and, Me
declaring on the day of: 12/4/2012 12:00:00 PM
We will learn VB.Net seriously
Lets see what happens to the floating point variables:
The Single:0.1234568, The Double: 0.123456789012346

VB.Net - 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; the range of values that can be stored within that memory;
and the set of operations that can be applied to the variable.

Variable Declaration in VB.Net


The Dim statement is used for variable declaration and storage
allocation for one or more variables. The Dim statement is used at
module, class, structure, procedure or block level.

Syntax for variable declaration in VB.Net is −

[ < attributelist > ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]]


[ ReadOnly ] Dim [ WithEvents ] variablelist
 attributelist is a list of attributes that apply to the variable.
Optional.
 accessmodifier defines the access levels of the variables, it
has values as - Public, Protected, Friend, Protected Friend and
Private. Optional.
 Shared declares a shared variable, which is not associated
with any specific instance of a class or structure, rather
available to all the instances of the class or structure. Optional.
 Shadows indicate that the variable re-declares and hides an
identically named element, or set of overloaded elements, in a
base class. Optional.
 Static indicates that the variable will retain its value, even
when the after termination of the procedure in which it is
declared. Optional.
 ReadOnly means the variable can be read, but not written.
Optional.
 WithEvents specifies that the variable is used to respond to
events raised by the instance assigned to the variable.
Optional.
 Variablelist provides the list of variables declared.

Some valid variable declarations along with their definition are


shown here

Dim StudentID As Integer


Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim exitButton As New System.Windows.Forms.Button
Dim lastTime, nextTime As Date

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

example which makes use of various types of variables –

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

When the above code is compiled and executed, it produces the


following result −

a = 10, b = 20, c = 30

You might also like