+ All Categories
Home > Documents > 1 VB net-Language Fundamentals

1 VB net-Language Fundamentals

Date post: 13-Nov-2014
Category:
Upload: suresh1130
View: 120 times
Download: 3 times
Share this document with a friend
Popular Tags:
32
VB.Net (Language Fundamentals)
Transcript
Page 1: 1 VB net-Language Fundamentals

VB.Net(Language

Fundamentals)

Page 2: 1 VB net-Language Fundamentals

A simple VB.net program

Imports System

Public Module HelloModule

Public Sub Main()

Console.WriteLine(“HelloWorld!!!”)

End Sub

End Module

Page 3: 1 VB net-Language Fundamentals

The Main() method

• A program's Main method can appear within a Visual Basic .NET module

• Because Visual Basic .NET modules are

classes wherein everything is shared, the Shared keyword is not used in such a declaration as in the previous example.

Page 4: 1 VB net-Language Fundamentals

Saving,Compiling and Executing the Program

• Save the file with extension “.vb” .(Test.vb)

• Goto command window

• Compile the file :• vbc Test.vb

• After successful compilation .EXE file is created(Test.EXE)

• Execute the EXE file as “Test”

Page 5: 1 VB net-Language Fundamentals

Execution result

Compiling the code

Page 6: 1 VB net-Language Fundamentals

Creating your first VB windows application

Page 7: 1 VB net-Language Fundamentals

Tool box

Form

Properties Box

Solution Explorer

Page 8: 1 VB net-Language Fundamentals

Editing project settings

Page 9: 1 VB net-Language Fundamentals

Writing the first windows application

Page 10: 1 VB net-Language Fundamentals

