REAL-WORLD COMPUTER PROGRAMMING FOR KIDS
STEP 15: YET MORE NECESSARY GROUNDWORK FOR READING CSV FILES INTO OUR APP
So we left off Step 14 with a thought about Visual Studio helping us out with writing some of our code. We will get to that “right quick,” but first, a word from ... me:
From this intallment of the Newsletter or “Step in our Journey,” I am slightly altering its name. I am prepending “Real-World” to the title so that it is now “Real-World Computer Programming for Kids.” The reason? That is the way that this Journey differs perhaps from a lot of programming books and articles and lessons. Rather than providing a sterilized, “photoshopped,” after-the-fact pristine image of the coding process, or just dealing in theory and “best-case scenarios,” we are working through it together, with all the challenges that are normally encountered in actual programming experience: finding blemishes in the code, debugging, lots of Refactoring, etc. It’s not always easy, but we’re in this together!
Now back to our normal programming (no pun intended).
As to Visual Studio lending you a helping hand with your coding chores: Yes, it can sometimes do that! Check this out:
With your cursor in the GetFileNames() method, notice that an idea/error icon (light bulb with a circled red “x”) appears:

Click its down arrow (just to the right of the lightbulb/circled red “x”).
When you do so, Visual Studio gives you a preview of what code it will automatically generate for you if you click “Generate method ...”:

