visual basic cheat sheet
TRANSCRIPT
-
7/27/2019 visual basic cheat sheet
1/19
VBCHEAT
SHEET
&NOTES
March 20
2013 2013
thecodingguys.nethttp://www.thecodingguys.net
http://blog.thecodingguys.net
http://www.thecodingguys.net/http://www.thecodingguys.net/http://blog.thecodingguys.net/http://blog.thecodingguys.net/http://blog.thecodingguys.net/http://www.thecodingguys.net/ -
7/27/2019 visual basic cheat sheet
2/19
CONTENTS ....................................................................................................................................................... 1
ABOUT ............................................................................................................................................................. 3
LICENSE ............................................................................................................................................................ 3
VB SYNTAX....................................................................................................................................................... 4
COMMENTS..................................................................................................................................................... 4
VARIABLES ....................................................................................................................................................... 4
VARIABLE NAMING............................................................................................................................................ 5VARIABLE 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
ARRAYSYNTAX .............................................................................................................................................. 9
Example ................................................................................................................................................... 9
METHODS ...................................................................................................................................................... 10
IF STATEMENTS .............................................................................................................................................. 10
SYNTAX ........................................................................................................................................................ 11
SYNTAXIF ELSE ............................................................................................................................................ 11
SELECT CASE................................................................................................................................................... 11
Example ................................................................................................................................................. 12
WHILE STATEMENT ........................................................................................................................................ 13
Example ................................................................................................................................................. 13
FOR ITERATIONS ............................................................................................................................................ 14
Example ................................................................................................................................................. 14
MANAGING ERRORS ...................................................................................................................................... 15
Example ................................................................................................................................................. 15
-
7/27/2019 visual basic cheat sheet
3/19
CREATING CLASSES ........................................................................................................................................ 16
Example ................................................................................................................................................. 16
-
7/27/2019 visual basic cheat sheet
4/19
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 Attribution-
NonCommercial-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
http://www.thecodingguys.net/tutorials/visualbasic/vb-tutorialhttp://www.thecodingguys.net/tutorials/visualbasic/vb-tutorialhttp://www.thecodingguys.net/downloadshttp://www.thecodingguys.net/downloadshttp://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/http://www.thecodingguys.net/downloadshttp://www.thecodingguys.net/tutorials/visualbasic/vb-tutorial -
7/27/2019 visual basic cheat sheet
5/19
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 Single Line Comments XML Comments
#region end region 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).
-
7/27/2019 visual basic cheat sheet
6/19
Variable Naming Variables start with uppoer or lower-case characters
Variable Syntax
Variables start with the Dim keyword.
Dim as
e.g.
Dim asim as string
Variable Data TypesData Type Description
Int Whole Numbers (Integers)Strings Set of characters
Double Numbers with decimals
Boolean True or False
http://www.thecodingguys.net/tutorials/visualbasic/vb-variables
http://www.thecodingguys.net/tutorials/visualbasic/vb-variableshttp://www.thecodingguys.net/tutorials/visualbasic/vb-variableshttp://www.thecodingguys.net/tutorials/visualbasic/vb-variables -
7/27/2019 visual basic cheat sheet
7/19
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 AsStringtext = "Asim Said: ""Hello World"" "
String.Format Options
Format Description
{0:C} Currency Symbol{0:N} Dot or Comma (for separating large
numbers}
{0:P} Percentage Symbol
Example
Dim strFormat AsString
strFormat = String.Format("{0:C}", 5)
-
7/27/2019 visual basic cheat sheet
8/19
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.
ExampleDim subStr AsString
subStr = "Welcome to thecodingguys!"
Console.WriteLine(subStr.Substring(0, 5))Console.ReadLine()
This uses the substring function which simply gets the first 5characters from our string subStr, it starts from 0 and stops at the 5
th
character, so the output is Welco.
Exceptions
ArgumentOutOfRangeException
Functions DescriptionToString Converts an integer (or double)
-
7/27/2019 visual basic cheat sheet
9/19
to a string
Toupper Formats a string so it is upper-
case
ToLower Formats a string so it is lower-case
Equality / Comparison
Logical
Operator Description
AND AND
Or Or
Operator Description
= Is equal to
Not equal to
< Less than
> Greater than
= Greater than or equal to
-
7/27/2019 visual basic cheat sheet
10/19
An array can hold more than one value.
Array
Syntax
Dim array-name (size) As
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
http://www.thecodingguys.net/tutorials/visualbasic/vb-arrayshttp://www.thecodingguys.net/tutorials/visualbasic/vb-arrayshttp://www.thecodingguys.net/tutorials/visualbasic/vb-arrays -
7/27/2019 visual basic cheat sheet
11/19
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.
PrivateSub hello()Console.WriteLine("Hello There!")Console.ReadLine()
EndSub
To call this we just type hello() like this:
Sub Main()
hello()
EndSub
The if statement is a condition statement and is used to make a decision based
on some condition which is true.
-
7/27/2019 visual basic cheat sheet
12/19
Syntax
If condition Then
EndIf
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.
SyntaxSelectCase VariableName
Case1code
Case2
http://www.thecodingguys.net/tutorials/visualbasic/vb-if-statementhttp://www.thecodingguys.net/tutorials/visualbasic/vb-if-statementhttp://www.thecodingguys.net/tutorials/visualbasic/vb-if-statement -
7/27/2019 visual basic cheat sheet
13/19
Case Elsecode
EndSelect
Example
Dim game AsStringConsole.WriteLine("Your favourite game?")game = Console.ReadLine()Dim message AsStringmessage = game & " is your favourite game!"
SelectCase gameCase"GTA 4"
Console.WriteLine(message)Case"Battlefield 3"
Console.WriteLine(message)Case"GRID"
Console.WriteLine(message)Case"Infamous"
Case ElseConsole.WriteLine("Looks like your
game is not on my list")EndSelect
Console.ReadLine()
http://www.thecodingguys.net/tutorials/visualbasic/vb-select-case
http://www.thecodingguys.net/tutorials/visualbasic/vb-select-casehttp://www.thecodingguys.net/tutorials/visualbasic/vb-select-casehttp://www.thecodingguys.net/tutorials/visualbasic/vb-select-case -
7/27/2019 visual basic cheat sheet
14/19
The while statement runs a piece of code, while its condition is true.
SYNTAXWhile variable True
Code to executeExit While
EndWhile
ExampleThis example reads a text file.
Dim path = "U:\users\admin\desktop\note.txt"Dim sr AsNewStreamReader(path)
Dim line = sr.ReadToEnd()
While line IsNotNothingConsole.WriteLine(line)Exit While
EndWhile
http://www.thecodingguys.net/tutorials/visualbasic/vb-while-
statement
http://www.thecodingguys.net/tutorials/visualbasic/vb-while-statementhttp://www.thecodingguys.net/tutorials/visualbasic/vb-while-statementhttp://www.thecodingguys.net/tutorials/visualbasic/vb-while-statementhttp://www.thecodingguys.net/tutorials/visualbasic/vb-while-statementhttp://www.thecodingguys.net/tutorials/visualbasic/vb-while-statement -
7/27/2019 visual basic cheat sheet
15/19
The For Next Loop will iterate through data, commonly used to print out a list
of numbers or anything similar!
SYNTAXFor index = 1 To 50
codeNext
Console.ReadLine()
Example
For i = 1 To 50
Console.WriteLine(i)Next
Console.ReadLine()
http://www.thecodingguys.net/tutorials/visualbasic/vb-for-next
http://www.thecodingguys.net/tutorials/visualbasic/vb-for-nexthttp://www.thecodingguys.net/tutorials/visualbasic/vb-for-nexthttp://www.thecodingguys.net/tutorials/visualbasic/vb-for-next -
7/27/2019 visual basic cheat sheet
16/19
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.
SYNTAXTry
Catch ex AsException
EndTry
Example
Try
Console.WriteLine("Pick a number")x = Console.ReadLine()Console.WriteLine(Convert.ToInt32(x) + a)
Catch fEx AsFormatExceptionConsole.WriteLine(fEx.Message)
EndTry
Console.ReadLine()
http://www.thecodingguys.net/tutorials/visualbasic/vb-managing-
errors
http://www.thecodingguys.net/tutorials/visualbasic/vb-managing-errorshttp://www.thecodingguys.net/tutorials/visualbasic/vb-managing-errorshttp://www.thecodingguys.net/tutorials/visualbasic/vb-managing-errorshttp://www.thecodingguys.net/tutorials/visualbasic/vb-managing-errorshttp://www.thecodingguys.net/tutorials/visualbasic/vb-managing-errors -
7/27/2019 visual basic cheat sheet
17/19
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.)
SYNTAXPublicclass
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:
PublicClassCar
PublicFunction manufacturer(vmanufacturer AsString)
-
7/27/2019 visual basic cheat sheet
18/19
Return vmanufacturerEndFunction
PublicFunction model(vModel AsString)Return vModel
EndFunction
PublicFunction colour(vcolour AsString)
Return vcolourEndFunction
EndClass
You would then use this class and call it from another one like this;
Dim myCar AsNewCar()
Console.WriteLine("Please enter your carmanufacturer: ")Dim m AsString =
myCar.manufacturer(Console.ReadLine())
Console.WriteLine("Please enter car model:")
Dim mo AsString =
myCar.model(Console.ReadLine())
Console.WriteLine("Please enter carcolour")
Dim c AsString =myCar.colour(Console.ReadLine())
-
7/27/2019 visual basic cheat sheet
19/19
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-creating-
and-managing-classes
Follow me on:
IF YOU FOUND THIS USEFUL, PLEASE SHARE IT!
http://www.thecodingguys.net/tutorials/visualbasic/vb-creating-and-managing-classeshttp://www.thecodingguys.net/tutorials/visualbasic/vb-creating-and-managing-classeshttp://www.thecodingguys.net/tutorials/visualbasic/vb-creating-and-managing-classeshttp://blog.thecodingguys.net/rss.xmlhttps://twitter.com/thecodingguyshttps://www.facebook.com/Thecodingguyshttps://plus.google.com/115153322927947938248/postshttp://www.thecodingguys.net/tutorials/visualbasic/vb-creating-and-managing-classeshttp://www.thecodingguys.net/tutorials/visualbasic/vb-creating-and-managing-classes