Back

Activity 1.5: Variables and Explicit Data Types

divider

Activity 1.5

Variables and Explicit Data Types

Key Concepts

Variables

Types

Initialization

You Typed It More Than Once

Console.WriteLine("Maya Okonkwo");
Console.Write("Maya Okonkwo");
Console.WriteLine(" is on the roster.");

The same name, typed twice, in two places.

Change it and you have to find every copy. The one you miss is a bug nobody notices.

Store It Once. Use the Name.

string playerName = "Maya Okonkwo";
Console.WriteLine(playerName);
Console.Write(playerName);
Console.WriteLine(" is on the roster.");

The quotes appear once. After that you write the name of the box, not the text.

That box is a variable.

This Is the Line From Day One

int screenWidth = 800;

Activity 1.1 told you to leave this alone and promised a session for it.

This is that session.

A Declaration Has Three Parts

int jerseyNumber = 24;

int is the type. jerseyNumber is the name. 24 is the value.

The type comes first, and it never changes afterward.

Four Types Cover Today

string
Text. "Maya Okonkwo"
int
A whole number. 24
double
A number with a decimal point. 18.4
bool
True or false. Nothing else.

The Type Is a Promise You Cannot Break

int jerseyNumber = "24";
Terminal window
1-5-card.cs(1,20): error CS0029: Cannot implicitly convert type 'string' to 'int'
The build failed. Fix the build errors and run again.

Quotes around 24 make it text. An int box will not take text.

Refused before the program ever ran.

A Decimal Is Not a Whole Number

int gamesPlayed = 18.4;
Terminal window
1-5-card.cs(1,19): error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
The build failed. Fix the build errors and run again.

It even offers a fix — “are you missing a cast?”

We cannot change a value's type yet. That is session 10, and you will remember this message.

Declaring Is Not Filling

int jerseyNumber;
Console.WriteLine(jerseyNumber);
Terminal window
1-5-card.cs(2,19): error CS0165: Use of unassigned local variable 'jerseyNumber'
The build failed. Fix the build errors and run again.

A labeled box with nothing in it. The compiler will not let you read it.

Putting the first value in is called initializing.

Two Things Worth Seeing Once

double pointsPerGame = 24;
bool isCaptain = true;
Console.WriteLine(pointsPerGame);
Console.WriteLine(isCaptain);
Terminal window
24
True

A double holding 24 prints 24, not 24.0.

A bool prints True, with a capital T. Neither is a mistake.

One Edit, Every Line

Change the value in the declaration and every line that used the name changes with it.

You never touch the output statements.

That is the whole reason variables exist.

Today's Objectives

  • Declaring, initializing, and reading a variable
  • Telling string, int, double, and bool apart
  • Choosing a type from the value a program has to store
  • Storing a value once and using it in several places

Key Terms

Variable
A named place that holds one value.
Type
What kind of value a variable is allowed to hold. Written first and fixed.
Declare
Create the variable by naming its type and its name.
Initialize
Put the first value into it. Reading one that was never initialized is an error.

'F' → Fullscreen

divider

Build

Create a new C# program named 1-5-card, starting empty.

You are building a profile card — an athlete, a game character, a club officer, a driver, anyone with facts attached to them. Pick your own, and use real facts or invented ones.


Task 1: Stored Once, Used Twice

  • Declare a string variable holding your subject's name.
  • Print the name on its own line.
  • Print a sentence that uses the name again, on one line of output.
  • Run it.

The quotes appear exactly once in your program. Everywhere else you write the variable's name.

Variables and Explicit Data Types
string playerName = "Maya Okonkwo";
Console.WriteLine(playerName);
Console.Write(playerName);
Console.WriteLine(" is on the roster.");
Output
Maya Okonkwo
Maya Okonkwo is on the roster.

Task 2: Four Types, One Card

  • Add an int, a double, and a bool, each holding a real fact about your subject.
  • Give every variable a name that says what it holds.
  • Build a card: a border, the name, a blank line, then one labeled line per fact.
  • Keep the labels aligned. Use spaces inside the label strings.
  • Finish with the sentence from Task 1.

Each label needs a Console.Write and each value a Console.WriteLine. That is the pattern from Activity 1.4, and it is how a label and a stored value share one line.

