Using the 3D Connexion SDK/Procedures
So, What are Procedures?
editA procedure is a bit of code which you can move out of your main program and use again and again for different purposes. Let's say you had a program which tool in variable A, and divided it by 2, squared the result and added 4, and did this 10 times. In standard Visual Basic, it would look like this:
A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4 A = ((A / 2) * (A / 2)) + 4
If you do this a lot, it makes sense to get Visual Basic to do all the tedious coding. Once you've read this chapter, you'll be able to make a procedure which does it for you. If you called it 'Calculate', your code could look like this:
Calculate Calculate Calculate Calculate Calculate Calculate Calculate Calculate Calculate Calculate
This causes the program to jump to 'Calculate' and then jump back when Calculate is finished.
Making a simple procedure
editTo make a procedure, let's make a new Visual Basic program and add a button. Add a Click event to it (Look at the Hello World Tutorial for more). Declare an Integer variable called MyNumber:
Public Class Form1 Dim MyNumber As Integer = 1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub End Class
In visual basic, our entire program is just one massive procedure for Windows (or other OS) to run, so procedures inside the program are called Sub-Procedures, or Subs for short. Visual basic lets us say whether it is Public (other programs can start it running) or Private (it is invisible to other processes). We'll make ours public. You can see in the code that Visual Basic has already made a Private Sub for our button click event. Let's add another sub, this time Public, and call it 'DoubleMyNumber':
Public Class Form1 Dim MyNumber As Integer = 1
Public Sub DoubleMyNumber() This is where the procedure code goes End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub End Class
The reason for putting the '()' after the name of the procedure will become clear in a moment. (Visual Basic actually adds it in automatically)
So, let's add the code to DoubleMyNumber:
Public Class Form1 Dim MyNumber As Integer = 1
Public Sub DoubleMyNumber() MyNumber = MyNumber * 2 End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub End Class
We need to call DoubleMyNumber when the button is pressed. This is nice and simple; we just type its name:
Public Class Form1 Dim MyNumber As Integer = 1
Public Sub DoubleMyNumber() MyNumber = MyNumber * 2 End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DoubleMyNumber Button1.Text = MyNumber End Sub End Class
Congratulations; you've just made a new procedure.
Adding Parameters
editTo make the procedure more useful, we can add parameters. A parameter is a variable which you can send to a procedure. Let's say we need to multiply the number by something other than 2. How about being able to do:
Multiply_My_Number_By(312)
We can tell the procedure to expect a variable. In fact, we can tell it to expect as many variables as we like, and separate them with commas. We could have a procedure to add 5 numbers:
AddUp(2,54,23,12,9)
All we need to do is declare it when we write the procedure description. Let's edit the program we just made. The brackets after the name are actually the place where a parameter can be declared. First we have to tell the procedure what to do with the variable after running:
ByRef: Call by Reference. This means you can change the variable
ByVal: Call by Value. The procedure cannot change the variable
Let's use ByRef. At this level, the choice of ByRef and ByVal is rarely important.
The code to get the parameter ByHowMuch is like so:
ByRef ByHowMuch As Integer
Here's the complete code:
Public Class Form1 Dim MyNumber As Integer = 1
Public Sub DoubleMyNumber(ByRef ByHowMuch As Integer) MyNumber = MyNumber * ByHowMuch End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DoubleMyNumber(312) Button1.Text = MyNumber End Sub End Class
Functions
editA function is like a procedure, but it is actually a variable. Let me explain:
A = Half(14)
Half(14) can be used just like a number. In fact, it is a number. Functions are much more flexible than procedures because they can be used to assign to different variables:
A = Half(A) B = Half(B) C = Half(C)
Like procedures, functions can have more than one parameter:
A = Multiply(A, 0.5)
We have to declare functions in a different way:
Public Function DoubleMyNumber(ByRef MyNumber As Integer) As Integer DoubleMyNumber = MyNumber * 2 End Function
Look at the last 2 words of line 1; As Integer. We have actually declared DoubleMyNumber as a new Integer, which we can assign to. We say DoubleMyNumber(Mynumber) = MyNumber * 2, and visual basic can work out that DoubleMyNumber(7) = 7 * 2
Visual Basic has a huge number of built in libraries of functions, try typing 'Math.' to open the Mathematical Operators library.
Welcome to the VB World!
editIf you read and understood all that, you're now ready for the big bad world of Visual Basic. You're also ready to get going on the 3D connexion SDK. If you didn't understand it, you need to hunt around for a Visual Basic course which suits you. There are loads out there, mainly much better than this, so happy hunting!
Go to the contents and start on the 3D Connexion tutorials.