Top Banner
Week 1 – 3 Recap Useful links/info: TPITVB.blogspot.com
29

Vb.Net 01 To 03 Summary Upload

May 19, 2015

Download

Technology

Hock Leng PUAH

Vb.Net 01 To 03 Summary Upload
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Vb.Net 01 To 03 Summary Upload

Week 1 – 3 Recap

Useful links/info:

TPITVB.blogspot.com

Page 2: Vb.Net 01 To 03 Summary Upload

VB.NET Week 1Outline: Introduction Simple walk through Coding Conventions

Page 3: Vb.Net 01 To 03 Summary Upload

Coding ConventionsAll keywords – Start with Capital

Example: Dim As Integer String BooleanIf Then Else ElseIf End If Select Case Case End Select

All variable name – Start with small letterExample: i x y nameOfVariable existingString

total sumOfNumber

Page 4: Vb.Net 01 To 03 Summary Upload

VB.NET Week 2Outline: Variables Strings Operators Maths helper functions Application: Create a Calculator for addition

Page 5: Vb.Net 01 To 03 Summary Upload

Variables - DeclaringDeclare a variable using the Dim and As

keywords:Dim sumOfNumber As Integer

Name of variable: sumOfNumberType: Integer

(Dim: Short for dimension)

Page 6: Vb.Net 01 To 03 Summary Upload

Variables - DeclaringData Type Summary

Boolean True, False

Date 8 bytes to store date and time

Double 8 bytes to store decimals(others: Single, Decimal)

Integer 4 bytes to store whole number(others: Short, Long)

String word or sentence(for just one character: Char)

Single: single-precision decimal (floating point)

Double: double-precision decimal

Page 7: Vb.Net 01 To 03 Summary Upload

Java8 primitive types:

byteshortintlongfloatdoublebooleanchar

String – not primitive type

Page 8: Vb.Net 01 To 03 Summary Upload

Variables - assigningAssign a value to your variable with the =

sign, which is sometimes called the assignment operator

sumOfNumber = 42 This line of code takes the value 42 and stores

it in the previously declared variable named sumOfNumber

The equals sign is not actually an equals sign. The = sign here means assign a value of.

Page 9: Vb.Net 01 To 03 Summary Upload

Variables - assigningDeclaring and Assigning Variables with a

Default Value Dim sumOf Number As Integer = 0Dim nameOfPerson As String = "None"

Page 10: Vb.Net 01 To 03 Summary Upload

Arithmetics Operators

+ Addition

- Subtraction

* Multiplication

/ Division

Page 11: Vb.Net 01 To 03 Summary Upload

Other Arithmetic Operators^ to the power ofmod remainder after division (Java:%)\ division but return only integer

portion

Short cut:+= x += y same as x = x + y-= x -= y same as x = x - y*= x *= y same as x = x * y/= x /= y same as x = x / y

Page 12: Vb.Net 01 To 03 Summary Upload

Do not have these operatorsJava++- -

Page 13: Vb.Net 01 To 03 Summary Upload

Comparison OperatorsOperator Examples

= (equals) 5 = 4 (false)

4 = 5 (false)

4 = 4 (true)

<> (not equal to) 5 <> 4 (true)

4 <> 5 (true)

4 <> 4 (false)

Comparison operators

are used to in condition

Eg If (condition) doSomthing

Page 14: Vb.Net 01 To 03 Summary Upload

Comparison OperatorsOperator Examples

> (greater than) 5 > 4 (true)

4 > 5 (false)

4 > 4 (false)

< (less than) 5 < 4 (false)

4 < 5 (true)

4 < 4 (false)

Page 15: Vb.Net 01 To 03 Summary Upload

Comparison OperatorsOperator Examples

>= (greater than or equal to)

5 >= 4 (true)

4 >= 5 (false)

4 >= 4 (true)

<= (less than or equal to)

5 <= 4 (false)

4 <= 5 (true)