Variables and Explicit Data Types
// Who this card is for
string playerName = "Maya Okonkwo";
int jerseyNumber = 24;
double pointsPerGame = 18.4;
bool isCaptain = true;
// Header
Console.WriteLine("=============================");
Console.Write(" ");
Console.WriteLine(playerName);
Console.WriteLine("=============================");
Console.WriteLine();
// Stat lines
Console.Write("Number: ");
Console.WriteLine(jerseyNumber);
Console.Write("Points per game: ");
Console.WriteLine(pointsPerGame);
Console.Write("Team captain: ");
Console.WriteLine(isCaptain);
Console.WriteLine();
// Sign-off, reusing the name
Console.Write(playerName);
Console.WriteLine(" is on the roster.");
Output
=============================
Maya Okonkwo
=============================
Number: 24
Points per game: 18.4
Team captain: True
Maya Okonkwo is on the roster.

Task 3: One Edit, Every Line

  • Count how many lines of your output contain the name. Write the number down.
  • Change the name in the declaration — one line, one edit.
  • Run it. Check that every one of those output lines changed.
  • Do the same with the number: change the declaration only, and confirm the card follows.

You did not touch a single output statement. If you had typed the name into each line instead, you would have made that many edits and risked missing one.


Task 4: What the Types Refuse

  • Put quotation marks around your int's value. Run it. Copy the error exactly.
  • Fix it, then give your int a value with a decimal point. Run it. Copy that error exactly too.
  • Fix it. Now delete the = value from one declaration, leaving only the type and name. Run it. Copy the third error.
  • Fix it. Then set your double to a whole number and run it. Write down exactly what prints.
  • Print your bool and write down exactly what prints, capital letters included.

Three of these are refused and two are allowed. The two that are allowed print something slightly different from what you might expect, and neither is a mistake.

The second error offers you a cast. Ignore it. We do not have casts until session 10, and reaching for one now means guessing at something that has a session of its own.


Task 5: Choose the Type

  • For each fact below, decide which of the four types fits it. Write your answers down before you write any code.
  • Then add two of them to your card, with labeled output lines.
FactExample value
Height in meters1.83
Games played this season12
NicknameMo
Currently injuredno
Team or guild nameRidge Valley
Jersey number24

One of these is a trap. A jersey number is written with digits, but nothing about it is ever counted or measured. Decide what you would do, and be ready to defend it either way.

Variables and Explicit Data Types
// Who this card is for
string playerName = "Maya Okonkwo";
int jerseyNumber = 24;
double pointsPerGame = 18.4;
bool isCaptain = true;
// Two more facts, each stored as the type that fits it
string teamName = "Ridge Valley";
int gamesPlayed = 12;
// Header
Console.WriteLine("=============================");
Console.Write(" ");
Console.WriteLine(playerName);
Console.Write(" ");
Console.WriteLine(teamName);
Console.WriteLine("=============================");
Console.WriteLine();
// Stat lines
Console.Write("Number: ");
Console.WriteLine(jerseyNumber);
Console.Write("Games played: ");
Console.WriteLine(gamesPlayed);
Console.Write("Points per game: ");
Console.WriteLine(pointsPerGame);
Console.Write("Team captain: ");
Console.WriteLine(isCaptain);
Console.WriteLine();
// Sign-off, reusing the name
Console.Write(playerName);
Console.WriteLine(" is on the roster.");
Output
=============================
Maya Okonkwo
Ridge Valley
=============================
Number: 24
Games played: 12
Points per game: 18.4
Team captain: True
Maya Okonkwo is on the roster.

Challenge (Optional)

  • Add a second card for a different subject, reusing none of the first card's variables.
  • Find out what happens if two variables are given the same name. Copy the error.
  • Find out whether a variable name can start with a digit, or contain a space. Copy what you get.
  • Store a value in a double with more decimal places than you expect it to keep, and see what prints.
divider

Checkpoint

Before you submit, check all six:

  • Your card declares at least one string, one int, one double, and one bool.
  • Every variable name says what it holds.
  • One value is used in at least two places, and its quotes or digits appear only once.
  • Changing one declaration changed every output line that used it.
  • Two facts from Task 5 are on the card, stored as the types you chose.
  • All five Task 4 results are written down — three errors and two printed values.

The last one cannot be reconstructed by reading your program. Three of those results are compiler messages and two are output that differs from what the code looks like it should produce.

divider

Reflection

Answer the following questions before submitting your work.

  1. Putting quotation marks around a number made your program refuse to run. What does the type tell C# that lets it refuse before running anything at all?
  2. Deleting = value from a declaration is an error, but the declaration still named a type and a name. In your own words, what is the difference between declaring a variable and initializing it?
  3. How many output lines changed when you edited one declaration in Task 3? Why is that better than typing the value into each line?
divider

Submit

Submit four things to the dropbox:

  1. Your 1-5-card program file.
  2. A copy of your card's output.
  3. Your five Task 4 results, and your type choices from Task 5.
  4. Your three reflection answers.

Activity Complete