At the end of this lesson, you will know…
- How to create/declare variables
- Where variables are visible
Before we start adding the game logic to the program, we should spend a little time going over some of the things you’ll see in the code.
The first thing we’ll talk about is variables.
We’ve already done a little bit with them, but there are some important things you need to be aware of, when creating and using variables.
Here’s the code for this lesson. This class doesn’t really do anything useful, but it shows how to use variables.
public class Variables
// Class-level variables
public int classVariable1 = 3; // Visible to other classes
private int classVariable2 = 7; // Only visible to this class
private int classVariable3; // Declaring the variable, without setting a value
public void Function1()
if(classVariable1 < 5)
functionVariable1 = 1;
if(classVariable1 >= 5)
functionVariable1 = 2;
public void Fuction2()
if(classVariable2 < 5)
functionVariable1 = 3;
if(classVariable2 >= 5)
functionVariable1 = 4;
One trick you can use to figure out where a variable is visible is to look at the curly braces it is between, when it’s “declared”. Generally, the variable is visible anywhere between those curly braces.
These are “class-level variables”. They can be used anywhere in this class.
The variables on line 6 through 8 are declared inside the curly braces for the class (lines 4 and 47). So, they’re visible everywhere between lines 4 and 47 – the whole class.
When we declare the variables on lines 6 and 7, we also set them to a value. We don’t do that on line 8. So, classVariable3 has the default value – which for “int”s is 0.
Different datatypes have different default values. And if you create a variable whose datatype is one of your classes, it has a value of “null” – nothing/empty.
classVariable1 (Line 6) can also be used outside of this class, since it is declared public. So, if another class makes an object of the Variables class, it will be able to change the value of classVariable1.
Since classVariable2 and classVariable3 are private, the other class wouldn’t be able to directly change the values of those variables. However, the other class could call one of the public functions (Function1 or Function2). Since those functions are inside the class, they can modify the private class-level variables (see lines 16, 23, 35, and 42).
“functionVariable1”, on line 12, is declared inside the curly braces for Function1 (lines 11 and 27). So, it’s visible everywhere inside that function.
“innerVariable1” (line 18) is declared between the curly braces on lines 15 and 19. So it’s only visible in between those lines. That’s why we can declare another variable with the same name on line 25, inside another completely separate set of curly braces.
We couldn’t declare another “functionVarable1” on line 18, since we have a functionVariable1 that is already visible at that level – the one declared on line 12.
Notice that we are able to declare a second “functionVariable1” variable on line 31, even though it has the same name as the variable declared on line 12.
We can do that because the “functionVariable1” on line 12 only exists between lines 11 and 27.
General variable declaration rules
It’s a good habit to declare your variables at the lowest level possible.
You could declare all your variables as class-level variables, and then change them in any function of your class. However, that can make it difficult to track down any problems you might have in the future. Since your variable can be changed in lots of places, you won’t necessarily know where it was changed.
Another extremely important rule with variables is to give them meaningful names.
Many programs have long lives. If you come back in six months, or six years, you want to be able to understand what your program is doing. When your class, functions, properties, and variables have clear names, it’s much easier to understand why they exist, and what they’re supposed to do.
Now you know where to declare your variables, and where your variables will be visible.
Source code for this lesson
There is no code for this lesson.
Next lesson: Lesson 14.2 – If statements
Previous lesson: Lesson 13.2 – Creating functions to handle user input
All lessons: Learn C# by Building a Simple RPG Index