# Using the 3D Connexion SDK/Using Variables
### Variables: your second program

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.
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:
You may have spotted by now that the properties of an object are all variables.

#### Assignation

Assignation is simply setting the value of a variable. You set the value like so:
Sets the value of A as 1.
Sets the value of A to the value of B.
Sets the value of A to 23 times the value of B.
We can also use A in assignation:
This increases the value of A by 1.
This doubles A
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:
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:
When Button1 is pressed, counter should increase by 1:
Counter = Counter + 1
Now, we need to add a line of code to update the text on the button:
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 Relativity

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:

##### Relativity

Relativity essentially compares 2 values. A Relativity expression is phrased like this:
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 is greater than 1
A is less than 1
A is not equal to 1
A is less than 1, or A is equal to 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.
We have 2 conditions for our progress bar:
Both of these have to be true for the program to work, so let's use and to combine them:
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:
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
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.

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

The code used here was a decision making process:
* Next: Using Procedures
* Home