4 <= 4 (true)

Page 16: Vb.Net 01 To 03 Summary Upload

Conversion from String to other types

Use CType if you are not sure the name of

the conversion function

Example: to convert to Boolean

CType(variableName, Boolean)

Page 17: Vb.Net 01 To 03 Summary Upload

Type Conversion examplesDim inputString As String = TextBox1.Text

Dim x As Integer x = CInt(inputString) ' Convert to Integer.

Dim y As Integer = 8Textbox1.Text = CStr(y) ' Convert to String.

c i n t

c s t r

Page 18: Vb.Net 01 To 03 Summary Upload

Maths FunctionsMath.Sqrt(n): Return the square root of n.Math.Abs(n): Return the absolute value of n.Math.Sign(n): Return the sign of n (-1, 0 or +1).

Example: Dim x As Integer

Dim y As Integer = 25

TextBox1.Text = Math.Sqrt(y)

‘ MsgBox("Sq root of 25 is " & CStr(x))

Page 19: Vb.Net 01 To 03 Summary Upload

Class vs Local variablesPublic Class Form1

Dim x As Integer = 0 ' Class variables.Dim name As String = ""

Sub Button0_Click …Dim y As Integer = 9 ' Local sub variables.

End Sub

:End Class

x = 5 ' OK or Not?y = 5 ' OK or Not?

x = 5 ' OK or Not?y = 5 ' OK or Not?

Page 20: Vb.Net 01 To 03 Summary Upload

VB.NET lesson week Get Input from User If Then Else, Else If Select Case Add condition checking into Simple

Calculator

Page 21: Vb.Net 01 To 03 Summary Upload

Get Input from User

Dim name As String

name = InputBox("Enter your name:")

MsgBox("Your name is " & name)

Page 22: Vb.Net 01 To 03 Summary Upload

If Then Else, Else IfSyntax

If condition Then

:

End If

Else

:

ElseIf condition2 Then ' Can repeat for

: ' more conditions

Example

If name <> "" Then

MsgBox(name)

End If

Example

If name <> "" Then

MsgBox(name)

Else

MsgBox("Empty")

End If

Example

If name = "" Then

MsgBox("Empty")

ElseIf name = "me" Then

MsgBox("Me")

Else

MsgBox(name)

End If

Page 23: Vb.Net 01 To 03 Summary Upload

More than one conditionAnd condition1 And condition2

AndAlso condition1 AndAlso condition2Recommended - Short-circuit And

Eg If i > 0 AndAlso j <> 5 Then

Once i > 0 is false, no need to check j <> 5

Page 24: Vb.Net 01 To 03 Summary Upload

More than one conditionOr condition1 Or condition2

OrElse condition1 OrElse condition2Recommended - Short-circuit Or

Eg If i > 0 OrAlso j <> 5 Then

Once i > 0 is true, no need to check j <> 5

Page 25: Vb.Net 01 To 03 Summary Upload

Short cut If and AssignmentIIf Immediate If

x = IIf( i > 0, 1, 0)

Same As

If i > 0 Thenx = 1

Elsex = 0

End If

Page 26: Vb.Net 01 To 03 Summary Upload

Select CaseSyntax

Select Case variable

Case condition1

:

Case condition2

:

Case Else

:

End Select

Example

Select Case name

Case ""

MsgBox("Empty")

Case "Me"

MsgBox("me")

Case Else

MsgBox(name)

End Select

Page 27: Vb.Net 01 To 03 Summary Upload

Select Case ConditionsCase "red", "white", "green" ' A few together .

Case 1 To 10 ' A range using

' keyword To .Case Is > 9 ' A range using

' keyword Is .

Page 28: Vb.Net 01 To 03 Summary Upload

Javaif (condition) {

:} else if (condition 2) {

} else {:

}

switch variable {case 1:

:break;

case 2::break;

default::

}

Page 29: Vb.Net 01 To 03 Summary Upload

End of lessons