You are on page 1of 19

VB

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

CREATING CLASSES ........................................................................................................................................ 16


Example ................................................................................................................................................. 16

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

#region <code> end region

Single Line Comments


XML Comments
Specifies a region (groups of
code)

There are no block comments in Visual Basic.

Variables are a storage location, each variable you create uses some
memory in the computer (RAM).

Variable Naming

Variables start with uppoer or lower-case characters

Variable Syntax
Variables start with the Dim keyword.

Dim <variable Name> as <data type>

e.g.

Dim asim as string

Variable Data Types


Data Type
Int
Strings
Double
Boolean

Description
Whole Numbers (Integers)
Set of characters
Numbers with decimals
True or False

http://www.thecodingguys.net/tutorials/visualbasic/vb-variables

Strings are sequence of characters.


Strings are wrapped up in double quotes.
New Line starts with & vbCrLf &
Concatenation is the and sign & (or add +)
The escape character is 4 double quotes like:

Dim text As String


text = "Asim Said: ""Hello World"" "

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

An array can hold more than one value.

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.

Private Sub hello()


Console.WriteLine("Hello There!")
Console.ReadLine()
End Sub

To call this we just type hello() like this:


Sub Main()
hello()
End Sub

The if statement is a condition statement and is used to make a decision based


on some condition which is true.

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

The Visual Basic Select Case is similar to a switch 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.

Dim path = "U:\users\admin\desktop\note.txt"


Dim sr As New StreamReader(path)
Dim line = sr.ReadToEnd()
While line IsNot Nothing
Console.WriteLine(line)
Exit While
End While
http://www.thecodingguys.net/tutorials/visualbasic/vb-whilestatement

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

Classes contains methods and other events. You create a class by


using the public keyword (you can use private but the class is not
accessible from the outside.)

SYNTAX
Public class <class name>
End Class

You initialize classes like this;


Dim myCar As New Car()

Passing Arguments (Parameters)


With classes you pass arguments or parameters. You dont have to
but majority of classes will to get some input.

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())

Console.WriteLine("You car details: " &


vbLf & " " & m & " " & vbLf & " " & mo & vbLf & " "
& c)
Console.ReadLine()

To learn more about classes see:


http://www.thecodingguys.net/tutorials/visualbasic/vb-creatingand-managing-classes

Follow me on:

IF YOU FOUND THIS USEFUL, PLEASE SHARE IT!

You might also like