Learn from the Best

Before deciding anything about what to recommend, I like to do some reading and some simple programs, to get a feel for the situation. In this case, I bought a number of books on C# and .NET that looked useful. I started my first experiments using Charles Petzold s book, Programming Windows with C# (Microsoft Press, 2002). I have used his books in the past, and I like his close to the iron approach. You might prefer a different teacher, but working through some examples is always a good idea.

Following Petzold, I created a new solution from the menu File/New/Blank Solution. I gave the project the name Petzold and put the files in my directory C:/Data/csharp. Next I added an empty C# project to the solution, using File/Add Project/New Project. Petzold likes to have us type in all the code on our own. You might prefer to create a C# Console Application or a Windows Form. In this book, we re creating Console Applications as a rule, because we can view debug output more readily, and they are easy enough to convert to pure Windows Forms when the time comes. For this first example, I created an empty project as instructed. Then I added a new C# file to the project, using Project/Add New Item and selecting Code File. I named it ConsoleHelloWorld. Then I typed in the program, much as Petzold had suggested:

 class ConsoleHelloWorld { 
public static void Main() {
System.Console.WriteLine("Hello, world!");

This program compiles. (I use Ctrl+Shift+B, Build Solution.) When I run it, using the menu Debug/Start, it opens a black DOS kind of window, displays Hello, world! , and says Press any key to continue. After the traditional brief search for the Any key, I press enter and the window disappears. Success!

For our next trick, I tried a Windows Dialog version of Hello World, after skimming through Petzold s very good description of C# s namespaces, console I/O, and general language information. Depending on the languages you already know, you might choose to do a few more experiments using the console, but it all looked familiar enough, so I moved on.


We re doing experiments to get a sense of how to do things and how hard they are to do. Often there is more value in trying a very different kind of experiment, rather than digging deeply into the current one. Since our application is supposed to be a Windows Forms kind of thing, it s probably best to move in that direction early and to fill in some of the unknowns about C# as we go. As always, you ll want to find your own balance on this, but my practice is to move to a simple experiment that will teach something very new ”in this case, a Dialog.

We ll add a new empty project, named DialogHelloWorld, and a new C# file of the same name, with these contents:

 class MessageBoxHelloWorld { 
public static void Main() {
System.Windows.Forms.MessageBox.Show("Hello, world!");

Now this won t even compile. The compiler says The type or namespace name ˜Windows does not exist in the class or namespace ˜System . This is one of the irritating aspects of building things entirely by hand, as Petzold teaches us. The good news is that we learn the details, but it s much easier to use some of the more advanced templates, rather than empty project and so on. I ll come back to that in a moment. What s going on here is that we have to add the references needed by this program.

In the Solution Explorer, right-click the References line under DialogHelloWorld and select Add Reference. This brings up a dialog with a tabbed window offering .NET, COM, and Projects. In .NET, we ll choose System.Windows.Forms.dll, press the Select button, and then OK. Now the program should compile, and it does. And it will run, bringing up a little message box that says Hello, world! (Depending on which solution you re using, you may need to right-click the DialogHelloWorld project and select Set As Start Project. In a solution, there can be many projects, but only one of them will be started when you choose Debug/Start.)

Now if you re like me and this is your first time with C# and Microsoft Visual Studio, these two simple things might not seem quite so simple. I m too embarrassed to take you through all the fumbling I did to find those simple steps, even with the book right in front of me. If you re more experienced now than I was then, I m sure you re thanking me for not going through that painful experience.

When we start from a blank project, we have to set up the references more on our own than if we used one of the built-in projects.

(The first time I tried this, I was using the limited Visual Studio that comes with C# at your local computer store, and it didn t offer the options that Petzold was using. That caused me to fumble around more than I would like to. If you re using that version of Visual Studio or working with a text editor and the command line, you re on your own for a bit here. What you see here is based on a full edition of Visual Studio.)

Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net