Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Introduction

Visual Basic .NET (VB.NET) is an object-oriented computer programming language that is implemented on the .NET Framework. VB.NET is not backwards-compatible with the previous version VB6, and code written in VB6 will not compile in VB.NET. VB.NET fully supports object-oriented concepts, with everything represented as an object that inherits from the base Object class. Microsoft provides Visual Studio and Visual Basic Express as integrated development environments for creating VB.NET applications. A basic VB.NET program structure includes namespace declaration, class/module, procedures, variables, and the Main procedure. The code sample provided prints "Hello World" to demonstrate a simple VB.NET program.

Uploaded by

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

Introduction

Visual Basic .NET (VB.NET) is an object-oriented computer programming language that is implemented on the .NET Framework. VB.NET is not backwards-compatible with the previous version VB6, and code written in VB6 will not compile in VB.NET. VB.NET fully supports object-oriented concepts, with everything represented as an object that inherits from the base Object class. Microsoft provides Visual Studio and Visual Basic Express as integrated development environments for creating VB.NET applications. A basic VB.NET program structure includes namespace declaration, class/module, procedures, variables, and the Main procedure. The code sample provided prints "Hello World" to demonstrate a simple VB.NET program.

Uploaded by

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

Visual Basic .

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

Integrated Development Environment (IDE) For VB.Net


Microsoft provides the following development tools for VB.Net programming −
 Visual Studio 2010 (VS)
 Visual Basic 2010 Express (VBE)
 Visual Web Developer

VB.Net Hello World Example


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
Let us look at a 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

Let us look various 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.

Compile & Execute VB.Net Program


If you are using Visual Studio.Net IDE, take the following steps −
 Start Visual Studio.
 On the menu bar, choose File → New → Project.
 Choose Visual Basic from templates
 Choose Console Application.
 Specify a name and location for your project using the Browse button, and then choose the OK
button.
 The new project appears in Solution Explorer.
 Write code in the Code Editor.
 Click the Run button or the F5 key to run the project. A Command Prompt window appears that
contains the line Hello World
//Explain Identifiers, Keywords and Data types

The Type Conversion Functions in VB.Net


VB.Net provides the following in-line type conversion functions −

Sr.No. Functions & Description

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.

Variable Declaration in VB.Net


The Dim statement is used for variable declaration and storage allocation for one or more variables.

Syntax for variable declaration in VB.Net is −


[ < attributelist > ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]]
[ ReadOnly ] Dim [ WithEvents ] variablelist
Where,
 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


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 StudentID As Integer = 100
Dim StudentName As String = "Bill Smith"

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

Accepting Values from User


The Console class in the System namespace provides a function ReadLine for accepting input from the
user and store it into a variable. For example,
Dim message As String
message = Console.ReadLine
The following example demonstrates it –

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.

Friend access is often the preferred level for an application's programming


elements, and Friend is the default access level of an interface, a module, a
class, or a structure.

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.

Operator Description Example


AddressOf Returns the address of a AddHandler Button1.Click,
procedure. AddressOf Button1_Click

Await It is applied to an operand in


an asynchronous method or Dim result As res
lambda expression to = Await
suspend execution of the AsyncMethodThatReturnsResult()
method until the awaited task Await AsyncMethod()
completes.

GetType It returns a Type object for MsgBox(GetType(Integer).ToString())


the specified type. The Type
object provides information
about the type such as its
properties, methods, and
events.

Function It declares the parameters Dim add5 = Function(num As


Expression and code that define a Integer) num + 5
function lambda expression. 'prints 10
Console.WriteLine(add5(5))

If It uses short-circuit Dim num = 5


evaluation to conditionally Console.WriteLine(If(num >= 0,
return one of two values. The "Positive",
If operator can be called with
three arguments or with two
arguments.

You might also like