12
Visual Basic.NET • Declaring Variables – Dim x As Integer – Dim y As Integer = 10 • Comments – ' comment • Assignment Statement – var= newvalue • Constant – Const Pi As Double = 3.14159

Visual Basic.NET

Embed Size (px)

DESCRIPTION

Visual Basic.NET. Declaring Variables Dim x As Integer Dim y As Integer = 10 Comments ' comment Assignment Statement var= newvalue Constant Const Pi As Double = 3.14159. Visual Basic.NET Data Types. Visual Basic .NET Runtime Storage Range of Values Data Type Data Type Size - PowerPoint PPT Presentation

Citation preview

Page 1: Visual Basic.NET

Visual Basic.NET

• Declaring Variables– Dim x As Integer– Dim y As Integer = 10

• Comments– ' comment

• Assignment Statement– var= newvalue

• Constant– Const Pi As Double = 3.14159

Page 2: Visual Basic.NET

Visual Basic.NET Data TypesVisual Basic .NET Runtime Storage Range of ValuesData Type Data Type Size============================================================Boolean System.Boolean 4 bytes True or False

Byte System.Byte 1 byte 0 to 255 (unsigned)

Char System.Char 2 bytes 1 Unicode “” character

Date System.DateTime 8 bytes January 1, 0001 to December 31,9999

Decimal System.Decimal 12 bytes +/-0.0 79,228,162,514,264,337,

593,543,950,335

Double System.Double 8 bytes -1.797693134862310.0E308 to -4.94065645841247E-324

Integer System.Int32 4 bytes -2,147,483,648 to 2,147,483,647

Long System.Int64 8 bytes -9,223,372,036,854,775,80

to 9,223,372,036,854,775,807

Object System.Object 4 bytes Any data type Depends on usage.

Short System.Int16 2 bytes -32,768 to 32,7670

Single System.Single 4 bytes -3.402823E38 to 0.0

String System.String 10 bytes 0 to approximately billion Unicode char

Page 3: Visual Basic.NET

Data Types• Option Explicit: forces variable declaration • Option Strict: forces variable declaration + will not

allow conversions where data loss would occur.

• Data Type Conversion– CType (variable_name, New type)– ToString method is available in all data types

• Members available for data types– String (Trim, Length, Substring, Replace,..)– Array (Length, GetUpperBound, Clear, Sort,..)– DateTime (Now,Today,Year,Day,Hour, Add, Subtract,

IsLeapYear, DaysInMonth, …)

Page 4: Visual Basic.NET

VB.NET OperatorsType Operator Purpose Example

Math + Add 5 + 2 = 7– Subtract 5 – 2 = 3* Multiply 5 * 2 = 10/ Divide 5 / 3 = 2.5\ Integer Divide 5 \ 2 = 2^ Exponentiation 5^2 = 25Mod Remainder 5 mod 2 =1

String + Concatenate “one” + “two” = “onetwo”& Concatenate “one” & “two” = “onetwo”

Assignment = Assigns the value of expression to variable x = 5 + 3

Math += Adds the value x += y

Page 5: Visual Basic.NET

Type Operator Purpose Example

-= Subtracts the x -= y*= Multiplies the x *= y/= Divides the x /= y\= Integer divides x \= y&= Concatenates the x &= y^= Exponentiates x ^= y

Comparison = Is equal to If (x = y)< Is less than If (x < y)<= Is less than or If (x <=equal to y)> Is greater than If (x > y)>= Is greater than If (x >=or equal to y)<> Is not equal to If (x <> y)Like Matches a pattern If (x Like “p??r”)Is Do object variables refer

to same object If (x Is y)

Logical And True if both expressionsare true If (x = 3 AND y=4)

Or True if one or If (x = 3 OR y=4) both expressions are true

Not True if expression is false If Not (x =5)Xor True if one expression is If (x = 3 Xor y=4)

true, but not both

Page 6: Visual Basic.NET

Arrays

• Specify upper bound of array, starting at index 0Dim Names(9) As String `10 elements 0 to 9Dim Num () As Integer = { {1,6} , {7,5} , {5,9} }

Names(0) = “Ahmed”Num (2,1)=4ReDim Preserve Names(2)

For Each e in Numprint e `Read only elements

Next

Page 7: Visual Basic.NET

Enumeration

Enum UserTypeAdmin =3Guest = 2User= 9

End Enum

Dim user As UserType = UserType.GuestDim user2 As UserType = 9

Page 8: Visual Basic.NET

Branching Structures

If (condition) Then

code

Elseif (condition) then

another code

Else

final code

End if

Page 9: Visual Basic.NET

Branching Structures

Select Case expresssionCase 1

code 1Case 2

code2Case 3

code3Case Else

Default codeEnd Select

Page 10: Visual Basic.NET

Looping

Do While (condition)code to repeat

Loop___________________Do

code to repeatLoop until (condition)• Exit Do `to get out of a loop

Page 11: Visual Basic.NET

Looping

For i = init_value To final_valueCode to repeat

Next

• Exit for ` to get out of loop

Page 12: Visual Basic.NET

Subroutines and Functions

• SubroutinesSub HelloWorld()

Response.Write(“Hello World”)End Sub

You call a sub using either of the following statements:HelloWorld()Call HelloWorld()

• Functions: can return a value to calling programFunction SayHello() As String

Return “Hello World”End Function

• NOTE– VB.NET requires parentheses around argument lists– The default is to pass parameters by value otherwise use “byref” keyward– Overload functions of procedures using the “overloads” keywaord