Skip to content

Instantly share code, notes, and snippets.

@Wilfred
Created September 18, 2009 22:03
Show Gist options
  • Save Wilfred/189321 to your computer and use it in GitHub Desktop.
Save Wilfred/189321 to your computer and use it in GitHub Desktop.
' apostrophes denote comments
REM keyword also does this
' VB6 is case insensitive
Option Explicit
' this ensures that you only use variables you have declared
' (avoiding typos creating new variables)
Dim X, Y, Z As Integer
' Integers are only 16-bit. Use Long instead
' X and Y are Variants, only Z is an integer
If X = 1 Then
X = 2 ' = is overloaded for comparison and assignment
Dim S As Long
End If
' variables have only function/sub level scope. S is still
' accessible here
S = 0
' null keyword is Nothing
Dim AnArray(3) As Boolean
' this creates a FOUR element array numbered 0-3
Dim AnotherArray(-5 To 10)
' we can also define arbitrary array bases
Call DoStuff(X, Y, Z) ' call keyword requires brackets
DoStuff X, Y, Z ' without call we may not use brackets
S = DoStuff(X, Y, Z) ' unless we are returning a value
Dim Obj As Object
Set Object = GetSomeObject("foo") ' set is compulsory for reference assignment
Let X = 2 ' let is completely optional for primitives
Dim foo As String
foo = "escape strings with "" (double quotation marks)"
foo = "append with ampersand " & foo
X = 1 / 2 ' floating point division
X = 3 \ 2 ' integer division
Private Sub Foo(ByVal A As Integer, ByRef B As String)
' arguments are passed by reference as default!
On Error GoTo Panic
' yes, goto is available
If A <> 0 And Test(A) Then ' not equal to zero
' Boolean logic does not use short circuiting
' so Test(A) is always evaluated
Exit Sub
End If
Panic:
Debug.Print "Do you know where your towel is?"
End Sub
Function Bar() As Integer
' subs may not return values
' they are optional for functions
End Function
Public Sub Thing_CrazyEvent() As Integer
' by convention this code is called when the CrazyEvent event
' occurs to Thing
Dim i As Long
' vb6 ide has a habit of changing the capitalisation based on
' variable names elsewhere
For I = 2 To 20 Step 2
MsgBox I
Next I ' stating variable here is optional but
' if you do the compiler will check it
Dim N As ListItem
For Each N In List
' for each loops are also available
DoSomething N
Next
End Function
Private Sub Something(A As String, B As String, C As String, _
D As String)
' line continuations are available using _ but they are capped at 25 lines
' this is a pain for sql, see http://www.dailydoseofexcel.com/archives/2005/04/18/line-continuation-limit/
End Sub
Implements AnInterfaceName
' vb objects can only use interface inheritance
' not implementation inheritance
' objects also do not support constructor methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment