Visual Basic Cheat Sheet
<class or object name>.<member name> (the period is called the dot operator)
txtTotal.Text = 10 Assigns the value 10 to the Text property of the
text box named txtTotal.
txtTotal.ReadOnly = True Assigns the True value to the ReadOnly property
of the text box named txtTotal so the user can’t
change its contents.
TxtMonthlyInvestment.Select() Uses the Select method to move the focus to
the text box named txtMonthlyInvestment.
Me.Close() Uses the Close method to close the form that
contains the statement. In this example, Me is a
keyword that is used to refer to the current
instance of the form class.
BtnExit.Click Refers to the Click event of a button named
btnExit.
Public Class frmInvoiceTotal
Private Sub btnCalculate_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnCalculate.Click
Dim discountPercent As Decimal
If txtSubtotal.Text >= 500 Then
discountPercent = 0.2
ElseIf txtSubtotal.Text >= 250 _
And txtSubtotal.Text < 500 Then
discountPercent = 0.15
ElseIf txtSubtotal.Text >= 100 _
And txtSubtotal.Text < 250 Then
discountPercent = 0.1
Else
discountPercent = 0
End If
Dim discountAmount As Decimal = _
txtSubtotal.Text * discountPercent
Dim invoiceTotal As Decimal = _
txtSubtotal.Text - discountAmount
txtDiscountPercent.Text = _
FormatPercent(discountPercent, 1)
txtDiscountAmount.Text = _
FormatCurrency(discountAmount)
txtTotal.Text = FormatCurrency(invoiceTotal)
txtSubtotal.Select()
End Sub
Private Sub btnExit_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnExit.Click
Me.Close()
End Sub
End Class
A statement that gets the name of the current user of an application
lblName.Text = My.User.Name
A statement that checks if a directory exists
If My.Computer.FileSystem.DirectoryExists( _
"C:\VB 2005\Files") Then ...
A statement that creates an instance of a form and displays it
My.Forms.frmInvestment.Show()
The built-in value types
Keyword Bytes .NET Type Description
Byte 1 Byte Positive integer value from 0 to 255
Sbyte 1 Sbyte Signed integer value from -128 to 127
Short 2 Int16 Integer from -32,768 to +32,767
Ushort 2 Uint16 Unsigned integer from 0 to 65,535
Integer 4 Int32 Integer from -2,147,483,648 to +2,147,483,647
Uinteger 4 Uint32 Unsigned integer from 0 to 4,294,967,295
Long 8 Int64 An integer from
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Ulong 8 Uint64 An unsigned integer from
0 to +18,446,744,073,709,551,615
Single 4 Single A non-integer number with approximately 7
significant digits
Double 8 Double A non-integer number with approximately 14
significant digits
Decimal 16 Decimal A non-integer number with up to 28 significant
digits (integer and fraction) that can represent
values up to 79,228 x 1024
Char 2 Char A single Unicode character
Boolean 1 Boolean A True or False value
How to declare and initialize a variable
Syntax
Dim variableName As type [= expression]
Examples
Dim counter As Integer
Dim numberOfBytes As Long = 20000
Dim price As Double = 14.95
Dim interestRate As Single = 8.125
Dim total As Decimal = 24218.1928D
' D indicates a Decimal value
Dim letter As Char = "A"C ' C indicates a Char value
Dim valid As Boolean = True
Dim x, y As Integer
' declare 2 variables with 1 statement
Dim i As Integer, d As Decimal
' declare 2 variables with different types
How to declare and initialize a constant
Syntax
Const ConstantName As type = expression
Examples
Const DaysInNovember As Integer = 30
Const SalesTax As Decimal = .075D
Arithmetic operators
OperatorName
+ Addition
- Subtraction
* Multiplication
/ division
\ Integer division
Mod Modulus
^ Exponentiation
+ Positive sign
- Negative sign
Examples of arithmetic expressions
Integer arithmetic
Dim x As Integer = 14
Dim y As Integer = 8
Dim result1 As Integer = x + y ' result1 = 22
Dim result2 As Integer = x - y ' result2 = 6
Dim result3 As Integer = x * y ' result3 = 112
Dim result4 As Integer = x \ y ' result4 = 1
Dim result5 As Integer = x Mod y ' result5 = 6
Dim result6 As Integer = -y + x ' result6 = 6
Decimal arithmetic
Dim a As Decimal = 8.5D
Dim b As Decimal = 3.4D
Dim result11 As Decimal = a + b ' result11 = 11.9
Dim result12 As Decimal = a - b ' result12 = 5.1
Dim result13 As Decimal = a / b ' result13 = 2.5
Dim result14 As Decimal = a * b ' result14 = 28.90
Dim result15 As Decimal = a Mod b ' result15 = 1.7
Dim result16 As Decimal = -a ' result16 = -8.5
Assignment operators
OperatorName
= Assignment
+= Addition
-= Subtraction
*= Multiplication
/= Division
\= Integer division
Statements that use the same variable on both sides of the equals sign
total = total + 100D
total = total - 100D
price = price * .8D
Statements that use the shortcut assignment operators
total += 100D
total -= 100D
price *= .8D
The order of precedence for arithmetic operations
Exponentiation
Positive and negative
Multiplication, division, integer division, and modulus
Addition and subtraction
How implicit casting works
Widening conversions
Byte®Short®Integer®Long®Decimal ®Single®Double
Implicit casts for widening conversions
Dim grade As Double = 93 ' convert Integer to Double
Dim a As Double = 6.5
Dim b As Integer = 6
Dim c As Integer = 10
Dim average As Double = (a + b + c) / 3
' convert b and c to Double values
' average = 7.5 (22.5 / 3)
Implicit casts for narrowing conversions
Dim grade As Integer = 93.75 ' grade = 93 (truncated)
Dim subtotal As Decimal = txtSubtotal.Text
' may throw an exception
Dim average As Integer = (a + b + c) / 3 ' average = 7
How to code explicit casts with the CInt and CDec functions
Dim grade As Integer = CInt(93.75)
' CInt converts to Integer with rounding
' (grade = 94)
Dim subtotal As Decimal = CDec(txtSubtotal.Text)
' CDec converts to Decimal
' May throw an exception
Dim average As Integer = CInt((CInt(a) + b + c) / 3)
' convert a and the calculated result to Integer values;
' average = 8
Four shared methods of the Math class
The syntax of the Round method
Math.Round(number[, precision])
The syntax of the Sqrt method
Math.Sqrt(number)
The syntax of the Min and Max methods
Math.{Min|Max}(number1, number2)
Statements that use shared methods of the Math class
Dim orderTotal As Double = Math.Round(orderTotal, 2)
Dim shipWeight As Integer = _
CInt(Math.Round(shipWeightDouble))
Dim sqrtX As Double = Math.Sqrt(x)
Dim maxSales As Double = _
Math.Max(lastYearSales, thisYearSales)
Dim minQty As Integer =
Math.Min(lastYearQty, thisYearQty)
Results from shared methods of the Math class
StatementResult
Math.Round(23.75)24
Math.Round(23.754, 2)23.75
Math.Round(23.755, 2)23.76
Math.Sqrt(20.25)4.5
Math.Max(23.75, 20.25)23.75
Math.Min(23.75, 20.25)20.25
How to declare and initialize a string
Dim message1 As String = "Invalid data entry"
Dim message2 As String = ""
Dim message3 As String = Nothing
How to join strings
Dim firstName As String = "Bob" ' firstName is "Bob"
Dim lastName As String = "Smith" ' lastName is "Smith"
Dim name As String = firstName & " " & lastName
' name is "Bob Smith"
How to join a string and a number
Dim price As Double = 14.95
Dim priceString As String = "Price: " & price
' priceString is "Price: 14.95"
How to append one string to another string
Dim firstName As String = "Bob" ' firstName is "Bob"
Dim lastName As String = "Smith" ' lastName is "Smith"
Dim name As String = firstName & " "
' name is "Bob "
name = name & lastName ' name is "Bob Smith"
How to append one string to another with the &= operator
Dim firstName As String = "Bob" ' firstName is "Bob"
Dim lastName As String = "Smith" ' lastName is "Smith"
Dim name As String = firstName & " "
' name is "Bob "
name &= lastName ' name is "Bob Smith"
Some of the Visual Basic functions for data conversion
FunctionConverts the expression to the…
CDbl(expression)Double data type
CDec(expression)Decimal data type
CInt(expression)Integer data type; any fractional portion is rounded to the nearest whole number
CLng(expression)Long data type
CStr(expression)String type
CType(expression, Typename)Specified type, which can be any type of object
Examples of the data conversion functions
Dim grade As Integer = CInt(93.75) ' grade = 94
Dim subtotal As Decimal = CDec(txtSubtotal.Text)
' may throw an exception
Dim a As Double = 6.5
Dim b As Integer = 6
Dim c As Integer = 10
Dim average As Integer = CInt((CInt(a) + b + c) / 3) ' average = 8
Dim salesString As String = "$2,574.98"
Dim salesDecimal As Decimal = CDec(salesString)
' CDec accepts $ , and .
Dim salesString = CStr(salesDecimal) ' salesString value = 2574.98
Dim salesString = CType(salesDecimal, String) ' same result as CStr
Common methods for data conversion
MethodDescription
ToString([format])A method that converts the value to its equivalent string representation using the specified format. If the format is omitted, the value isn’t formatted.
Parse(string)A shared method that converts the specified string to an equivalent data value.
Some of the shared methods of the Convert class
MethodDescription
ToDecimal(value)Converts the value to the Decimal data type.
ToDouble(value)Converts the value to the Double data type.
ToInt32(value)Converts the value to the Integer data type.
ToChar(value)Converts the value to the Char data type.
ToBool(value)Converts the value to the Boolean data type.
ToString(value)Converts the value to the String data type.
Conversion statements that use the ToString and Parse methods
Dim sales As Decimal = 2574.98D
Dim salesString As String = sales.ToString ' decimal to string
sales = Decimal.Parse(salesString) ' string to decimal
An implicit call of the ToString method
Dim price As Decimal = 49.5D
Dim priceString As String = "Price: $" & price
' automatic ToString call
Conversion statements that use the Convert class
Dim subtotal As Decimal
subtotal = Convert.ToDecimal(txtSubtotal.Text) ' string to decimal
Dim years As Integer = Convert.ToInt32(txtYears.Text)
' string to integer
txtSubtotal.Text = Convert.ToString(subtotal) ' decimal to string
Dim subtotalInt As Integer = Convert.ToInt32(subtotal)
' decimal to integer