So we’ve now got code that will cause the text of our app to change when we start it up as well as every time the user clicks the “Next” button, but there are some things missing.
How do we “assess” the user – that is to say, how do we determine (actually, just make an “educated” guess about) their age range? For that is the whole point of the app – to guess a person’s age (in a general way) based on the selections they make.
By “in a general way,” what I mean is that we will guess the person’s age only in a very broad way, with three categories, which are represented in the collage below.

Photo of young girl © Yann Forget/Wikimedia Commons
Photo of Mark Twain by Matthew Brady is in the public domain
Photo of 95-year-old woman (and rooster, who also appears to be no spring chicken) © Jorge Royan/http://www.royan.com.ar Photo of young girl © Yann Forget/Wikimedia Commons
The young girl on the left is in the “Whippersnapper” phase of life; at the time the above photo of Mark Twain was taken, he was NYNO (Neither Young Nor Old); on the far right, the venerable old lady is well into the “Geezer” period of life (or perhaps “Geezerette” period of life, if you will).
Which category are you in? I’m guessing the “Whippersnapper” phase. I am in the NYNO stage of life, myself. Or, to be honest, I guess, I have just begun the “Geezer” phase, as that is considered to begin at 60, and I am a smidgin beyond that.
In this Step we will add the code needed to make an “educated guess” about the age of the user.
There are actually a few things we need to set up to get this done, namely:
1) Add a variable to capture the “score”
2) Prevent the user from moving on from the current question until they have made a selection
3) Add code to calculate the final “score”
4) Add a Label (or something) to display the result to the user
We will take care of the first thing listed above in this Step of our Journey.
To do so, we add a variable to keep track of what the user has selected.
In the same area where we added the sequence variable, add another variable of the same data type (int) named ageScore, so that part of the code now looks like this (the new code is bolded):
public partial class Form1 : Form
{
int sequence = 0;
int ageScore = 0;
Now we will assign a value to this new variable at the appropriate place – when the user selects the “Next” button, and the value of the choice the user made (which RadioButton they selected) can be recorded.
First, though, we need to decide how we are going to do this. What numerical value should we give for choices made?
For our purposes we will say that a score of “1” means the user chose the selection most geared toward the youngest age range, a score of “3” will mean that the user selected the choice that it is expected older people would select, and a “2” is given for the “in-between” answers. We then average the scores at the end, and see whether the user’s average score is closest to 1 and we will guess they are a “Whippersnapper,” or closest to 3 and we will guess they are a “Geezer,” or closer to 2, in which case we will view them as being NYNO (Neither Young Nor Old).
To prepare for doing this, we need to change the GroupBox and RadioButton code a little bit. We will assign each RadioButton control the appropriate value as to whether that choice is Whippersnapperish, Geezeresque, or NYNO-like. We do that by assigning the corresponding number we just talked about (1 for Whippersnappers, 2 for NYNOs, and 3 for Geezers) to the RadioButtons’ Tag property.
The Tag property, which many controls have, is a handy place to store cryptic or semi-cryptic information concerning the control. By “cryptic” I mean it’s something you are concealing from the users. Not because you’re trying to trick them, but because it is a programming detail that they don’t need to know about. In our case, we will use the RadioButtons’ Tag property to indicate whether an answer is more likely to be one selected by Whippersnappers, NYNOs, or Geezers.
Note: A “control” is a visible component on a form (or web page, for that matter) that can be interacted with. Some examples of controls are Buttons, RadioButtons, Textboxes, Checkboxes, and ComboBoxes. We will learn about various controls as we go along.
To be specific about what we’re going to change, let’s remind ourselves of what the existing code looks like:
if (sequence == 1)
{
groupBox1.Text = "Select a ROGER";
radioButton1.Text = "EBERT";
radioButton2.Text = "FEDERER";
radioButton3.Text = "MARIS";
}
...and this is what it needs to be changed to (new code bolded):
if (sequence == 1)
{
groupBox1.Text = "Select a ROGER";
radioButton1.Text = "EBERT";
radioButton1.Tag = 2;
radioButton2.Text = "FEDERER";
radioButton2.Tag = 1;
radioButton3.Text = "MARIS";
radioButton3.Tag = 3;
}
Note: I mean no disrespect to the Rogers who are not among the three here. I easily could have added Roger Williams, Roger Ramjet, or – most egregious omission of all -- Roger Miller, one of the greatest songwriters that ever lived. And doubtless other Rogers, too.
So based on this code, it is expected that young people will choose “Federer,” old people will choose “Maris,” and folks who are kind of in-between will choose “Ebert.” That is because we are using 1 to represent Whippersnappers, 3 to represent Geezers, and 2 to represent NYNOs.
But hold on. While this will work (you’ll see how later), it’s really not a great idea to use “magic numbers” like 1, 2, and 3 to mean something that has no direct connection with those numerical values. When using “magic numbers” like this, it’s easy to get confused and ask yourself, “Now wait a minute – which value does 1 correspond to – is it the youngest or the oldest?”
The solution to this is to use constants. Constants are the opposite of variables. As we learned, a variable’s value can change, or vary. On the other hand, the value you assign a constant remains the same all the time; it retains its initial value; it is constant. So we will assign names to constants that will “stand in” for, or represent, those numbers (1,2, and 3).
To do that, go to the top of the page of code where you added the two variables earlier (sequence and ageScore) and add this code:
const int WHIPPERSNAPPER = 1;
const int NYNO = 2;
const int GEEZER = 3;
As do variables, constants have data types; here, too, as with the variables, we are using ints (integers). The difference is the “const” that precedes the data type, indicating that these values are set once and for all.
Now, change the code above (which runs when the value of the variable “sequence” is 1) to this (the new code is bolded):
if (sequence == 1)
{
groupBox1.Text = "Select a ROGER";
radioButton1.Text = "EBERT";
radioButton1.Tag = NYNO;
radioButton2.Text = "FEDERER";
radioButton2.Tag = WHIPPERSNAPPER;
radioButton3.Text = "MARIS";
radioButton3.Tag = GEEZER;
}
As you can see, where it used to say “Tag = 1” it now says “Tag = WHIPPERSNAPPER; where it used to say “Tag = 2,” now it says “Tag = NYNO,” and where it used to say “Tag = 3” it now says “Tag = GEEZER.” The values are still stored as the integer values, though (1,2, or 3) – the names are just there to make it more understandable to humans when they read the code. WHIPPERSNAPPER represents, or stands in for, the number 1 (etc.).
It’s now easier to read the code without getting confused about what the numbers assigned to the Tag properties mean, right? The “explanation” or “reminder” is built right into the code by using those human-friendly constants, which provide descriptive names to represent the values.
To add this sort of code to the whole shebang, copy-and-paste the code below, replacing the LoadNextBatch() block of code with the following:
private void LoadNextBatch()
{
sequence++;
if (sequence == 1)
{
groupBox1.Text = "Select a ROGER";
radioButton1.Text = "EBERT";
radioButton1.Tag = NYNO;
radioButton2.Text = "FEDERER";
radioButton2.Tag = WHIPPERSNAPPER;
radioButton3.Text = "MARIS";
radioButton3.Tag = GEEZER;
}
else if (sequence == 2)
{
groupBox1.Text = "Select a BRUCE";
radioButton1.Text = "HERBELIN-EARLE";
radioButton1.Tag = WHIPPERSNAPPER;
radioButton2.Text = "LEE";
radioButton2.Tag = GEEZER;
radioButton3.Text = "SPRINGSTEEN";
radioButton3.Tag = NYNO;
}
else if (sequence == 3)
{
groupBox1.Text = "Select a SONG";
radioButton1.Text = "DEEP PURPLE";
radioButton1.Tag = GEEZER;
radioButton2.Text = "SHAPE OF YOU";
radioButton2.Tag = WHIPPERSNAPPER;
radioButton3.Text = "SMOKE ON THE WATER";
radioButton3.Tag = NYNO;
}
else if (sequence == 4)
{
groupBox1.Text = "Select a NOVEL";
radioButton1.Text = "1984";
radioButton1.Tag = NYNO;
radioButton2.Text = "FRANKENSTEIN";
radioButton2.Tag = GEEZER;
radioButton3.Text = "THE GOLDFINCH";
radioButton3.Tag = WHIPPERSNAPPER;
}
else if (sequence == 5)
{
groupBox1.Text = "Select a MOVIE";
radioButton1.Text = "FORREST GUMP";
radioButton1.Tag = NYNO;
radioButton2.Text = "IT'S A WONDERFUL LIFE";
radioButton2.Tag = GEEZER;
radioButton3.Text = "WONDER WOMAN";
radioButton3.Tag = WHIPPERSNAPPER;
}
else if (sequence == 6)
{
groupBox1.Text = "Select a TELEVISION SHOW";
radioButton1.Text = "THE DICK VAN DYKE SHOW";
radioButton1.Tag = GEEZER;
radioButton2.Text = "GAME OF THRONES";
radioButton2.Tag = WHIPPERSNAPPER;
radioButton3.Text = "THE OFFICE";
radioButton3.Tag = NYNO;
}
else if (sequence == 7)
{
groupBox1.Text = "Select an AUTHOR";
radioButton1.Text = "DAV PILKEY";
radioButton1.Tag = WHIPPERSNAPPER;
radioButton2.Text = "JOHN GRISHAM";
radioButton2.Tag = NYNO;
radioButton3.Text = "MARK TWAIN";
radioButton3.Tag = GEEZER;
}
else if (sequence == 8)
{
groupBox1.Text = "Select a BAND";
radioButton1.Text = "BENNY GOODMAN";
radioButton1.Tag = GEEZER;
radioButton2.Text = "GRAND FUNK";
radioButton2.Tag = NYNO;
radioButton3.Text = "IMAGINE DRAGONS";
radioButton3.Tag = WHIPPERSNAPPER;
}
else if (sequence == 9)
{
groupBox1.Text = "Select a GIRL'S NAME";
radioButton1.Text = "EMMA";
radioButton1.Tag = WHIPPERSNAPPER;
radioButton2.Text = "GERTRUDE";
radioButton2.Tag = GEEZER;
radioButton3.Text = "KATHY";
radioButton3.Tag = NYNO;
}
else if (sequence == 10)
{
groupBox1.Text = "Select a BOY'S NAME";
radioButton1.Text = "HORACE";
radioButton1.Tag = GEEZER;
radioButton2.Text = "JOHN";
radioButton2.Tag = NYNO;
radioButton3.Text = "JOSHUA";
radioButton3.Tag = WHIPPERSNAPPER;
}
}
Earth-shakingly Important Note: If you have a basic programming question (suitable to an audience of “Kids”), send it to idiolectable@gmail.com, specifying whether you would like your name and location used if it is printed in a future “Step” of this newsletter. A nickname is acceptable (the first “Letter to the Editor” of mine that was printed appeared in Rolling Stone magazine, back in the early 1970s, and I signed it “Sylvester” for some reason which I no longer remember). Also, it’s always interesting to see where people are from, so please provide your City or Town and the State it’s in, too (or Province, or whatever the region where you live is called).
In the next Step, we will add code to prevent the user from moving on until he has made a selection of one of the RadioButtons.
The audio version of the Step can be found here: