Visual Basic Cheat Sheet
Visual Basic Cheat Sheet
CHEAT
SHEET
&
NOTES
March 20
2013
http://www.thecodingguys.net
http://blog.thecodingguys.net
2013
thecodingguys.net
CONTENTS .......................................................................................................................................................1
ABOUT .............................................................................................................................................................3
LICENSE............................................................................................................................................................3
VB SYNTAX.......................................................................................................................................................4
COMMENTS..................................................................................................................................................... 4
VARIABLES .......................................................................................................................................................4
VARIABLE NAMING ............................................................................................................................................ 5
VARIABLE SYNTAX ............................................................................................................................................. 5
VARIABLE DATA TYPES ....................................................................................................................................... 5
STRINGS ...........................................................................................................................................................6
Example ................................................................................................................................................... 6
STRING FUNCTIONS ........................................................................................................................................... 7
Example ................................................................................................................................................... 7
Exceptions................................................................................................................................................ 7
OPERATORS .....................................................................................................................................................8
EQUALITY / COMPARISON ................................................................................................................................... 8
LOGICAL ......................................................................................................................................................... 8
ARRAYS............................................................................................................................................................9
ARRAY SYNTAX .............................................................................................................................................. 9
Example ................................................................................................................................................... 9
METHODS ...................................................................................................................................................... 10
IF STATEMENTS.............................................................................................................................................. 10
SYNTAX ........................................................................................................................................................ 11
SYNTAX IF ELSE ............................................................................................................................................ 11
SELECT CASE................................................................................................................................................... 11
Example ................................................................................................................................................. 12
WHILE STATEMENT ........................................................................................................................................ 13
Example ................................................................................................................................................. 13
FOR ITERATIONS ............................................................................................................................................ 14
Example ................................................................................................................................................. 14
MANAGING ERRORS ...................................................................................................................................... 15
Example ................................................................................................................................................. 15
This document is not a full tutorial just small cheat sheet and has the
basics of Visual Basic. For a full tutorial and updates go to:
http://www.thecodingguys.net/tutorials/visualbasic/vb-tutorial
http://www.thecodingguys.net/downloads
This work is licensed under the creative commons AttributionNonCommercial-NoDerivs 3.0 Unported
You may not alter, transform, or build upon this work.
You may not use this work for commercial purposes.
You are free to copy, distribute and transmit the work
Visual Basic has a simple syntax and much of the language is like
English, lines dont end with semi-colons and the language is not
case-sensitive so something like A and a are the same thing.
Comments
Variables are a storage location, each variable you create uses some
memory in the computer (RAM).
Variable Naming
Variable Syntax
Variables start with the Dim keyword.
e.g.
Description
Whole Numbers (Integers)
Set of characters
Numbers with decimals
True or False
http://www.thecodingguys.net/tutorials/visualbasic/vb-variables
String.Format Options
Format
{0:C}
{0:N}
Description
Currency Symbol
Dot or Comma (for separating large
numbers}
Percentage Symbol
{0:P}
Example
Dim strFormat As String
strFormat = String.Format("{0:C}", 5)
Console.WriteLine(strFormat)
Console.ReadLine()
This would print out 5, however this depends on your computer
settings so if youre in USA the symbol is $, or in Frances its the euro
sign .
String Functions
There are many functions available so you can format strings, for
example make uppercase, split, substring, or convert a number to a
string.
Example
Dim subStr As String
subStr = "Welcome to thecodingguys!"
Console.WriteLine(subStr.Substring(0, 5))
Console.ReadLine()
This uses the substring function which simply gets the first 5
characters from our string subStr, it starts from 0 and stops at the 5th
character, so the output is Welco.
Exceptions
ArgumentOutOfRangeException
Functions
ToString
Description
Converts an integer (or double)
Toupper
ToLower
Operator
=
<>
<
>
<=
>=
to a string
Formats a string so it is uppercase
Formats a string so it is lowercase
Description
Is equal to
Not equal to
Less than
Greater than
Less than or equal to
Greater than or equal to
Equality / Comparison
Logical
Operator
AND
Or
Description
AND
Or
Array Syntax
Dim array-name (size) As <data type>
Example
Dim games (5) As string
Arrays are accessed by their index number.
Games(0) = "GTA 4"
Games(1) = "Battlefield 3"
Games(2) = "SWAT 4" ' I LOVE THIS GAME!
Games(3) = "Arma 2"
Games(4) = "RollerCoaster Tycoon 3" '
Games(5) = "GRID
Arrays are access by the index value, arrays start at 0 so to print out
Arma 2 you would write
Console.WriteLine(games(3))
http://www.thecodingguys.net/tutorials/visualbasic/vb-arrays
Methods (or procedures) are very good for reusing code and making
code more understandable. A method is just a block of code which
you can call.
Syntax
If condition Then
End If
Syntax If Else
If condition Then
code to execute
Else
code to execute
End If
http://www.thecodingguys.net/tutorials/visualbasic/vb-if-statement
Syntax
Select Case VariableName
Case 1
code
Case 2
Case Else
code
End Select
Example
Dim game As String
Console.WriteLine("Your favourite game?")
game = Console.ReadLine()
Dim message As String
message = game & " is your favourite game!"
Select Case game
Case "GTA 4"
Console.WriteLine(message)
Case "Battlefield 3"
Console.WriteLine(message)
Case "GRID"
Console.WriteLine(message)
Case "Infamous"
Case Else
Console.WriteLine("Looks like your
game is not on my list")
End Select
Console.ReadLine()
http://www.thecodingguys.net/tutorials/visualbasic/vb-select-case
The while statement runs a piece of code, while its condition is true.
SYNTAX
While variable True
Code to execute
Exit While
End While
Example
This example reads a text file.
The For Next Loop will iterate through data, commonly used to print out a list
of numbers or anything similar!
SYNTAX
For index = 1 To 50
code
Next
Console.ReadLine()
Example
For i = 1 To 50
Console.WriteLine(i)
Next
Console.ReadLine()
http://www.thecodingguys.net/tutorials/visualbasic/vb-for-next
Every computer program has some kind of bug in it, and you need to
be able to catch any exceptions occur, to do this you use a try catch
block.
SYNTAX
Try
Catch ex As Exception
End Try
Example
Try
Console.WriteLine("Pick a number")
x = Console.ReadLine()
Console.WriteLine(Convert.ToInt32(x) + a)
Catch fEx As FormatException
Console.WriteLine(fEx.Message)
End Try
Console.ReadLine()
http://www.thecodingguys.net/tutorials/visualbasic/vb-managingerrors
SYNTAX
Public class <class name>
End Class
Example
Here is a class called car, with some methods:
Public Class Car
Public Function manufacturer(vmanufacturer As
String)
Return vmanufacturer
End Function
Public Function model(vModel As String)
Return vModel
End Function
Public Function colour(vcolour As String)
Return vcolour
End Function
End Class
You would then use this class and call it from another one like this;
Dim myCar As New Car()
Console.WriteLine("Please enter your car
manufacturer: ")
Dim m As String =
myCar.manufacturer(Console.ReadLine())
Console.WriteLine("Please enter car model
:")
Dim mo As String =
myCar.model(Console.ReadLine())
Console.WriteLine("Please enter car
colour")
Dim c As String =
myCar.colour(Console.ReadLine())
Follow me on: