REAL-WORLD COMPUTER PROGRAMMING FOR KIDS
STEP 21: ALL THE CODE, AND NOTHING BUT THE CODE (PLUS A LITTLE MORE)
In this step, I am going to show you all the code as it now stands. First, a “panoramic” (but vertical, instead of the more customary horizontal) screen shot of all the code as it appears in Visual Studio. This way you can see how Visual Studio colorizes various elements of the code to give your brain mental shortcuts as to what it is you are looking at. Then, I will paste in the code in the “regular” way (text that you can copy-and-paste).
So without further ado or adon’t, here’s the code as a vertical “scroll”:

Just as the layout of code eventually appears logical to us, whereas at first it just seems like a cacaphonous, jumbled-up mess, this wild display of colors has a beauty of its own, too. Of course, YMMV.

Bob and Doug McKenzie, in the documentary of Canadian life “Strange Brew” (which title they stole from a song by the psychedelic blues band Cream)
Now for the same code, but in the relatively tame black on white (its saving grace being that it is copy-and-pastable, or printable). In fact, you may want to print it out, so that you can annotate it with your own comments and questions; or perhaps, like me, you like to sit out in the sun and pore over your code (or somebody else’s code), taking your time to let it sink in (the rays and the code, that is).
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace GuessingAges
{
public partial class Form1 : Form
{
int MultipleChoiceClassIndex = -1;
int stepAmount = 0;
int numberOfQuestions = 0;
int questionsAnswered = 0;
int correctAnswers = 0;
string originalQuestionNofN;
string originalPercentageStr;
bool freezeAnswer = false;
List<MultipleChoiceClass> mccList;
public Form1()
{
InitializeComponent();
radioButton1.AutoCheck = true;
radioButton2.AutoCheck = true;
radioButton3.AutoCheck = true;
}
private void Form1_Load(object sender, EventArgs e)
{
originalQuestionNofN = lblQuestionNOfN.Text;
originalPercentageStr = lblPercentage.Text;
}
private void cmbxSelectMultiChoiceFile_DropDown(object sender, EventArgs e)
{
if (cmbxSelectMultiChoiceFile.Items.Count > 0) return;
string pathToSearch = @"C:\Programming4Kids\";
string[] fileNames;
fileNames = GetFileNames(pathToSearch);
foreach (string file in fileNames)
{
cmbxSelectMultiChoiceFile.Items.Add(file);
} // foreach
} // cmbxSelectMultiChoiceFile_DropDown
private string[] GetFileNames(string pathToSearch)
{
string[] _fileNames;
_fileNames = Directory.GetFiles(pathToSearch, "*.csv");
return _fileNames;
}
private void cmbxSelectMultiChoiceFile_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedFile = string.Empty;
string[] fileStrArray;
MultipleChoiceClass mcc;
double stepPercentage = 0.0;
const int QUESTION = 0;
const int CANDIDATEANSWER1 = 1;
const int CANDIDATEANSWER2 = 2;
const int CANDIDATEANSWER3 = 3;
selectedFile = cmbxSelectMultiChoiceFile.SelectedItem.ToString();
// Subsequent times this runs, mccList will not be null
if (mccList is null)
{
mccList = new List<MultipleChoiceClass>();
}
else // if mccList already exists, remove its elements to prevent re-populating them:
{
mccList.Clear();
// ...and update progress controls
lblQuestionNOfN.Text = originalQuestionNofN;
lblPercentage.Text = originalPercentageStr;
progressBar1.Value = 0;
}
try
{
fileStrArray = File.ReadAllLines(selectedFile, Encoding.UTF8);
numberOfQuestions = fileStrArray.Length;
stepPercentage = 100 / numberOfQuestions;
stepAmount = (int)Math.Round(stepPercentage);
foreach (string line in fileStrArray)
{
string[] subPhrases = line.Split(',');
mcc = new MultipleChoiceClass();
mcc.Question = subPhrases[QUESTION];
mcc.CandidateAnswer1 = subPhrases[CANDIDATEANSWER1];
mcc.CandidateAnswer2 = subPhrases[CANDIDATEANSWER2];
mcc.CandidateAnswer3 = subPhrases[CANDIDATEANSWER3];
if (mcc.CandidateAnswer1.Contains("*"))
{
mcc.CorrectAnswer = 1;
mcc.CandidateAnswer1 = mcc.CandidateAnswer1.Substring(1);
}
else if (mcc.CandidateAnswer2.Contains("*"))
{
mcc.CorrectAnswer = 2;
mcc.CandidateAnswer2 = mcc.CandidateAnswer2.Substring(1);
}
else if (mcc.CandidateAnswer3.Contains("*"))
{
mcc.CorrectAnswer = 3;
mcc.CandidateAnswer3 = mcc.CandidateAnswer3.Substring(1);
}
mccList.Add(mcc);
} // foreach
LoadNextBatch();
} // try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
} // cmbxSelectMultiChoiceFile_SelectedIndexChanged
private void LoadNextBatch()
{
MultipleChoiceClassIndex++;
freezeAnswer = MultipleChoiceClassIndex + 1 == numberOfQuestions;
btnNext.Enabled = false;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
try
{
UpdateQuestionNumberLabel();
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value = questionsAnswered * stepAmount;
}
if (MultipleChoiceClassIndex < numberOfQuestions)
{
lblQuestion.Text = mccList[MultipleChoiceClassIndex].Question;
radioButton1.Text = mccList[MultipleChoiceClassIndex].CandidateAnswer1;
radioButton2.Text = mccList[MultipleChoiceClassIndex].CandidateAnswer2;
radioButton3.Text = mccList[MultipleChoiceClassIndex].CandidateAnswer3;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
}
private void UpdateQuestionNumberLabel()
{
const string baseQNOfN = "Question {0} of {1}";
int currentQuestionNumber = questionsAnswered + 1;
lblQuestionNOfN.Text = string.Format(baseQNOfN, currentQuestionNumber, numberOfQuestions);
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (freezeAnswer)
{
questionsAnswered = numberOfQuestions;
UpdatePercentageCorrect();
btnNext.Enabled = false;
return;
}
btnNext.Enabled = (sender as RadioButton).Checked;
}
// Called from the "Next" button just before moving on to the next question
private void UpdatePercentageCorrect()
{
double percentageCorrect = 0.0;
if (AnswerIsCorrect())
{
correctAnswers++;
}
percentageCorrect = (Convert.ToDouble(correctAnswers) /
Convert.ToDouble(questionsAnswered)) * 100;
lblPercentage.Text = percentageCorrect.ToString() + "%";
progressBar1.Value = questionsAnswered * stepAmount;
}
private bool AnswerIsCorrect()
{
int correctAnswer = 0;
int answerSelected = 0;
correctAnswer = mccList[MultipleChoiceClassIndex].CorrectAnswer;
if (radioButton1.Checked) { answerSelected = 1; }
else if (radioButton2.Checked) { answerSelected = 2; }
else if (radioButton3.Checked) { answerSelected = 3; }
return (answerSelected == correctAnswer);
}
private void button1_Click(object sender, EventArgs e) // "Next" button
{
if (questionsAnswered < numberOfQuestions)
{
questionsAnswered++;
}
UpdatePercentageCorrect();
if (questionsAnswered == numberOfQuestions) return;
LoadNextBatch();
}
} // Form1
} // namespace GuessingAges
And we cannot forget this important piece of code, from the MultipleChoiceClass.cs code file:
class MultipleChoiceClass
{
public string Question = string.Empty;
public string CandidateAnswer1 = string.Empty;
public string CandidateAnswer2 = string.Empty;
public string CandidateAnswer3 = string.Empty;
public int CorrectAnswer = 0;
}
In the next Step, we will get another view of the code, not as it appears in Visual Studio, but the lines of code that actually run, in order.
Until then!
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: