REAL-WORLD COMPUTER PROGRAMMING FOR KIDS
STEP 26: STEPS ON STEPPING (and BREAKPOINT BONANZA), Part 2
After another thorough Desk Check (reading the code away from the computer, in this case, after printing it out), I made some more changes, to wit:
In an effort to simplify the code, I did away with the GetFileNames method altogether. After all, rather than call that method, I could just directly do the same thing the method was doing on the line where I had been calling it.
Note: I’m not going to show all of the code changes, because a lot of them were relatively minor or stylistic in nature, but I will provide all the code again once everything is working just as it should be.
I will show this change, though, because it is non-trivial. Here is the “before” picture of the code:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
freezeAnswer = MultipleChoiceClassIndex == numberOfQuestions;
if (freezeAnswer)
{
questionsAnswered = numberOfQuestions;
UpdatePercentageLabel();
btnNext.Enabled = false;
return;
}
btnNext.Enabled = (sender as RadioButton).Checked;
}
...and here is the “after” picture:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (questionsAnswered == (numberOfQuestions-1)) // if true, we're on the last Question
{
questionsAnswered++;
MultipleChoiceClassIndex++;
}
freezeAnswer = MultipleChoiceClassIndex+1 == numberOfQuestions;
if (freezeAnswer)
{
UpdatePercentageLabel();
btnNext.Enabled = false;
radioButton1.Enabled = false;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
return;
}
btnNext.Enabled = (sender as RadioButton).Checked;
}
Now I will run it again to see what, if any, problems arise. If they do, we will continue with Stepping through the code to track down the problem. In fact, I’m sure there will still be at least one “issue” with the code.
Here goes something (not “nothing,” as is popularly held to be what it is that is going):
Fortunately for us (since we want to demonstrate some more Stepping through the code), there are still plenty of problems with the code. Rather than list them all (I found a handful already), let’s just attack/tackle them one at a time.
So, the first one is: After selecting a file from the Combo Box, the “Question N of N” label (lblQuestionNofN) still says just that, but it should be “Question 1 of 10” (since the file I selected has 10 questions in it).
The thing to do, then, is to see where an assignment is made to lblQuestionNofN, and figure out why it isn’t made right after the file is selected.
I search the code from the top for “lblQuestionNOfN”; the first “hit” is where the original (Design Time) value of its Text is assigned in the InitializeForm method:
// Called from PopulateList() (when a new file is loaded)
private void InitializeForm()
{
lblQuestionNOfN.Text = originalQuestionNofN;
. . .
Notice in the comment that this is called from the PopulateList method. So this is called after a file is selected. It should not be setting the Label to that original “generic” value, but rather to what we are expecting to see. So this is probably where the problem is.
I changed the line of code shown above to:
lblQuestionNOfN.Text = string.Format("1 of {0}", numberOfQuestions);
It still doesn’t work! What is happening? Is that comment misleading us? Let’s find out for sure just where this method is called from. The only other place in the code that is setting the value of lblQuestionNOfN is from the Click Event Handler of the “Next” button, and I didn’t click it yet, so that’s pretty much out of consideration as the culprit of this bug that we are hunting.

Going to the code that references (calls) the InitializeForm method, the comment was not lying – the PopulateList method does call it. BUT ... not always. Check it out:
if (mccList is null) // Subsequent times this runs, mccList will not be null
{
mccList = new List<MultipleChoiceClass>();
}
else // if mccList already exists, remove its elements to prevent re-populating them
{
mccList.Clear();
InitializeForm();
}
It’s only called on subsequent times a file is selected – not the first time! So I changed the code like so:
if (mccList is null) // Subsequent times this runs, mccList will not be null
{
mccList = new List<MultipleChoiceClass>();
}
else // if mccList already exists, remove its elements to prevent re- populating them
{
mccList.Clear();
}
InitializeForm(); // moved down here, so it’s called regardless
Now the PopulateList method calls the InitializeForm method whether it’s the first time or the gazillionth time.
I tried it again. What do you think happened? ...
It worked! Sort of. It now says, “1 of 0” (instead of 0 of 0). So why does it not realize that there are ten questions instead of 0?
Now the question is, where is the numberOfQuestions variable assigned the correct number?
Eureka! Aha! Voila! And all that jazz. It is getting assigned after we call IntializeForm, as you can see here (key lines italicized):
if (mccList is null) // Subsequent times this runs, mccList will not be null
{
mccList = new List<MultipleChoiceClass>();
}
else // if mccList already exists, remove its elements to prevent re-populating them
{
mccList.Clear();
}
InitializeForm();
try
{
fileStrArray = File.ReadAllLines(selectedFile, Encoding.UTF8);
numberOfQuestions = fileStrArray.Length;
. . .
So I move the call to InitializeForm below the assignment to numberOfQuestions:
if (mccList is null) // Subsequent times this runs, mccList will not be null
{
mccList = new List<MultipleChoiceClass>();
}
else // if mccList already exists, remove its elements to prevent re-populating them
{
mccList.Clear();
}
try
{
fileStrArray = File.ReadAllLines(selectedFile, Encoding.UTF8);
numberOfQuestions = fileStrArray.Length;
InitializeForm();
. . .
... and run it again.
This time it works. Something we can deduce from this is that “timing is everything.” This applies in life, too; for example, you shouldn’t sleep while you’re eating, and you shouldn’t eat while you’re sleeping.
Okay, on to the next bug, I mean opportunity; or . . . challenge, at least.
When I select the correct answer on Question 1 and then select the “Next” button, among other things the “Percentage” Label is updated. But it says, “0% (0 of 1)”; it should say, “100% (1 of 1).”
Why is it not accepting my correct answer as being correct?
Let’s find out.
Where is lblPercentage being assigned a value?
Logically enough, it is in the UpdatePercentageLabel method (last line of code below):
private void UpdatePercentageLabel()
{
double percentageCorrect = 0.0;
if (questionsAnswered == 0) return; // This should never be true, but if it is, there is no need to continue
if (AnswerIsCorrect())
{
correctAnswers++;
}
percentageCorrect = (Convert.ToDouble(correctAnswers) /
Convert.ToDouble(questionsAnswered)) * 100;
double prcntg = Math.Round(percentageCorrect, 2);
lblPercentage.Text = string.Format(prcntg.ToString() + "% ({0} of {1})", correctAnswers, questionsAnswered);
}
So, I put a Breakpoint on the first “real” line of code (the declaration and assignment of 0.0 to the percentageCorrect variable isn’t “steppable,” because the Preprocessor compiles that prior to this point). You don’t need to think about Preprocessors any more than Pterodactyls, though -- not at present, anyway.

Back to ultra-super-serious business! We hit the Breakpoint and, when we get to the call to AnswerIsCorrect, F11 into that. Can you see what the problem is?

The Indexer (MultipleChoiceClassIndex) is on 1 (as shown by the rectangled portion of that screen shot), whereas it should be 0, since this is the first question (and we start counting at 0). No wonder it said I chose the wrong answer – it was comparing my answer to the answer for the next question!
That’s enough for today, though. We will take up with that sticky wicket* in the next scintillating and mind-boggling Step in this Journey.
* “Sticky Wicket” is British-ese (a form of English) for “a hard problem,” in case you didn’t know. It comes from their national sport of Cricket, where villages vie against one another to see how many crickets they can drive into a field (so that they can capture the Crickets in order to feed them to their Bearded Dragons – pretty much all British people have at least one Bearded Dragon).

Until then!
The first volume of my four-volume series, Real-World Computer Programming for Kids of All Ages, namely Volume 1: Windows Forms Apps Using C# and Visual Studio is now available in both Paperback and Kindle formats. Volume 1 contains everything that has been printed in this Newsletter so far and through Step 34.
The kindle version of the book can be accessed here: https://www.amazon.com/dp/B08H7DKKCS/ and the Paperback version can be accessed here https://www.amazon.com/dp/B08H9RB1WG/

Earth-shakingly Important Notice: 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. If you are a subscriber to the newsletter, you can also leave a question at the bottom of this Step, in the “Comments” section.
If you do not want to give your real name, 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).
Finally, 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).
To listen to this Step, the audio of it can be found here: