Step 2: Add a Random Object and a List of Icons

You need to use two new statements to create two objects and add them to your form. The first is a Random object, like the one you used in the math quiz game. The second is a new one: a List object.

To add a Random object and a list of icons

  1. Before you add the following code to create the list, consider how it works.

    public partial class Form1 : Form
    {
        // Use this Random object to choose random icons for the squares
        Random random = new Random();
    
        // Each of these letters is an interesting icon 
        // in the Webdings font, 
        // and each icon appears twice in this list
        List<string> icons = new List<string>() 
        { 
            "!", "!", "N", "N", ",", ",", "k", "k",
            "b", "b", "v", "v", "w", "w", "z", "z"
        };
    
  2. Go to the code editor by right-clicking Form1.cs in Solution Explorer, and then clicking View Code from the menu. Start typing the code shown in the previous step. If writing Visual C# code, be sure you put the code after the opening curly brace, and just after the class declaration (public partial class Form1 : Form). If writing Visual Basic code, put the code right after the class declaration (Public Class Form1).

  3. When adding the List object, take a close look at the IntelliSense window that opens. The following is a Visual C# example. (Similar text appears if you add a list in Visual Basic.)

    IntelliSense window

    Properties window showing Click event
    Note Note

    If you look at the code in small sections, it's easier to understand. Your programs can use List objects to keep track of many items. A list can hold numbers, true/false values, text, or other objects. You can even have a List object that holds other List objects. The items in a list are called elements, and each list only holds one type of element. So a list of numbers can only hold numbers—you can't add text to it. Also, you can't add numbers to a list of true/false values.

    Note Note

    When you create a List object using a new statement, you need to tell it what you want to keep in it. That's why the tooltip at the top of the IntelliSense window shows the type of elements in the list. Also, that's what List<string> (in Visual C#) and List(Of String) (in Visual Basic) means: It's a List object that holds strings. A string is what your program uses to store text, which is what the tooltip to the right of the IntelliSense window is telling you.

  4. Consider why in Visual Basic a temporary array must be created first, but in Visual C#, the list can be created with one statement. This is because the Visual C# language has collection initializers. In Visual Basic 2010, you can use a collection initializer. However, for compatibility with the previous version of Visual Basic, we recommend using the preceding code.

    Note Note

    When you use a collection initializer with a new statement, after the new List object is created, the program fills it with whatever is inside the curly braces. In this case, you get a list of strings named icons, and that list will be initialized so that it contains sixteen strings. Each of those strings is a single letter, and they all correspond to the icons that will be in the labels. So the game will have a pair of exclamation points, a pair of uppercase N letters, a pair of commas, and so on. Your List object will have sixteen strings in all, one for each cell in the TableLayoutPanel.

    Note Note

    In Visual Basic, you get the same result, but first the strings are put into a temporary array, which is then converted into a List object. An array is similar to a list, except, for example, arrays are created with a fixed size. Lists can shrink and grow as needed, which is important in this program.

To continue or review

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft