![]() |
---|
![]() | |
---|---|
Michael Colombo - July 4, 2020 - Read time: 4 min
Updating Variables
There are times when a variable’s value must be updated when the program reaches a certain point. Following the example from last time, let’s say that we want to change Kul’s player class to something else. To update the content of a variable, we can use this syntax:
pClass = "Warlock";
After this instruction, whenever we’ll try reading the "pClass" variable again, its value will always be considered as "Warlock". In methods like Main(), variables and instructions are read in vertical order from top to bottom, the latest variable’s update is the one that counts. In C# we can refer to "reading" a variable with the term "calling" as well.
Console.WriteLine(quot;{pName} has become a {pClass}!");
Console.ReadLine();
Although it takes few seconds to update a variable’s content with manual editing like this, this is something that is rarely done in normal scenarios, because dynamic information like a player’s class, level and name should be managed through the use of databases, and we’ll see how that works in the future. However, it’s useful to know that a variable’s value can be changed with that simple syntax.
It’s possible to create an instruction that allows any user of the program to change a variable. For example, if the user of the player Kul wants to change the player name to something else, we can use ReadLine:
Console.Write(quot;Enter a new name: ");
pName = Console.ReadLine();
This allows the user of the program to tell the program a new name for the player. Any name typed in the console window will be read by the program and recorded inside the "pName" variable.
The same can be done for numerical information like levels or other player stats, but there are a few things that should be noted about numerical variables. While string type variables can contain both numbers and dots (besides normal text), numerical variables like int can only contain whole numbers, because it’s a variable type dedicated to integers.
Numbers as Input
Let’s make an example. If we want to set a player’s height using meters, we will need to use a number with a floating point, like "1.1" or "0.84". If we declare a "pHeight" variable using the int type, Visual Studio will warn us:
int pHeight = 1.1;
The variable type shouldn’t be an int but a double:
double pHeight = 1.1;
The double type allows us to make variables that can contain numbers with a floating point like the one above. The limitation of the double type variable is that it can only contain 15 digits in total. For example:
double pHeight = 1.1234567890123456789;
This variable’s value is too long, it will be trimmed and rounded as:
double pHeight = 1.12345678901235;
Since we’re just trying to set the player’s height, 15 numbers are definitely more than enough, but it’s still important to know the limitations of this variable type. There will be times when our program may need to perform detailed calculations where high precision is necessary. By the way, the integer type can contain 10 digits.
Now that we have set a default pHeight variable, we can add an instruction that allows the user of the program to change the player height parameter. We can try using ReadLine again:
pHeight = Console.ReadLine();
But Visual Studio will warn us again. ReadLine by default reads everything as a string type and we cannot convert text to numbers using ReadLine alone. To get around this issue, we can use this instruction:
pHeight = Convert.ToDouble(Console.ReadLine());
This allows the program to convert the string from ReadLine to a double type, which can be recorded inside the "pHeight" variable without problems. This is how we can use this instruction:
Console.WriteLine(quot;Default player height is {pHeight}.");
Console.Write(quot;Enter a new player height: ");
pHeight = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(quot;New player height is {pHeight}!");
Console.ReadLine();
At this point, we should reorganize our code and place the instructions we learned about in a more efficient order. Complete example usage of all instructions written above can be found here: