Getting Started with Geoprocessing and ArcObjects in .NET Continued from page 37 User Environmental Variable on your user PATH to the .NET 2.xx directory. This will allow you to run the C# command line compiler from any directory with ease. Right-click My Computer, click the Advanced tab, click Environment Variables, find the PATH variable in the list of User variables, click edit, and add ;C:\WINDOWS\ Microsoft.NET\Framework\v2.x.xxx to the end of the existing PATH variables list. Make sure you get the version correct, and don’t forget the semicolon in the front to separate it from the previous environmental variable in the PATH list. Hello World! The .NET Framework supports both the Visual Basic and C# languages. For the purposes of this article, all code listings will be in C#. There are a number of ways to write and compile .NET applications. Writing your code in Notepad and compiling using the C# command line compiler is by far the simplest way of constructing a .NET application. Start Notepad (or your plain text editor of choice) and type the code from Listing 1. This is a simple C# program that prints the classic “Hello world!” to standard output at the command prompt. Save this file as HelloWorld.cs, where .cs is the file extension for a C# file. In this example, I’m working in the directory I created at C:\testing\ csharp. Before we can run our code, we must compile it into an executable file. To compile this example, we will use the C# command line compiler. Assuming you have your PATH environmental variable set up, open a command prompt window (Start > Run > type cmd), and compile your code, as in Listing 2. Typing csc calls the C# compiler, the /out: flag tells the compiler where to put the output executable file, and the final argument we pass in is our input C# file. Now, at the command prompt, navigate to your executable’s directory and run it (as shown in Listing 3). Congratulations, you just wrote, compiled, and ran your first .NET program! Not so hard, eh? Stepping Things Up a Notch The next section will get an Integrated Development Environment (IDE) up and running. With the classic “Hello world!” under our belts, let’s get down to business. Launch Visual Studio/Visual C# 2008 Express (which I will hereafter collectively refer to as just Visual Studio), go to File > New Project, and select Console Application. Give your project an appropriate name, click OK, and Visual Studio creates a new solution for you that includes references to assemblies we will be using and a C# file named Program.cs by default. Before we can use any geoprocessing tools (or any ArcObjects components) in our program, we must add references to the proper Esri assemblies to expose those functionalities. If the Solution Explorer pane is not visible in Visual Studio, go to the View menu and select Solution Explorer. In the Solution Explorer, expand your projects tree and expand the References tree. You should see references to several System assemblies that Visual Studio sets automatically for you when it builds your solution. Right-click References and select Add Reference. The Add Reference window may take a few seconds to come up, and when it does, select the .NET tab, scroll down through it and examine the assemblies that are available to you, especially those that begin with Esri, as those are all the Esri assemblies that contain the geoprocessing and ArcObjects functionalities that we want to expose for our use in Visual Studio. Select Esri.ArcGIS.Geoprocessor from the list and click OK. Notice that the assembly has been added to the References in your project. Now let’s focus on Program.cs and writing some code. Writing Geoprocessing Code To access the Geoprocessor object’s methods and properties, we still need to do one more thing: add a using directive to our code. The using directive has two uses: to permit the use of types and create an alias for a namespace or a type. Every variable and constant has a type, as does every expression that evaluates to a value in a namespace (i.e., a collection of different classes), so you don’t have to qualify the entire namespace. This will soon make more sense. In Program.cs (which is where we will be typing from here on), on the line below the current last existing using directive, type the following: using Esri.ArcGIS.Geoprocessor; Add the following line inside your Main function, and instantiate (create an instance of) a Geoprocessor object: static void Main(string[] args) { Geoprocessor gp = new Geoprocessor(); } Note that as you started typing “geoprocessor”, Intellisense kicked in and took you right to the Geoprocessor class in the drop-down list of items available to you. Now, comment out using Esri.ArcGIS.Geoprocessor;. Do you notice that Geoprocessor loses 38 ArcUser Spring 2010 www.esri.com