Introduction:
What are you going to learn from this guide?
You are about to learn how to properly code in Visual Basic.
And maybe pick one or two things that you did not know before.
I am assuming that you are able to choose a control from the toolbox
and placed on the form and setting properties.
I've broken the guide into four parts.
Part1 - The basics involving writing good code and formats.
Part 2 - The code base> The visual basic commands.
Part3 - Advanced Visual Basic - Dealing with API and a lot of interesting things.
Part4 - Extra Goodies
Part 1: Basics
An important part of being a good programmer, Visual Basic is good for the use of file names and controls. It may seem like extra work, but in the long run, it will allow you to code faster and allow yourself and others better understand your own code.
A rule that I use for prefixesis to keep the code in lowercase and capitalize the first letter of the name of the control or file.
Prefixes for files:
Project prj =
frm = Form
mod = module
CLS = Class
User usr = control
Prefixes Common Controls
LBL = Control Label
Command Button cmd =
Img = Image control
pic = Image box
TMR = Timer
Shape shp =
chk = Checkbox
LST = Listbox
Textbox txt =
opt = Option (Radiobutton)
hs = horizontal scroll bar
vs = the vertical scroll bar
CBO = combobox
MNU = Menu
SCK = Winsock
Bad names do not use!
Form1
Command1
Timer1
Picture1
Good Names
frmMain
cmdClose
TmrLoop
picView
Prefixes can also be very useful when writing code.
String str =
lng = Long
int = Integer
SNG = Single
bln = Boolean
var = Variant
BadExample:
dim stuff as a string
stuff = InputBox ("Enter your name")
good example:
dim strName as String
strName = InputBox ("Enter your name")
Part 1B. Make the legible part of code 2.
You've already learned how prefixes can help make the code easier and faster.
Now we move on and return comments!
One of the worst things is about finding the code that is not returned and / or any space blank.
Bad example
subProcessNumber (ByRef intNumber as a whole)
= 3 then if intNumber
more
intNumber if = 4 then
End If
End If
End Sub
Good example:
sub ProcessNumber (ByRef intNumber as integer)
If intNumber = 3, then
Other
If intNumber = 4 then
End If
End If
End Sub
When you indent?
Tab is also a time when you start to code in an event or a subroutine.
Then for each if statement, loop, you alsohyphen.
Comments:
Visual Basic Comments begin with 'and can go anywhere you want
They are useful to explain the code for yourself and others.
I really wish VB had comments on multiple lines, as in C + + but it is not so bad.
White Space:
Leaving white space makes the code easier to read and makes it look cleaner
Part 2: The Code
The fun stuff starts now.
The first line in all shapes and forms must be
OptionExplicit
What does Option Explicit do?
Well, it forces you to declare all variables with them otherwise they do all the type of variant.
Sample so you can not do:
for i = 0-100
the next
You have to have before I said
Dim i as byte
for i = 0-100
the next
Variables
What are the variables? They contain data that can change when you run the program
Bytes = 0-255 numbers holds
String = holds the characters or letters as"Hello World! E numbers too 123456789"
Integer = numbers without decimals from -32,768 to 32,767
Long = numbers with no decimal -2147483647 to 2147483647
Single = May contain decimal numbers 32bit
Holds Boolean = True or False
Constants
What are the constants? They argue that the data does not change.
An example would be a constant
Private Const Pi = 3.14
Private / Public / Global
What do you mean private?
Private means that you can only accessin its present form, a module or class.
What public and the global average?
Public means that it can be accessed from any shape, form or class
Static
If you make a static variable that will save its value the next time the sub is running.
If then statements
A line, if then statement
If condition = True then blnSomething = true
Multi-line if then statement
If condition = true then
'Your code goes here
End If
Complex if then elsestatements
If condition = True Then
Other
'Condition = false
End If
If condition = true then
ElseIf contition2 = True Then
End If
Select Case statement:
As the switch statement in C + +
What is it good for?
And 'good for instances when you have a variable and
do not want to have a million, if then statements.
Example
intNumber Dim As Integer
select case intNumber
Case 1:
case 2:
Another case:
endselect
Loops:
For Next loop
Syntax
For step 100 = 0-100
next
Do While
Do Until
Overview Subs and Functions of ByVal and ByRef
By default all parameters are passed by reference in Visual Basic which means they are passing
the memory address and not the value.
Sub Test ()
strReturnValue dim as string
Test2 call (strReturnValue)
msgbox strReturnValue
End Sub
Sub Test2 (as StrRefString)
StrRef = "Hello World"
End Sub
What is a function? A function is a special type of procedure that returns a value, for example,
Sub Main
AddNumbers msgbox (3.4)
End Sub
private function AddNumbers (ByVal intNumber1 as Integer, ByVal intNumber2 As Integer) As Integer
AddNumbers = (+ intNumber1 intNumber2)
End Function
In the above example shows a message box with the result of 7
You can specify how you want to pass a parameterby reference (ByRef)
or by value (ByVal) You want to use pass by value when you are not going to change the parameter.
Sub Example (StrRef ByRef As String, ByVal intNumber As Integer)
End Sub
Some quick reference:
All Visual Basic functions and commands are listed in the object browser.
It looks like a box with things coming out in the toolbar.
Use App.Path instead of hard-coded paths.
App.Path returns the path to yourapplication
You can use app.previstance to detect whether the application is already running.
Part 3: Advanced Visual Basic Information
Section A: Understanding API
Your guide to Visual Basic Api is [http://www.allapi.net]
Once seen allapi.net
Get API Viewer 2004 tons lists VB API
Get Help API API and has many examples of how to use them.
Conversion of API C + + Visual Basic Api
VB= Integers C + + Short
Longs C + + VB = Whole
Section B: Pointers
There is a common myth going around that Visual Basic does not support pointers.
This is not true that they are just hidden.
VarPtr
ObjPtr
StrPtr
AddressOf - Used for finding addresses and procedures
Section C: Understanding VB binary data.
Visual Basic stores a string in the following format
Length As Integer
Text as String
Bytes occupy abytes of data.
Boolean occupies two bytes or 00 00 or FF FF
Integer to take up two bytes.
Long takes four bytes.
Single occupies four bytes.
Double occupies eight bytes.
Section D: Reverse Engineering VB Visual Basic (5 / 6)
For a couple of months this was the area that I worked on.
For Visual Basic 6 exe there are two methods for native compilation or P-code.
P-Code is pretty easy to understand what all the opcodes are known and it is just
question to understand what opcode does what and which connects the imports of the exe.
Natives on the other hand it is difficult is to convert assembly code Visual Basic.
Check out [http://decompiler.theautomaters.com] - Visual Basic Decompiler forum
for more information.
Section E: Build DLL in Visual Basic as a real C + +
By default Visual> Basic DLL with no exports, is only active x dll.
But you can do in Visual Basic if you can intercept the compilation process.
You can make your own c2.exe to intercept the parameters passed to the real c2.exe then pass that
the final information to create a DLL file with the normal real exports.
Section F: Debugging / Decompiling
I like to use Debug.Print advice or use a MsgBox statement to see what's going on in mycode.
excellent tool for debugging a compiled exe - SmartCheck.
Visual Basic Decompiler list:
Semi VB Decompiler - http://www.visualbasiczone.com
VB Parser 1.2
Race
VB Reformer
VBDE
VB Editor
VBREZQ
EXDEC
Section G: Choose the type of compilation.
Visual Basic offers two ways to compile the native application or P-Code
Native-
It's faster
More difficult to Decompile
Yoularger
P-Code
Slower
Easier to Decompiler
Smaller EXE
Part 4: extra extra
Section A: Links!
http://www.pscode.com - best place to search for the Visual Basic.
http://www.vbgamer.com - dealing with Visual Basic Games
[Http: / / decompiler.theautomaters.com] - Visual Basic Decompiler forum
http://www.visualbasiczone.com - my site for VB
http://www.vbcity.com - Excellent communitypeople aware
conference calling best buy notebook computers uk (amazon.co.uk)