Print this page

Visual Basic


VISUAL BASIC Quick Reference
 
Example
 
Function Main() AsInteger
   MsgBox("Hello World!")
   Return 0
End Function
 
For Loop
 
Dim counter asInteger

For counter = 0to myWidgetCollection.Count - 1 MessageBox.Show(myWidgetCollection.Item(counter).Name)

Next
 
While Loop
While Number > 6

      Number = Number - 1     

EndWhile
 
If…Then

If finalVelocity > cnstSpeedOfLight then

   MessageBox.Show("You cannot exceed lightspeed!")

EndIf
 
Array
Dim a(2) AsString
a(1) = "good"
a(2) = "bye"
formula = a(1) & a(2)
Dim S2X2(1, 1) As Short
 
Struct
 
Structure Employee
   Public GivenName AsString
   Public FamilyName AsString
   Public Extension AsLong 
   Private Salary As Decimal
End Structure
 
 

Function Method

PublicSub Remove(ByVal index asInteger)

   If index > Count - 1Or index < 0Then

      System.Windows.Forms.MessageBox.Show("Index not valid!")

   Else

      List.RemoveAt(index)

   EndIf
End Sub
 
Class
Public Class Widget
   Public Name asString
End Class

Throw Exceptions

PublicSub Accelerate(ByVal finalVelocity asInteger)

   If finalVelocity > cnstSpeedOfLight then

      Throw New SpeedOfLightException("You cannot exceed light speed!")

   EndIf
   ' (Additional code omitted)
End Sub
 

Catch Exceptions

Dim cmdresults AsInteger

' The following two property settings can also me done

' in the Properties window, but are shown here for completeness.

OleDbcommand2.CommandText = "NewAuthor"

OleDbCommand2.CommandType = CommandType.StoredProcedure

 

' Set parameter values. In this case, all parameter values

' are strings.

OleDbCommand2.Parameters("au_id").Value = TextBox1.Text

OleDbCommand2.Parameters("au_lname").Value = TextBox2.Text

OleDbCommand2.Parameters("au_fname").Value = TextBox3.Text

OleDbCommand2.Parameters("phone").Value = TextBox4.Text

OleDbCommand2.Parameters("address").Value = TextBox5.Text

OleDbCommand2.Parameters("city").Value = TextBox6.Text

OleDbCommand2.Parameters("st").Value = TextBox7.Text

OleDbCommand2.Parameters("zip").Value = TextBox8.Text

OleDbCommand2.Parameters("contract").Value = CheckBox1.Checked

 
OleDbConnection2.Open()
Try

   cmdresults = OleDbcommand2.ExecuteNonQuery()

Catch ex as Exception

   MessageBox.Show("Failed to execute command, err = " & ex.Message)

End Try
OleDbConnection2.Close()
MessageBox.Show("Number of records inserted = " & cmdresults.ToString)
 
 
Database
Using ADO & ADO.NET