Namespaces and the Base Classes

The Base Classes

There are many more namespaces, and the best way to learn about them and the classes they contain is to explore them yourself - and Microsoft have provided a tool for just this purpose. That's what we'll look at next.

The WinCV Tool

WinCV is a class viewer tool, which can be used to examine the classes available in shared assemblies- including all the base classes. (A shared assembly is an assembly that has been marked for use by other applications - we'll cover shared assemblies in Chapter 10). For each class, it lists the methods, properties, events and fields using a C#-like syntax.

At the present time you start it by typing in wincv at the command prompt or clicking on the file in Windows Explorer. This gives you a basic window with two panes, as shown.

You then type in a search string that is contained in the class you are looking for in the Searching For box. For example if you want to find a class that handles dates, you might try typing in Date in this box. As you type, WinCV will display a list of classes that contain the string in question in their names:

As this screenshot shows, there are a lot of classes whose name contains the string date - then it's just a question of scanning through the names to find the one that looks most suitable. The definition of the selected class is then displayed in the right hand pane. The left hand pane also indicates the namespace the class is contained in - you'll need this in order to be able to use that class in your C# code. In the case of DateTime, WinCV tells us that it is part of the System namespace, which means that the line

using System;

that is placed by the AppWizard by default in most C# projects will be sufficient to give us access to the class without explicitly indicating the namespace.

The right hand pane of WinCV tells us which assembly the class is defined in - the screenshot shows us that System.DateTime is defined in mscorlib.dll. This information is useful to indicate if we need to link in any assemblies when we compile, in order to have access to the class. Again, the above screenshot tells us that we are OK for the DateTime class, since it is defined in mscorlib.dll, which is, by default, referenced anyway.

Now we've laid the groundwork, the rest of this chapter is devoted to presenting a number of sample C# projects which are designed to illustrate how to carry out some common programming tasks that are made easy by the base classes.

About the Samples

Before we go through the samples in this chapter, we'll quickly go over how we are structuring them. Since in most cases all we want to do is perform a few operations such as writing to a file or accessing some registry keys then displaying the results, it would be simplest to provide console samples - that way we minimize the extraneous code. However in practice, very few Windows applications are written as console applications, and it would be nice to put the output in a more realistic environment. Accordingly, the samples will all be presented as Windows applications, based on a form that has a list box, as shown here.

We haven't yet covered how to write Windows applications in C# - that's the subject of Chapter 8, but we'll give you just enough information here for you to understand how the samples are created.

Each project is created as a C# Windows application: So when we start up the Visual Studio developer environment and select New Project, we fill in the New Project dialog like this (though supplying our own name and path for the project).

Once the project has been created, we use the Toolbox in the developer environment to place a List box on the form - by default this will be called listBox1. The List box is actually an instance of a C# object of type ListBox, from the System.WinForms namespace.

We then examine the code the wizard has generated for us and make the following changes (shown highlighted).

public class Form1 : System.WinForms.Form
{
   /// <summary> 
   ///    Required designer variable
   /// </summary>
   private System.ComponentModel.Container components;
   private System.WinForms.ListBox listBox1;
   
   private int nItems=0;
   public void AddItem(string sItem)
   {
      object oItem = sItem;
      listBox1.InsertItem(nItems,oItem);
      ++nItems;
   }

   public Form1()
   {
      //
      // Required for Win Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //

      AddItem("An item of output");
      AddItem("2nd bit of output");
      AddItem("More output");
   }
   ...

The code that we are adding is to the Form1 class which represents the entire form (window) created when the program is run. We first add a member field, nItems, and a member function, AddItem(). These are to make it easier for us to add lines of text to the list box. This is normally done by using the member function, InsertItem() of the ListBox class. But in its simplest overload, this function takes two parameters - the zero-based index of where we wish to insert the item, and the item itself, passed as an object (InsertItem() will use the ToString() function to extract the string to be added). Since we wish to add each successive item to the end of the list box, this would require us to keep a count of how many items we have added. Our AddItem() function automates that process for us.

   private int nItems=0;
   public void AddItem(string sItem)
   {
      object oItem = sItem;
      listBox1.InsertItem(nItems,oItem);
      ++nItems;
   }

I should mention we could also add the items to the list box with the line of code

      listBox1.Items.Add(oItem);

though either way our function does marginally simplify the code.

We then actually add the items in the Form1 constructor. In this case we have simply added three strings.

      AddItem("An item of output");
      AddItem("2nd bit of output");
      AddItem("More output");

In all the subsequent samples these three lines will be replaced by the code to do whatever processing is required to illustrate the task in hand, followed by AddItem() calls to display the results. Each of the projects in this chapter were created in essentially the same way to the above example. Note however that in the following samples, we've also changed the name of the Form1 class and the namespace that it appears in. The developer environment by default gives us a namespace that has the same name as the project, however Microsoft guidelines say that the namespace should start with the company name (which makes sense, as you would not have any name clashes with other company's classes). So for all the projects here we'll use the namespace Wrox.BookSamples.CSharpPreview.ChBaseClasses.

You should bear in mind when reading through the samples that some of the classes may be modified before the .NET SDK is finally released. Hence they will give you a good idea of how to carry out tasks, but some of the names or signatures of methods may be slightly different. Also in many cases there are several classes or methods whose functionality overlaps. Just because we've presented a certain way of doing something doesn't mean there won't be alternative ways of doing the same task.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor