Using the 3D Connexion SDK/Using Variables

Variables: your second programEdit

Most programs rely on a set of variables. These are ways to get the computer to store and recall bits of information. It is like a box to store information in. You give each variable a name when you declare it. Declaring a variable tells the computer 3 things:

1: The name

2: What sort of information it is supposed to store

3: The initial value

The name has to be a word which is not already used. Visual basic checks to make sure the name is alright and will tell you if there is a problem. Use something which is memorable (Time_taken rather than t), but quick to type (MyChNum rather than My_Chosen_Number).

The type of data is important. The most common types are Integer- a whole number such as 15, Single- a number with a decimal part such as 14.560385, Double- a number with a decimal part which needs to be very accurate such as 14.359649064923490 and String, a bit of text such as "A bit of text".

The way we declare a variable in Visual basic is by using the word 'Dim'. Here's how we declare a variable called MyVar as a String of text.

 Dim MyVar As String = "This is the initial value"

Let's go into our hello world application and add the variable. You can add a variable at any point in the program, but where it is determines what it can be used for. If we add it in the description of Form1 it can be used anywhere in the program:

 Public Class Form1
   Dim MyVar As String = "This is the initial value"
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Button1.Text = "Hello World" End Sub
End Class

You may have spotted by now that the properties of an object are all variables.

AssignationEdit

Assignation is simply setting the value of a variable. You set the value like so:

 A = 1

Sets the value of A as 1.

 A = B

Sets the value of A to the value of B.

 A = 23 * B

Sets the value of A to 23 times the value of B.

We can also use A in assignation:

 A = A + 1

This increases the value of A by 1.

 A = A * 2

This doubles A

 A = A / 2

This halves A

Let's make an application which counts the number of times a button is pressed.

Just like in the Hello World application, create a new program and add a button. Go to the Events list and add a Click event to the button. Your code should now look like this:

 Public Class Form1
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     End Sub
 End Class

Let's create a variable named 'counter'. We don't need it to have a decimal part, so we'll set the type to Integer. It should start at 0. The code for this is:

Dim counter As Integer = 0

Because this variable has to be stored even when button1 is not being clicked, it needs to go in the main part of the program:

 Public Class Form1
   Dim counter As Integer = 0
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     End Sub
 End Class

When Button1 is pressed, counter should increase by 1:

Counter = Counter + 1

 Public Class Form1
   Dim counter As Integer = 0
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Counter = Counter + 1
     End Sub
 End Class

Now, we need to add a line of code to update the text on the button:

 Public Class Form1
   Dim counter As Integer = 0
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Counter = Counter + 1
       button1.text = Counter
     End Sub
 End Class

Notice that Button1.Text is actually a string. If you use other languages, you will need to convert between variable types before you can assign to a different type.

Run your code to see the program.

Limits and RelativityEdit

Often, you will need to add limits to a variable. Let's add a Progress Bar to our form:

(Image)

Have a look at the properties. The Value Property sets how far the progress bar has gone- notice how it does a nice animation when changed.

This value has limits; it cannot be more than 100 or less than 0. Sort of makes sense; I've never copied 110% of a file. If you try setting Value to 200, your program will fail. To stop this, we need to compare the value we are about to use with the maximum. We need to use Relativity expressions:

RelativityEdit

Relativity essentially compares 2 values. A Relativity expression is phrased like this:

 A = 1

This expression is actually another form of Variable; a Boolean variable. This variable is either TRUE or FALSE. If A=1, (A = 1)=TRUE.

It's identical to assignment, but has to be used in a different place. I'll come on to that later. Here are a few more Relativity expressions:

 A > 1

A is greater than 1

 A < 1

A is less than 1

 A <> 1

A is not equal to 1

 A <= 1

A is less than 1, or A is equal to 1

 A >= 1

A is greater than 1, or A is equal to 1

We can combine expressions using AND, OR, NOT or XOR conditions. And means that both expressions must be TRUE, OR means neither of the expressions are FALSE, XOR means only one of them is TRUE. Not is used to change TRUE to FALSE and FALSE to TRUE.

      not(TRUE) = false
  TRUE or FALSE = true
 FALSE xor TRUE = true
  TRUE xor TRUE = false

We have 2 conditions for our progress bar:

 counter >= 0
 counter <= 100

Both of these have to be true for the program to work, so let's use and to combine them:

 (counter >= 0) and (counter <= 100)

We then use standard English to get the program to correct it. We type If, followed by our condition, followed by Then. Next comes the code to run, ended by an End If:

 If (counter >= 0) and (counter <= 100) then
       Counter = Counter + 1
       button1.text = Counter
       progressbar1.value = counter
 End If

There's a problem here. If we test the program and get Counter up to 100, we can then press the button again and try and get the progress bar up to 101. This crashes the program. The problem is with the order in which we give instructions:

1. Check if value of Counter is OK

2. Add 1 to Counter

3. Update form

Visual Basic works on a line-by-line basis; it does each line in order. We need to re arrange the program.

2. Add 1 to Counter

1. Check if value of Counter is OK

3. Update form

 Counter = Counter + 1
 If (counter >= 0) and (counter <= 100) then
       button1.text = Counter
       progressbar1.value = counter
 End If

We can add more things to this code; how about setting the value to 0 once it gets to the top? For this we need an Else part.

       counter = counter + 5
       If (counter >= 0) And (counter <= 100) Then
           Button1.Text = counter
           ProgressBar1.Value = counter
       Else
           counter = 0
       End If

What Just Happened (If...Then...Else Logic)Edit

The code used here was a decision making process:

 If Condition Then
   Things to do if Condition = TRUE
 Else
   Things to do if Condition = FALSE
 End If
Last modified on 28 July 2009, at 16:56