Visual Studio is willing to create our method for us, adding three things, as you can see above:
First, a “private” designation so that unauthorized code cannot call it (blocks of code need to be marked “public” if you want to allow code from other places to call them – as an example, our custom Class, MultipleChoiceClass, has members that are declared public; as the class is in its own file, we need them to be public so as to access them from Form1.cs).
Second, on the same line, just following “private,” we see an appropriate return value data type (ComboBox.ObjectCollection). This is appropriate because we are assigning the returned value of the method call to the ComboBox’s Items (which are of the data type ComboBox.ObjectCollection).
Third, inside the curly braces, is some “boilerplate/placeholder” code, namely throw new NotImplementedException(); That code is there mainly to prevent an error about the method not returning a valid value, which would prevent the project from compiling. If we had no code there, Visual Studio would complain because the method, which is supposed to return a ComboBox.ObjectCollection, would be returning nothing. The “NotImplementedException” is basically telling us, “Hey, dude! (or dudette!), you forgot to put some code here returning a value of the appropriate type!” Or then again it might be saying, in a sense, “Pardon me, young man!” or “Pardon me, young lady!” instead of calling you “dude” or “dudette” which may be too familiar for your taste. You choose how polite or hoity-toity the compiler is.
Note: When the program fails with an error (or “Exception” in programming parlance) it is called (by C#) “throwing an exception.” That is why you see the word “throw” in the placeholder code above. You might think of it as the compiler “throwing a fit.” But the compiler is not really angry at you -- or anybody, really. It’s actually helping you when it points out errors (“exceptions”). It is the epitome of what’s called “constructive criticism.” So when your code causes an error, it’s not called a “Boo-boo” or an “Oopsie” or even “pitching a fit” (an expression commonly used in the American South and by The Eagles in their song Get Over It). It’s called “throwing an exception.”
Click the “Generate method” text so that we don’t have to type in all that stuff ourselves (of course, you can copy-and-paste the code we end up with from below, if you want to).
It is actually easier, I think, and definitely more intuitive (at this stage of the game, at least) to use a different data type (than ComboBox.ObjectCollection) for the method’s return type. We will use a string array, which is designated with the word “string” followed by two brackets, like this: string[]. Sorry if that looks like the Leaning Tower of Pisa at the end of “string” there. There should actually be some space between the left bracket and the right bracket, but if you literally put a space between them, it wouldn’t compile, and I don’t want to set a bad example by entering code that won’t fly, so to speak, so ... it is what it is, as they say.

An “array” is a fancy word that just means “a bunch of.” So a string array (also called an array of string) is a data type that contains “a bunch of” strings, not just one. It’s like a “value pack” of strings.
In our case, the string array will contain as many filename strings as there are in the folder where we placed the CSV files.
Enter the following code in the ComboBox’s DropDown event handler:
private void cmbxSelectMultiChoiceFile_DropDown(object sender, EventArgs e)
{
string pathToSearch = @"C:\Programming4Kids\";
string[] fileNames;
if (cmbxSelectMultiChoiceFile.Items.Count == 0)
{
fileNames = GetFileNames(pathToSearch);
foreach (string file in fileNames)
{
cmbxSelectMultiChoiceFile.Items.Add(file);
}
}
}
Before we go into just what all this code is doing, I was reminded when looking at it how baffled I was the first time I saw code like this. To me it seemed like completely random jibberish – and uglier than a burnt stump, to boot!
But now that I understand it, I find some code very aesthetically pleasing to contemplate (downright beautiful); Poetry in repose. Enough of that waxing rhapsodic, though. The point is that the code actually makes sense the way it’s laid out. Most specifically, the curly braces are not just sprinkled in there willy-nilly. Each pair lines up with its counterpart. For every left curly brace, there is a right curly brace below it. All the code within a pair of curly braces runs in partnership.
To help you keep it straight (which set of curly braces belong to which), you might want to add a comment at the end of each right, or ending, curly brace.
To demonstrate what I’m talking about (actually, writing about) more clearly, here is a screenshot of the code above in Visual Studio, with comments that I added at the end of the right (ending) curly braces, showing which block of code they belong to:

As you can see, Visual Studio itself helps you out -- there are dotted vertical lines connecting each pair of curly braces. These connecting lines are not code; the compiler doesn’t “see” them, or pay attention to them; they are provided purely as visual assistance to you, the programmer. All you have to do is follow these dotted lines to see where its matching curly brace is.
Glancing at code and understanding small blocks of code within a bigger block of code becomes second nature after a while; instead of the code looking like a bunch of goobledyflip, it actually looks quite natural -- and even, dare I say, poetical (although that sounds like something Anne (with an “e”) of Green Gables would say).

That was quite a detour, but I think it was worthwhile. Now let’s get back to the code, and explain “what all” it’s doing. We’ll take it bit by bit, so to speak:
string pathToSearch = @"C:\Programming4Kids\";
The path to the folder which contains our CSV files is assigned to a string variable.
string[] fileNames;
An array of string, or string array is declared.
if (cmbxSelectMultiChoiceFile.Items.Count == 0)
The code above checks to see if the ComboBox does not yet contain any items. If so (the ComboBox is empty), the next block of code will run. Conversely, if the CombBox does contain 1 or more items, the following code will not run (to prevent the same file names from being added to the ComboBox multiple times).
fileNames = GetFileNames(pathToSearch);
The string array (fileNames) calls the method named GetFileNames(), passing the string variable which contains the path to the CSV files. The return value of GetFileNames() is assigned to the string array. We will examine the code in the GetFileNames() method directly.
foreach (string file in fileNames)
A “foreach” satement is used to iterate over the string array. That means, for each string in the fileNames array, whether it be 1 or 1 million, the code in the foreach block will run, adding the current value of “file” (a filename) to the ComboBox. If there are a million, it will take longer, though.
cmbxSelectMultiChoiceFile.Items.Add(file);
The current string in “file” is added to the ComboBox.
Now let’s dissect the code in our custom method, which looks like this:
private string[] GetFileNames(string pathToSearch)
{
string[] _fileNames;
_fileNames = Directory.GetFiles(pathToSearch, "*.csv");
return _fileNames;
}
We will go over it a line at a time, too.
string[] _fileNames;
This is a “local” (seen only within this method) string array named _fileNames. It is good to prepend an underscore to local variables that have the same name as other variables in your code to avoid confusion. This keeps them “separate” in your brain.
_fileNames = Directory.GetFiles(pathToSearch, "*.csv");
We call the native method Directory.GetFiles() method, passing it the path where we have saved our CSV files as the first parameter, or argument, and then a filter as the second parameter (or argument). This method returns a string array of file names. The filter (“*.csv”) tells it to only grab filenames that end with the “.csv” file extension.
You don’t have to do this, though. You can either leave your files with their default extension of “.txt” and change the filter above accordingly (to “*.txt”) or you can just leave out the second (filtering) parameter altogether. But realize that all files in the folder will be seen and their names returned and assigned to the _fileNames string array – something you may not want if they are not all valid CSV files in that folder.
In other words, it’s preferred that you change the extension of the CSV file names from “.txt” to “.csv” if possible. This will prevent problems if other types of files get saved into your “Programming4Kids” folder. Also, after renaming them as “*.csv” you can not only load them in Notepad, but also in your spreadsheet software (such as Microsoft Excel or Libre Office Calc). CSV files and spreadsheet software get along just fine. The spreadsheet software will automatically strip out the commas, and put each item in its own cell. Try it if you want to – from Windows Explorer, right-click one of your *.csv files and select Open With > [ your spreadsheet program, whether it be Microsoft Excel, Libre Office Calc, Google Sheets, Visicalc, or whatever ]
return _fileNames;
Finally, our method returns the string array (or array of string) to the caller which, in turn, assigns that value to the fileNames string array in the ComboBox’s DropDown event code.
Note: By “native method” above I mean that Directory.GetFiles() “comes in the box” with C#/Visual Studio. We didn’t have to write it ourselves. However, there’s a slight catch. To use it, we have to add this at the top of our code file:
using System.IO;
In context, it will look something like this, once added to our other “using” clauses:
using System;
using System.Windows.Forms;
using System.IO;
That’s enough for now. In our next Step, #16, we will (Yes! Finally!) actually read the files into our app.
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. 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).
To listen to this Step, the audio of it can be found here: