Visual studio 2010 part 6 doc

8 151 0
Visual studio 2010 part 6 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

42 Microsoft Visual Studio 2010: A Beginner’s Guide which you’ll see many times throughout this book. The Console application defined the skeleton code class to have the name Program. In reality you can name the class anything you want. Whatever names you choose should make sense for the purpose of the class. For example, it makes sense for a class that works with customers to be named Customer and only contain methods that help you work with customers. You wouldn’t add methods for working directly with invoices, products, or anything other than customers because that would make the code in your Customer class confusing. Classes are organized with namespaces, which are discussed next. The FirstProgram Namespace A namespace helps make your class names unique and therefore unambiguous. They are like adding a middle name and surname to your first name, which makes your whole name more unique. A namespace name, however, precedes the class name, whereas your middle name and surname follow your first or given name. A namespace also helps you organize code and helps you find things in other programmers’ code. This organization helps to build libraries of code where programmers have a better chance to find what they need. The .NET platform has a huge class library that is organized into namespaces and assemblies; this will become clearer the more you program. The main .NET namespace is System, which has multiple sub-namespaces. For example, guess where you can find .NET classes for working with data? Look in System.Data. Another quick test: Where are .NET classes for working with networking protocols like TCP/IP, FTP, or HTTP? Try System.Net. Another benefit of namespaces is to differentiate between classes that have the same name in different libraries. For example, what if you bought a third-party library that has a Customer class? Think about what you would do to tell the difference between Customer classes. The solution is namespaces, because if each Customer has its own namespace, you can write code that specifies each Customer by its namespace. Always using namespaces is widely considered to be a best practice. The Program class in Listing 2-1 belongs to the FirstProgram namespace, repeated here for your convenience (in C#): namespace FirstProgram { // Program class omitted for brevity } You can put many classes inside of a namespace, where inside means within the beginning and ending braces for a namespace. Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax 43 The using directives at the top of the C# part of Listing 2-1 are really a shortcut that makes it easier for you to write code. For example, the System namespace contains the Console class. If the using System directive were not present, you would be required to write System.Console.WriteLine instead of just Console.WriteLine. This was a short example, but using directives can help clean up your code and make it more readable. A VB module must be declared at the global level, meaning that it can’t be added to a namespace that you create. The following example demonstrates what a VB namespace looks like: Namespace FirstProgram Public Class Customer End Class End Namespace In this example, you can see that the FirstProgram namespace contains a Customer class. The next task you’ll want to take on is writing code, but before doing so, let’s look at some of the features of the VS Code editor. An Overview of the VS Code Editor The VS Code editor is where you’ll be performing much of your coding work. This section will point out a few features you will be interested in and show you how to perform customizations. Figure 2-3 shows the editor with the Console application skeleton code from the C# part of Listing 2-1. Figure 2-3 The VS Code editor 44 Microsoft Visual Studio 2010: A Beginner’s Guide The following sections examine various elements of the Code editor, starting with class and member locators. Class and Member Locators The two drop-down lists, class locator and member locator, at the top of the editor are for navigating the code. If you have multiple classes in your file, you can use the class locator drop-down list on the left to select the class you want to find, and the editor will move you to the first line of that class declaration. In practice, I only put a single class within a single file, so the class locator doesn’t get much attention. However, you will have VS wizards that automatically generate code and put many classes in the same file, and the class locator is very useful if you want to find a particular class and learn about what the automatically generated code is doing. The member locator drop-down list on the top right contains a list of methods and other members for the class selected in the class locator. The only class member we’ve discussed so far is the method, but there are more, as you’ll learn in upcoming chapters. Selecting a member causes the editor to move you to the first line of that class member. Whenever you find yourself scrolling through a large file, remember that the member locator will help you find what you’re looking for quickly. The vertical bar on the left side of the editor is called the indicator margin, where you’ll see icons for features such as bookmarks and debug breakpoints. The next section discusses bookmarks. Bookmarks Figure 2-3 shows a bookmark on the line for the program class. Bookmarks allow you to navigate code quickly without manual navigation when working with multiple documents or multiple locations within the same document. Table 2-1 shows a list of keyboard commands for bookmarks. Table 2-1 Bookmark Shortcut Keys Key Code Meaning CTRL-B, T Toggle a bookmark on or off CTRL-B, N Move to next bookmark CTRL-B, P Move to previous bookmark CTRL-B, C Clear all bookmarks CTRL-W, B Open the Bookmarks window Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax 45 One of the entries in Table 2-1, CTRL-W, B opens the Bookmarks window shown in Figure 2-4, allowing you to manage bookmarks throughout your application. The bookmark has a toolbar, which is the same toolbar that appears in VS when the editor window is active. The actions on the toolbar include the items from Table 2-1, plus the ability to move between folders. Within the Bookmark list, you can check to make a bookmark active or inactive. When the bookmark is inactive, previous and next navigation will not stop at the bookmark. You can change the name of the bookmark by clicking the name twice. The File Location and Line Number tell you where the bookmark is located. Setting Editor Options The editor is very configurable, and there are more options available than many people realize. You can view available options by selecting Tools | Options to show the Options window in Figure 2-5. As you can see from the figure, selecting Environment | Fonts And Colors allows you to change the appearance of VS. Regarding our current discussion of the editor, this is where you can customize the coloration of code elements that appear in the editor. TIP If you want to share your custom editor settings, you can use the Import and Export Settings Wizard that you learned about in Chapter 1. There is also an Import And Export Settings branch right below Fonts And Colors in the Options window. Most editor customizations are in a language-specific branch of the Options window. Figure 2-6 shows the options available for C# programmers. Figure 2-4 The Bookmarks window 46 Microsoft Visual Studio 2010: A Beginner’s Guide Figure 2-5 The Options window Figure 2-6 C# code editor options Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax 47 The Options window in Figure 2-6 is opened to Text Editor, C#, Formatting New Lines. As you can see, there are very detailed settings for even how the editor automatically formats new lines and where braces appear. If the code doesn’t format the way you want it to, visit this page to set the options to what you please. Saving Time with Snippets Snippets are important to learn because they will save you time. A snippet is a set of keystrokes that form a template for a piece of code. The code for a snippet is typically something that is common in normal programming. You’ll see many common statements and blocks of code in this chapter, many of which have associated snippets. This section shows you the mechanics of using snippets, and you’ll see more examples throughout the rest of this chapter. To use a snippet, begin typing the snippet prefix until the snippet acronym appears in the Intellisense completion list, press the TAB key twice, and fill in the snippet form while tabbing through each field. Press ENTER when you’re done. Since you’ve already learned about namespaces, I’ll show you the namespace snippet. To start, open any code file and click to start typing in a part of the file outside of all code blocks, such as directly below any using statements but above any existing namespace statements. Type the letter n and watch the completion list go straight to the namespace element. Type an a and you’ll see the namespace alone in the completion list, as shown in Figure 2-7. NOTE The CTRL-ALT-SPACE keystroke in Figure 2-7 switches between the Intellisense modes Consume First and Standard mode. In Standard mode, which shows CTRL-ALT-SPACE, typing characters automatically selects keywords. However, there are situations where you are trying to type a word that doesn’t exist yet and Intellisense is too aggressive by adding the selected completion list item, instead of what you typed. In those cases, you can press the CTRL-ALT-SPACE keys to go to Consume First mode and what you’ve typed will be selected. You can still use the DOWN ARROW key on your keyboard in Consume First mode to select the highlighted term in the completion list. Figure 2-7 Using snippets 48 Microsoft Visual Studio 2010: A Beginner’s Guide You can identify snippets in the completion list by the torn paper icon. At this point, you can press the TAB key to complete the namespace keyword. Then press TAB again to produce a template where you can fill out the highlighted fields. Figure 2-8 shows the results of creating a namespace snippet by typing n and pressing TAB, TAB. As shown in Figure 2-8, you would type in the Namespace name in the highlighted form field to replace MyNamespace, which is placeholder text. For templates with more fields, you would press the TAB key to move between fields. In the case of the namespace shown in Figure 2-8, there is only one field in the template to complete. VB offers a couple of ways to add snippets: by typing prefixes or via a pick list. To see how VB snippets work, place your carat inside of the Module1 module, underneath End Main (not inside of the Main block). Type Su and press TAB, and notice that VS creates a Sub (method) along with a template containing a field for filling out the Sub snippet. Another way to add VB snippets is to type a ? and press TAB. You’ll receive a pick list, as shown in Figure 2-9. You can navigate this pick list to find the snippet you need, as classified in one of the folders. VB ships with many more built-in snippets than for C#. Now that you know how to use snippets, let’s move on to the different types of statements you can have in C# and VB and how snippets work with those statements. Figure 2-9 VB snippet pick list Figure 2-8 Filling in the Snippet template Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax 49 Coding Expressions and Statements There are various types of statements you can write with both C# and VB, including assignment, method invocations, branching, and loops. We’ll start off by looking at primitive types, such as integers and strings, and then I’ll show how to build expressions and set values by performing assignments. Then you’ll learn about branching statements, such as if and switch in C# or the case statement in VB. Finally, you’ll learn about various loops, such as for and while. I describe these language features in general terms because they differ between C# and VB, but you’ll learn that the concepts are essentially the same. Before writing any code, you should know how Intellisense works; it is an important productivity tool that reduces keystrokes for common coding scenarios. Making Intellisense Work for You Previously, you saw how snippets work. Snippets use Intellisense to show a completion list. Intellisense is integrated into the VS editor, allowing you to complete statements with a minimum number of keystrokes. The following walkthrough shows you how to use Intellisense, as we add the following line to the Main method. Don’t type anything yet; just follow along to see how Intellisense works: C#: Console.WriteLine("Hello from Visual Studio 2010!"); VB: Console.WriteLine("Hello from Visual Studio 2010!") The following steps show you how VS helps you save keystrokes: 1. Inside the braces of the Main method, type c and notice how the Intellisense window appears, with a list of all available identifiers that start with c. This list is called a completion list. 2. Type o and notice that the completion list filters all but those identifiers that begin with co. 3. Type n and you’ll see that the only identifier available is Console. This is what we want, and you only needed to type three characters to get there. 4. At this point most people press the ENTER or TAB key to let VS finish typing Console, but that is effectively a waste of a keystroke. . Figure 2 -6 shows the options available for C# programmers. Figure 2-4 The Bookmarks window 46 Microsoft Visual Studio 2010: A Beginner’s Guide Figure 2-5 The Options window Figure 2 -6 C# code. Intellisense works: C#: Console.WriteLine("Hello from Visual Studio 2010! "); VB: Console.WriteLine("Hello from Visual Studio 2010! ") The following steps show you how VS helps. with the Console application skeleton code from the C# part of Listing 2-1. Figure 2-3 The VS Code editor 44 Microsoft Visual Studio 2010: A Beginner’s Guide The following sections examine

Ngày đăng: 04/07/2014, 03:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan