Writing Code with the Microsoft Visual Studio .NET 2003 IDE


Construction Cue

This book will assume that you're writing your code inside the Visual Studio .NET IDE (integrated development environment). Although this isn't a requirement for writing games using .NET, nor of using .NET itself, it is the IDE of choice for this book. You can see the Visual Studio .NET 2003 IDE in Figure 1.2.

Figure 1.2. The Visual Studio .NET IDE.



The IDE is the one-stop shop for everything you need to write .NET applications. It not only includes the editors you will need for writing your code, but a host of other features designed to make the development of .NET applications easy. It has designers to allow you to easily create rich content, such as Windows applications. It also has a built-in compiler and debugger, with everything integrated seamlessly. Throughout this book, it will be assumed that you are using the IDE to do your development.

The best way to familiarize yourself with the IDE is to use it to write a simple application. Classic computer programming tradition would have you write a simple "Hello World" application that really does nothing more than output that text to the screen. In all honesty, that application couldn't be any more boring, so instead you should try to write something slightly more in-depth. No need for anything fancy, because this is just an introduction to the IDE, but how about something with a little user interaction? Let's write an application that asks for the user's name and year of birth, and then outputs the current age.

The C# Code

Go ahead and start up the Visual Studio .NET 2003 IDE now. When it first starts up, it should default to the start page you saw in Figure 1.2. Click the New Project button on this page to start a new project. If this page isn't shown, you can also click the New, Project selection from the File menu or use the Ctrl+Shift+N keyboard shortcut. This brings up a New Project dialog, such as that in Figure 1.3.

Figure 1.3. The New Project dialog.


You should try the C# code first, so in the New Project dialog, select the Visual C# Projects item from the left-hand list box, and the Console Application item from the right-hand list box. Select any name and click the OK button to create the project. This creates a new console application that currently does nothing. Replace the code that's automatically generated for you with the code found in Listing 1.1.

Listing 1.1. A Simple C# Console Application
 using System; class ConsoleApp {  static void Main()  {  Console.Write("Hello World C#!\r\nPlease enter your name:");  string name = Console.ReadLine();  Console.Write("Hello {0}, please enter the year you were born: ", name);  int year = int.MaxValue;  while(year == int.MaxValue)  {  try  {  year = int.Parse(Console.ReadLine());  }  catch (FormatException)  {  Console.Write("You did not enter a valid number. ");  Console.WriteLine("Please enter an integer, such as 1975.");  }  }  Console.WriteLine("You must be approximately {0} years old!",     DateTime.Now.Year-year);  Console.WriteLine("Press the <Enter> key to exit the application");  Console.Read();  } } 

If you are already fluent in C, C++, or Java, the syntax of the C# language is probably quite similar to what you've been accustomed to. Although the underlying runtime of C# is still the CLR, the syntax is definitely derived from these languages, and developers familiar with them normally have no difficulty making the switch. As you can see here, the code isn't overly complex. It uses the Console class first to write out a simple message (including the ubiquitous "Hello World") before asking the user's name and year of birth. Notice that the application will continue to ask for a year of birth until a valid numeric value has been entered. It finally outputs the user's current age based on the simple formula. You can see the expected output of this application in Figure 1.4.

Figure 1.4. Your first application!


The VB .NET Code

If you are more familiar with Visual Basic syntax, you will likely still be able to follow the preceding code for the most part. The syntactical difference between the two languages isn't that great, but you should try to write a Visual Basic .NET version anyway. Once again, you will need to start a new project and follow the same instructions as you did with the C# code, with the natural exception being the project type you are creating. Instead of selecting Visual C# Projects from the left-hand list, you should choose Visual Basic Projects instead. After the project has been created, replace the automatically generated code with the code you find in Listing 1.2.

Listing 1.2. A Simple VB .NET Console Application
 Module Module1  Sub Main()  Console.Write("Hello World C#!" + vbCr + vbLf + "Please enter your name:")  Dim name As String = Console.ReadLine()  Console.Write("Hello {0}, please enter the year you were born: ", name)  Dim year As Integer = Integer.MaxValue  While year = Integer.MaxValue  Try  year = Integer.Parse(Console.ReadLine())  Catch fe As FormatException  Console.Write("You did not enter a valid number. ")  Console.WriteLine("Please enter an integer, such as 1975.")  End Try  End While  Console.WriteLine("You must be approximately {0} years old!",     DateTime.Now.Year - year)  Console.WriteLine("Press the <Enter> key to exit the application")  Console.Read()  End Sub 'Main End Module ' Module1 

As you can see, the translation of the code from C# to VB .NET isn't overly complicated. Because each language is running on top of the same runtime, there are only minor syntactical differences between the two pieces of code. You should also notice that each code produces the same results. This is a powerful feature of .NET because it frees the developer up to write code in whichever language he is most comfortable with. There's no longer the worry that if you use programming language X, you won't be able to take advantage of feature Y in programming language Z. Because each of the managed languages use the same underlying runtime, they all have similar feature sets.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

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