14. Variables

In the previous lesson you may have noticed references to something called variables. A variable is simply some memory set aside by the computer for you to use during the running of your program. This allows us to remember pieces of information and update them as necessary.

In our case we want to remember how many wins the person playing “X” has and how many wins the person playing “O” has, and so we require two variables. Making a variable in Visual Basic requires adhering to particular syntactical requirements.

Syntax
When you write code you need to write it in a manner that the language expects, those expectations are known as “Syntax“. If you type your code incorrectly then almost all programming languages will report what’s called a “Syntax Error“. The syntax of a variable declaration in VB is as such

Dim <<variablename>> As <<datatype>>

Each component of that line is important to understand:

  • Dim: This informs Visual Basic that you are about to create a variable
  • <<variablename>>: This is where you give your variable a name
  • As: This informs Visual Basic we’re about to tell it what type of variable this is
  • <<datatype>>: What sort of variable is this one going to be.

Naming your variable
Just like when we named our objects, you need to try and stick to a consistent naming scheme for your variables and the beauty of variables is the same basic principles apply that we used in naming our objects, namely:

  • Don’t use spaces
  • Start with a lower case letter
  • Use camelCase or underscores when multiple words are used
  • Try and keep the names succinct but descriptive

The only real difference is you don’t have to worry about a prefix.

Data Types
A data type simply refers to what kind of information are you going to hold in your variable. They are reserved words that you put at the end of your variable declaration telling the computer what is allowed to be put inside your variable. We will be encountering five different data types at this early stage of the programming journey:

  1. Integer: This is a whole number between approximately -32000 and +32000
  2. Long: This is a whole number outside the range of “Integer
  3. Double: This is for numbers requiring a decimal or fractional component
  4. String: This is for when you want to remember strings of letters and/or words
  5. Boolean: This is for when something is either “True” or “False

Rather than explain them all in detail now we’ll use them as required and explain them as they’re encountered.

Integer
The first two variables we require are going to hold the number of wins for both the “Nought” player and the “Cross” player. That number is highly unlikely to be greater than 32000 and so Integer is probably the best data type to use and so our variable declarations are going to look something like this:

Dim oWins As Integer
Dim xWins As Integer

So that’s all well and good and our declarations are correct, but where do we put them???

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>