1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As...

16
1 Microsoft® Visual Basic® .NET Language # 1

Transcript of 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As...

Page 1: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

1

Microsoft®Visual Basic® .NET

Language# 1

Page 2: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

2

Variable

Declaring Variable

Dim {VariableName} As {Type}

Dim {VariableName} As {Type} = {value}

Example:

Dim var1 As String

Dim Var2, Var3, Var4 As String

Example:

Dim var1 As String = "Hello World"

Page 3: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

3

Data Types

Five Categories

Numeric

Boolean

String

Date

Object

Integrated in the common language runtime

2 Varieties of Data Types

Value Types

Reference Types

Page 4: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

4

Numeric Data Types

Data Type Memory Representation

Stores

Byte 1 byte Positive integer value0 to 255

Short (Int16) 2 bytes Integer value -32768 to 32767

Integer (Int32)

4 bytes Integer value -2,147,483,648 to 2,147,483,647

Long (Int64) 8 bytes Vary large integer value.-263 to (263-1)

Single 4 bytes Single-precisionfloating-point number.

Double 8 bytes Double-precisionfloating-point number.

Decimal 16 bytes Vary-Vary large integer and floating-point number

Page 5: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

5

Not Number Data Types.

Boolean True / False

String Store only text data.

Character Store only one character.

DateTime Store Date and Time Data.

Example :Var1 = #01/01/2003#Var2 ="Sep 1,1977"Var3 = #01/01/2003 6:25:11 PM#Var4 = Now()

Page 6: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

6

Value Types (Numeric, Boolean, String, Date)

Store Data in Stack Memory

Function A

Dim Var1 As String = "AAA"

Call B

End Function

Function B

Dim Var2 As String = "BBB"

Call C

End Function

Function C

Dim Var3 As String = "CCC"

End Function

Stack Memory

(LastInFirstOut)

Var1="AAA"

Var2="BBB"

Var3="CCC"

Page 7: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

7

Reference Types (Object)

Store Variable in Stack Memory

Store Data in Heap Memory

Function A

Dim Var1 As ObjA

Var1 = New ObjA

Var1.Data = "ZZZ"

End Function

Stack Memory

Var1

ObjA

Heap Memory

Data = "ZZZ"

GC

Page 8: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

8

Data Type Functions

GetType( {Variable} ) IsNumeric( {Variable} ) IsDate( {Variable} ) IsReference( {Variable} )

Conversion Data Types

CBool CByte CChar

CDate CDbl CDec

CInt CLng CShort

CSng CStr CType

Page 9: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

9

Parse Method

To convert a numeric string to a numeric or others, use the Parse method

To Ignore Commas, Use the NumberStyles.AllowThousands Flag

Dim MyString As String = “12345”

Dim MyInt As Integer = Integer.Parse(MyString)

MyInt += 1

Console.WriteLine(MyInt)

‘ The output to the console is “12346”

Dim MyString As String = “123,456”

Dim MyInt As Integer = Integer.Parse(MyString, _

Globalization.NumberStyles.AllowThousands)

Console.WriteLine(MyInt)

‘ The output to the console is “123456”

Page 10: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

10

Constant

Once a constant has been declared

Cannot change value.

Process faster than variable.

Const { ConstantName } As Type = Value

Example :

Const pi As Double = 3.14159265358979

Page 11: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

11

Array

Set of data which have the same type.

Zero based.

Declaring Array

Dim MyString (5) As String

MyString(3) = "Happy"

Dim str as String

str = MyString(3) ‘str = "Happy"

MyString(5)

MyString(4)

MyString(3) Happy

MyString(2)

MyString(1)

MyString(0)

Page 12: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

12

String

Trim method

remove spaces

Pad method

Expand a specific number of characters

Dim MyString As String = “ Big ”

Dim TrimString As String = MyString.Trim()

Console.WriteLine(TrimString)

‘The output to the console is “Big”

Dim MyString As String = “Hello World!”

Console.WriteLine(MyString.PadLeft(20, “-”))

‘The output to the console is “--------Hello World!”

Page 13: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

13

Changing Case

Methods that compare strings and characters are case-sensitive

Convert the case of strings that are entered by users before comparing them

To change the case of a string use:

String.ToUpper

String.ToLower

Dim MyString As String = “hello world!”

Console.WriteLine(MyString.ToUpper())

‘The output to the console is “HELLO WORLD!”

Dim MyString As String = “HELLO WORLD!”

Console.WriteLine(MyString.ToLower())

‘The output to the console is “hello world!”

Page 14: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

14

Split and Join

Split method breaks up a string into an array of substrings

String is broken at positions indicated by the specified separator characters parameter

If the separator parameter is Nothing, the white-space characters are assumed to be the separator

Join method concatenates a specified separator between string array

A specified separator string is placed between each element of a string array

Dim Line As String = “hello world”

Dim Words() As String = Line.Split(“ ”)

‘Words(0) = “hello” and Words(1) = “World”

Dim MyString() As String = {“apple”, “orange”, “grape”, “pear”}

Dim JoinString as String = String.Join(“/”, MyString, 0, 3)

‘JoinString = “apple/orange/grape”

Page 15: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

15

Length and IndexOf

Length Method

Gets the number of characters in this instance

IndexOf Method

Reports the index of the first occurrence of a string or one or more characters, within this instance.

Dim MyString As String = “Hello World!”

Console.WriteLine(MyString.Length())

‘The output to the console is “12”

Dim MyString As String = “Hello World!”

Console.WriteLine(MyString.IndexOf(“or”)

‘The output to the console is “7”

Page 16: 1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:

16

Operators

Arithmetic Operators

+ - * / += -= *= /=

A = 2 + 3 B += 5 C = B / 2

String Operators

+ &

Message = "Hello " + Name

Boolean Operators

= < > <= >= <>

And Or Not AndAlso OrElse

If A=5 AndAlso B>6 Then