On the button click write thisPrivate Sub Button1_Click(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBox.Show("Hello World", _

"A first look at vb", _

MessageBoxButtons.OK, _

MessageBoxIcon.Information)

End Sub

Page 11: 1 VB net-Language Fundamentals

Execution of the Program

Page 12: 1 VB net-Language Fundamentals

ByVal and ByRef in VB.net• The word ByVal is short for “By Value”

• This means passing a copy of a variable to your Subroutine in which case changes that are made are made to the copy and the original will not be altered.

• ByRef is short for “By Reference”.

• This means that you are not handing over a copy of the original variable but pointing to the original variable.

Page 13: 1 VB net-Language Fundamentals

Example • Let us consider the example

Dim Number1 As Integer Number1 = 10

Call IncrementVariable(Number1)MsgBox(Number1)-------------------------------------Private Sub IncrementVariable(ByVal Number1 As

Integer) Number1 = Number1 + 1

End Sub

• Try the code with ByRef Number1 as Integer as argument to the function

Page 14: 1 VB net-Language Fundamentals

Fundamental Data Types in VBVB type Range

Boolean True or false

Byte 0 to 255 ( unsigned 8 bit)

Short -32768 to 32767 (signed 16 bit )

Char Any Unicode value (new to vb.net)

U0000 to Uffff (16 bit unicode character)

Date -The range of values is from midnight

on January 1, 0001 (0001-01-01T00:00:00) through 1 second before midnight on December

31, 9999 (9999-12-31T23:59:59).

Page 15: 1 VB net-Language Fundamentals

VB type Range

Long 9223372036854775808 through

9223372036854775807.

Float 1.5*10-45 to 3.4*1038 (32 bit floating point number)

Double 5.0*10-324 to 1.7*10308 (64 bit floating point number)

Decimal 100 to 1028(96-bit signed number)

String Limited by system memory (Represents a set of unicode characters)

Single -3.40282347E38

through 3.40282347E38.(32 bit value) Object Any type can be stored in an object

variable(Base class of all types in .NET)

Page 16: 1 VB net-Language Fundamentals

The System Data TypesVB type System Type Range

Byte System.Byte 0 to 255 ( unsigned 8 bit)

Short System.Int16 -32768 to 32767 (signed 16 bit )

Char System. Char Any Unicode character

U0000 to Uffff (16 bit unicode character)

Date System.DateTime -The range of values is from midnight

on January 1, 0001 (0001-01-01T00:00:00) through 1 second before midnight on December

31, 9999 (9999-12-31T23:59:59).

Page 17: 1 VB net-Language Fundamentals

VB type System Type Range

Long System.Int64 9223372036854775808 through

9223372036854775807. Float System.Single 1.5*10-45 to 3.4*1038 (32 bit floating point

number)

Double System.Double 5.0*10-324 to 1.7*10308 (64 bit floating point number)

Boolean System.Boolean true or false

Decimal System.Decimal 100 to 1028(96-bit signed number)

String System.String Limited by system memory (Represents a set of unicode characters)

Single System.Single -3.40282347E38

through 3.40282347E38.(32 bit value)

Object System.Object Any type can be stored in an object variable(Base class of all types in .NET)

Page 18: 1 VB net-Language Fundamentals

Type Conversion

Page 19: 1 VB net-Language Fundamentals

Declaring and Initializing variables• We declare variables like this: Dim number1 As Integer

Dim number 2 As Integer

• We initialize the variables as: number1 = 3

number2 = 5

• Variable identifiers may be suffixed with type characters that serve to indicate the variable's type. The declaration can be rewritten as

Dim number1% and Dim number2%

Page 20: 1 VB net-Language Fundamentals

List of type charecters

Page 21: 1 VB net-Language Fundamentals

List of literal formats

Page 22: 1 VB net-Language Fundamentals
Page 23: 1 VB net-Language Fundamentals

Introducing Array Types• Array declaration and initialization

– Dim a(4) As Integer;– Dim a as String ={“first” ,”second”, “third”}

– Dim a(,) As Integer{{1,2},{3,4},{5,6}}

• The above declaration produces the following array:

a(0,0)=1 a(0,1)=2a(1,0)=3 a(1,1)=4a(2,0)=5 a(2,1)=6

Page 24: 1 VB net-Language Fundamentals

• For Allocating arrays dynamically we use the “New” keyword

Dim a() As Integer a=New Integer(4){1,2,3,4}

• If the array elements won't be initialized by the allocation, it is still necessary to include the curly brackets:

Dim a( ) As Integer a = New Integer(5) {}

Can you figure out what is the meaning of:

Dim a() As Integer?

Page 25: 1 VB net-Language Fundamentals

Access Modifiers

Page 26: 1 VB net-Language Fundamentals

Operators

+ - / * ++ -- %

== <> < > >= <=TypeOf…IS

Is Like

&& || ! & | ^ ~

Relational Operators

Logical Operators

Arithmetic Operators

Shift Operators

>> <<

Page 27: 1 VB net-Language Fundamentals

= >= <= *= /= &= |= ^= <<= >>= %=

Assignment Operators

Page 28: 1 VB net-Language Fundamentals

Looping Statements

FOR Loop :

For variable = expression To expression [ Step

expression ]statements Next [ variable_list ]

• Eg: Dim answer As Integer

Dim startNumber As Integer

answer = 0

For startNumber = 1 To 4

answer = answer + startNumber

Next startNumber

MsgBox answer

Page 29: 1 VB net-Language Fundamentals

do loopDo While [expression]

statements

Loop• Eg: Dim number as Integer

number = 1Do While number < 5

MsgBox numbernumber = number + 1

Loop

Do…Until loopDo Until [expression]

statements Loop

Page 30: 1 VB net-Language Fundamentals

• Eg: Do Until number < 5

MsgBox numbernumber = number + 1

Loop

For Each For Each variable In expression

statements

Next [ variable ]

• EgDim a( ) As Integer = {1, 2, 3, 4, 5}

Dim b As Integer

For Each b In a

Console.WriteLine(b)

Next

Page 31: 1 VB net-Language Fundamentals

Decision constructsIf/Else statement

If expression Then

statements

End If

also

If expression Then

statements

Else

statements

End If

Condition must evaluate to bool value

Page 32: 1 VB net-Language Fundamentals

Select Case statements

Dim creamcake As StringDim DietState As String creamcake= TextBox1.TextSelect Case creamcake

Case "Eaten"DietState = "Diet Ruined"

Case "Not Eaten"DietState = "Diet Not Ruined"

Case ElseDietState = "Didn't check"

End SelectMsgBox DietState


Recommended