Vb.Net 01 To 03 Summary Upload - Presentation Transcript
Week 1 – 3 Recap
Useful links/info:
TPITVB.blogspot.com
VB.NET Week 1
Outline:
Introduction
Simple walk through
Coding Conventions
Coding Conventions
All keywords – Start with Capital
Example: Dim As Integer String Boolean
If Then Else ElseIf End If
Select Case Case End Select
All variable name – Start with small letter
Example: i x y nameOfVariable existingString
total sumOfNumber
VB.NET Week 2
Outline:
Variables
Strings
Operators
Maths helper functions
Application: Create a Calculator for addition
Variables - Declaring
Declare a variable using the Dim and As keywords:
Dim sumOfNumber As Integer
Name of variable: sumOfNumber
Type: Integer
(Dim: Short for dimension)
Variables - Declaring
Data Type Summary
Single: single-precision decimal (floating point) Double: double-precision decimal True, False Boolean 8 bytes to store date and time Date 8 bytes to store decimals (others: Single, Decimal) Double 4 bytes to store whole number (others: Short, Long) Integer word or sentence (for just one character: Char) String
Java
8 primitive types:
b yte
s hort
i nt
l ong
f loat
d ouble
b oolean
c har
S tring – not primitive type
Variables - assigning
Assign 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 - assigning
Declaring and Assigning Variables with a Default Value
Comparison Operators 4 <= 4 (true) 4 <= 5 (true) 5 <= 4 (false) <= (less than or equal to) 4 >= 4 (true) 4 >= 5 (false) 5 >= 4 (true) >= (greater than or equal to) Examples Operator
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 examples
Dim inputString As String = TextBox1.Text
Dim x As Integer
x = CInt (inputString) ' Convert to Integer.
Dim y As Integer = 8
Textbox1.Text = CStr (y) ' Convert to String.
c i n t c s t r
Maths Functions
Math.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 variables
Public 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 If
Syntax
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 condition
And condition1 And condition2
AndAlso condition1 AndAlso condition2
Recommended - 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 condition
Or condition1 Or condition2
OrElse condition1 OrElse condition2
Recommended - 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 Assignment
IIf I mmediate If
x = IIf( i > 0, 1, 0)
Same As
If i > 0 Then
x = 1
Else
x = 0
End If
Select Case
Syntax
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 Conditions
Case "red", "white", "green" ' A few together .
Case 1 To 10 ' A range using
' keyword To .
Case Is > 9 ' A range using
' keyword Is .
Java
if (condition) {
:
} else if (condition 2) {
} else {
:
}
switch variable { case 1: : break; case 2: : break; default: : }
0 comments
Post a comment