4. Store values in variables
Your spell may need to remember a message for the workshop or a number for later use. A variable gives that value a name. Start by storing a message to write in the log.
Name a string
public class Program : ArcaneProgram
{
public void OnStart()
{
string message = "Hello!";
Workshop.Log.Write(message);
}
}string message = "Hello!"; creates a variable named message and gives it the string Hello! Creating the variable is called declaring it.
| Part | Meaning |
|---|---|
| string | A variable for text |
| message | The name you choose |
| = "Hello!" | Its initial value |
| ; | End of the statement |
Write uses message without quotation marks. This means to use the variable's contents, Hello!, rather than display the word message.
Assign another value
After declaring the variable, message = "Ready"; changes its contents to Ready. Putting a value in a variable is assignment. Do not repeat string because this is not a new variable.
A variable has a type, which describes its kind of value. string holds text; int holds integers. For example, int count = 3; declares an integer containing three. The number 3 and the string "3" have different types.
Start your names with ordinary English letters. message and Message are different names. Use consistent spelling wherever you refer to the same variable.
Exercise
Insert message = "Ready"; immediately before Write in the example. What will it display?
Show example solutionHide example solution
It displays Ready. The autonomous spell stores Hello!, replaces it with Ready, then displays the current value.
public class Program : ArcaneProgram
{
public void OnStart()
{
string message = "Hello!";
message = "Ready";
Workshop.Log.Write(message);
}
}