Assigning default values to variables

When declaring a variable in most cases you will provide it with an initial value. This is known as assigning the variable a value, as shown in the following lines.

playerLives as integer = 10
playerScore as integer = 0

playerLives is assigned a value of 10, whereas playerScore is assigned a value of 0.

Later on in your program you can assign these variables other values. These could be literal values or the values contained in other variables. However, at the point of declaration if you choose to assign a default value to a variable it must be a literal value (or a constant which we'll examine another time). You cannot, for example, attempt to set the default value of a variable to that of another variable. If the previous code listing was changed so that playerScores was assigned the value of playerLives then the program will fail to compile.

playerLives as integer = 10
playerScore as integer = playerLives

When an error is detected in a program a message will appear in the Compiler tab of the Message Window explaining what the problem is, which in this case is that the variable default value for playerScore must be a literal or constant. Therefore in this instance to correct the error simply change the code so that playerScore is assigned a literal value such as 0.

As both variables have been declared as integers then an error would also be detected upon compilation if you attempted to assign these variables non integer values. For example this code listing will fail.

playerLives as integer = 10.12
playerScore as integer = "hello"