Date post: | 19-May-2015 |
Category: |
Technology |
Upload: | hock-leng-puah |
View: | 1,016 times |
Download: | 2 times |
Week 1 – 3 Recap
Useful links/info:
TPITVB.blogspot.com
VB.NET Week 1Outline: Introduction Simple walk through Coding Conventions
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
VB.NET Week 2Outline: Variables Strings Operators Maths helper functions Application: Create a Calculator for addition
Variables - DeclaringDeclare a variable using the Dim and As
keywords:Dim sumOfNumber As Integer
Name of variable: sumOfNumberType: Integer
(Dim: Short for dimension)
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
Java8 primitive types:
byteshortintlongfloatdoublebooleanchar
String – not primitive type
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.
Variables - assigningDeclaring and Assigning Variables with a
Default Value Dim sumOf Number As Integer = 0Dim nameOfPerson As String = "None"
Arithmetics Operators
+ Addition
- Subtraction
* Multiplication
/ Division
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
Do not have these operatorsJava++- -
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
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)
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)
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)
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
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))
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?
VB.NET lesson week Get Input from User If Then Else, Else If Select Case Add condition checking into Simple
Calculator
Get Input from User
Dim name As String
name = InputBox("Enter your name:")
MsgBox("Your name is " & name)
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
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
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
Short cut If and AssignmentIIf Immediate If
x = IIf( i > 0, 1, 0)
Same As
If i > 0 Thenx = 1
Elsex = 0
End If
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
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 .
Javaif (condition) {
:} else if (condition 2) {
} else {:
}
switch variable {case 1:
:break;
case 2::break;
default::
}
End of lessons