Chapter 2
IN THIS CHAPTER
Using the designer
Exploring Solution Explorer
Coding with Code View
Using the Tools menu
Working with the debugger
Integrated Development Environments, or IDEs, are the Swiss army knife of the programmer’s toolkit. IDEs provide a mechanism for storing program code, organizing and building it, and looking at finished products with design editors. IDEs make things happen and, in the bargain, cut hours from a task.
Visual Studio is impressive; it is massive. It would be tough to cover all Visual Studio features in a single book, much less a single chapter. In fact, you’re unlikely to ever use all the Visual Studio features.
Rather than try to cover everything, this chapter gives you the chance to experience only the features of Visual Studio that you’ll realistically use every day. Of course, continuing to explore the IDE and discovering new stuff is important — don’t just stop with the content of this chapter. It provides only a brief overview of some of the astonishing features in the tool. You can discover more about the IDE at https://docs.microsoft.com/visualstudio/get-started/visual-studio-ide
.
One thing that is integrated into an IDE is a way to edit files graphically, sometimes called a Graphic Development Environment or designer. Visual Studio allows you to graphically edit many different types of code bases and provides adjunct tools for the further management of said code bases. In short, the designer is the drag-and-drop element of the Visual Studio world. It isn't always the best way to develop a program, but it sure can help sometimes.
The problem is that because of the necessities of the software development world, different designers all work a little differently. A significant difference exists between HTML and XAML, for example, so the web designer and the WPF Designer don't look or act the same. Visual Studio gives you several visual designers to help develop applications, including these commonly used designers:
Microsoft is getting into the run-anywhere game late — very late, but it’s doing it with style. This type of application relies on the Windows Runtime (WinRT) to provide services across a multitude of devices that include your desktop, smartphone, Xbox, and other platforms. Book 5, Chapter 5 offers a good overview of this new method of creating Windows applications, but you should view this method more as an adjunct to what you have done in the past, rather than a complete replacement, because it’s simply not practical to throw out your entire existing code base for software that is less capable. Because UWP applications run everywhere, they can’t access the full potential of a desktop machine. It’s more like writing a 32-bit application with serious limits, rather than a robust 64-bit application that can take on heavy tasks such as machine learning and serious database management. Each application type has a place in your developer Toolbox.
Much of the setup for a UWP application is the same as for any other project you’ve created so far in the book. However, there are a few differences, such as the need to select a target version of Windows for your app and also select the minimum acceptable version of Windows. You also have to configure your system for Developer Mode, which is covered in Book 5, Chapter 5. The initial display that you see also differs from other application-creation processes in that it gives you a series of steps to follow, as shown in Figure 2-1.
The core of the user interface design experience is a language called Extensible Application Markup Language (XAML, pronounced zammel), which (unsurprisingly) is an XML-derived domain-specific markup language for the creation of user interfaces. In the designer, shown in Figure 2-2, the design is in the top frame and the underlying code is in the bottom frame.
In addition, you design the application using pages. Each page is designed to work with displays of various sizes and you see the pages listed in a Document Outline pane, as shown in Figure 2-2. The actual page size is controlled using the drop-down list above the graphical design area. The default is the 13.5” Surface Book (3000 x 2000) 200% Scale. However, you can also choose other devices, such as 5” Phone (1920 x 1080) 300% Scale. Each of these options helps you see how your design will look on a specific device.
There are a few small but significant features in the UWP Designer that should be pointed out. You can see them all in Figure 2-2.
On the left side of the bar at the bottom of the designer is the Zoom field. The options in this field let you do things like configure the designer to zoom to fit the current selection. This is invaluable for working on an application that is bigger than your screen, or for examining details.
Next to the Zoom field are a series of buttons that make working with the designer easier. Here are the buttons in order of appearance:
At the left side of the dividing line between the Design and XAML frames is a little double-arrow icon. Clicking this icon changes whatever is in the bottom frame to be in the top frame, and vice versa.
On the right side of the same dividing line are three buttons that determine the kind of split you have — a vertical split, a horizontal split (which is the default), or no split (so that the designer or Code Editor take the full space). Some people like the code. Some people like the designer. Some people prefer a mixture of the two. You determine how you want to use the tools.
Windows Presentation Foundation is covered in some depth in Book 5, but for now you should know that it is the current focus of Microsoft’s Windows desktop development experience. Yes, you still see a lot of Windows Forms development, but newer applications rely on either WPF or UWP. Book 5 talks all about WPF, so you can read more about it there. Figure 2-3 shows the WPF Designer, which has a striking similarity to the UWP Designer shown in Figure 2-2.
You can click the design in the designer and move things around, if you want, or use the designer to select things and edit the properties in the Properties panel (covered in the upcoming section “Paneling the Studio”). Additionally, if you change something in the code area, you’ll see the change in the design. It’s a good system. The following list contains some button differences between the WPF Designer and the UWP Designer:
The main difference between the Windows Forms Designer and the WPF Designer is the lack of a code panel in Windows Forms. Although there is code backing up Windows Forms, you don’t get to edit it directly. The Windows Forms Designer performs this task for you and places the code in a special designer file, such as Form1.Designer.cs
for a default project.
The topic of Windows Forms isn’t covered in any detail in this book (there are a few projects, such as the network status example in Book 3, Chapter 4 and the image example in Book 3, Chapter 5). Even though it’s still an active development platform, most developers are moving to WPF instead, so it pays to spend more time working with an environment that developers use for new projects. WPF performs the same programming duties as Windows Forms but is the newer technology. The Windows Forms Designer is shown in Figure 2-4.
Data View is usually used with the Server Explorer and is part of an in-studio representation of SQL Management Studio. You can view and edit data tables in SQL Server (and other) database management systems right inside Visual Studio. An example is shown in Figure 2-5; in this case, you see the Show Table Data view of the Address (Person) table of the AdventureWorks2012 database.
There is a remarkable amount of power here, and there just isn’t enough space to cover it all. Again, this isn’t a database book, so you may want to read the MSDN entries for the Data Connections feature of Server Explorer for more details.
To be as flexible as it needs to, Visual Studio has a large collection of modular windows that are called panels (or sometimes panes, depending on whom you talk with). These panels do everything that isn't involved with directly editing a design or code. They manage files, manage objects, show feedback, show properties, do all sorts of stuff.
Visual Studio has more than 30 panels. You normally access them using either the View menu, such as View⇒ Solution Explorer to see the Solution Explorer pane, or the Debug/Windows menu, such as Debug⇒ Windows⇒ Breakpoints to see the Breakpoints pane. There isn’t room to discuss them all here, so the chapter covers only the five you use most often.
Solution Explorer (see Figure 2-6) manages solutions, projects, and files. Even though solutions consist mostly of files and folders, managing a solution is a somewhat more complex operation than it seems at first blush.
For solutions themselves, Solution Explorer provides a number of important tools (accessible by right clicking the solution entry), including the following:
Projects are the individual compiled units and are divided by type. You can find more about projects in Chapter 1 of this minibook. Solution Explorer brings to projects the capability to add and remove files, make references and services, set up a class diagram, open the representative Windows Explorer folder, and set properties. All this information is saved in the Project file. The Project file is just an XML file. There are a few key characteristics of the file:
You likely won't modify your Project file, but it's important that you know it can be done, and that Microsoft has inline comments. They expect the file to get changed.
Files are a lot less exciting. They are pretty much exactly what you expect. They host the source code of the program being developed. Solution Explorer manages the files in the project basically the way Windows Explorer does. Solution Explorer lists the files and folders and allows them to be opened in the designer or the Code Editor.
The Properties panel (see Figure 2-7) is a simple, flexible tool that allows you to update those endless lists of details about everything in development projects. The panel is basically a window with a two-column-wide data grid. It reads properties in key/value pairs and allows for easy view and edit.
You can find details about everything in your application here. If you click nearly anything in Visual Studio and press F4 to bring up the Properties panel (refer to Figure 2-7), you will get properties. Try it with these fun selections:
If there is any metadata about something, the properties are in the Properties panel. It’s a great tool.
One of the great misunderstood tools is the Toolbox (see Figure 2-8). On the surface, the Toolbox seems so simple. The design-time controls for a given file type are available to drag and drop. Still, there is one important thing you need to remember about the Toolbox: It displays only controls that are appropriate to the file in focus. So if you’re running a Windows Form in the designer, you won’t see a database table available to drop. Keep in mind that the Toolbox is context sensitive. It works only when it has a file to work on.
There is one other interesting property of the Toolbox: It can be used to store text clippings, which can be useful for demonstrations and presentations. It is also handy for storing often-used pieces of code. To do so, follow these steps:
Open a code file.
Anything will do, .cs
file, .xaml
file, whatever.
Make sure the Toolbox is open and then drag the selected code to the General section of the TextBox.
The copied code becomes a tool.
Drag the new tool into the code.
The copied code now appears in the code file.
Managed services include services such as the Event Logs and MSMQ, which you need to look at to test parts of your application. Internet Information Services, for example, is a managed service that shows up in the list. To get a server into Server Explorer, follow these steps:
Play around with the services you see. There are a lot of features in this panel that this book doesn't cover.
Above the Services in Figure 2-9 are the data connections. These are data connections that have been made on previous projects, which Visual Studio keeps around in case you need them for any other projects. Although keeping these connections around seems like a bit of a security risk, it sure as heck is convenient.
The goal is to reduce the dependency on SQL Management Console (the old method for managing the database for developers), and it does a darn good job. The key issue is access to the data model (table names and columns) and stored procedures; developing a program without access to the database is tough. In a new connection, these database objects are given default folders:
The key thing you should try is to open a Stored Procedure (you can do this by double-clicking it in the Data Sources panel — which is available in a solution that has data connections). When you do so, you can easily edit SQL code, with indenting and colorization, right in Visual Studio. Use this. It’s really neat.
The last of the five main panels is Class View. As discussed in Books 1 and 2, everything in the .NET Framework is an object. The classes that make up the framework — all derivatives of Object
— are viewable in a tree view. The Class View is the home for that tree view (see Figure 2-10).
From the Class View, you can look at all the members of a class and access the inline documentation, which gives you an overview of the framework and custom classes that make up your project.
There is a lot to the Code Editor in Visual Studio. This is where you spend most of your time. Code Editor has two primary tools:
The Code Editor is where you edit code. It doesn’t matter what type of code; all of it is edited here. You use the Code Editor to create class libraries, and it provides a smart interface for ensuring your class libraries have a good chance of working. If you are in XML, it works like an XML editor (such as the lower half of the view in Figure 2-2, shown previously).
You can get to a code file in the Code Editor a few ways. The most common way is to double-click on a source code file in Solution Explorer, and it will open in Code Editor.
If you are viewing something in the designer, you can get to the code-behind related to the file in question by any of three methods:
You’ll find yourself using all three over time. Note that you can get directly to the code-behind files by clicking the little triangle next to a designer file and then double-clicking the code files nested within. (This feature works with auto-generated files, but won’t open the code-behind associated with the auto-generated file.)
IntelliSense (sometimes also called AutoComplete, IntelliCode, or other names just to confuse you) is Microsoft’s autocompletion feature, and it’s a prominent part of the Code Editor. You find IntelliSense whether you want to or not. In Code View, click inside a class and press Ctrl+spacebar. Everything you are allowed to type there shows up in a big list.
The nice thing is, IntelliSense is context-sensitive. Type Console and press the dot (.
). All available members of the Console
class appear. IntelliSense keeps you honest and prevents having to remember the two-million-odd members of the .NET Framework on a day-by-day basis.
IntelliSense helps with method signatures, too. Continue the line you started earlier by adding WriteLine
— in other words, type (Console.WriteLine) — and then check out the IntelliSense. It will tell you all the overloads for the member in question. Use the up and down arrows to move between these members. It's slick.
Visual Studio will auto-outline your code for you. Notice the little box with a minus sign (–) next to every namespace, class, and method in the Code Editor. Those are close-up sections of the code for readability. It might not seem like much now, but when you have 2,200 lines of code in there, you will be glad.
You can create your own outlining, too. Preceding a section that you want to outline, type #region on a new line. At the end of the section, type #endregion. This newly defined section — regardless of whether it’s at an existing outline point — will get a new outline mark. If a comment is added after a region
statement, it will show in the collapsed outline.
A number of windows affect text input and output in order to solve certain problems. As a group, they don't really have a name, so some developers call them auxiliary windows. Here are a few of them:
Build logging: Every time you build, the Output window tracks all the linking and compiling that go on under the sheets and shows any errors that might come up.
You can use errors listed in the Output window to navigate the code. The buttons in the Output box assist with getting around the code based on the messages in the window.
Debug.Write()
statement in your code, the Output window is where it will go. Additionally, if you use Console.Write()
, but are running a Windows Forms application, for instance, the text will go to the Output window.The Immediate window: This window does exactly what one would expect it to do: take action immediately. In debug mode, you can use the Immediate window to send commands to the program as it is running, to change the state, or evaluate operations. Try the following to see how the Immediate window works:
Put a breakpoint somewhere in the code by clicking in the gray bar running down the side of Code View.
A red dot appears.
Choose Debug ⇒ Start Debugging or press F5.
The program should stop at the spot you selected.
Select the Immediate window.
If you don't see the Immediate window displayed by default, choose Debug ⇒ Windows ⇒ Immediate or press Ctrl+D,I.
Type ? this.
See the IntelliSense menu?
Type a . (dot).
You see a listing of all the things you can do with the this
object (which may be nothing if you’re not in a class) at the particular point you’re at in the code. The ?
command means to print whatever the object contains.
That's what the Immediate window is for. You can do more than print values, too. You can change variable values and modify the execution of your program. It is a powerful part of your debugging Toolbox.
The Breakpoints window: If you’re still running the last example, you can display the Breakpoints window by pressing Ctrl+D,B. The Breakpoints window appears, and any breakpoints you add will be in it (see Figure 2-11).
What’s cool here is that you can right-click any breakpoint and completely alter the properties of that breakpoint by choosing Settings from the context menu. Try it — the important options are described in this list:
MachineName
, ProcessName
, ProcessId
, ThreadName
, and ThreadId
. It's useful for multiprocessor development, among other things.//TODO:
. Adding a TODO comment makes it appear as a task in the Task List window (found on the View menu). Double-clicking the task in the Task List takes you to the line where the task was set, so you don’t have to search for it.Any overview chapter always has topics that just don’t fit in any category. This chapter presents a double handful of tools that you want to know about.
The Tools menu provides you with access to interesting tools that you can use for a variety of purposes. The following list doesn’t include every Tools menu entry, but it does contain those commonly found in the Community edition of Visual Studio.
https://docs.microsoft.com/en-us/visualstudio/ide/code-snippets
for additional details. Users
<User Name>
AppDataLocalMicrosoftVisualStudio
<Version Number>
Settings
directory or change current settings to another batch of settings. You can share settings with other developers.Previous chapters of this book discuss the basics of building a project. A few other options in the Build menu deserve at least a small explanation, including the following:
You might be surprised to discover how many professional developers learn new techniques, which is by looking at other people’s code. It might seem like cheating, but it’s actually the smart way to do things because someone else has already done all the hard work. Of course, trying to figure out how a particularly subtle programming technique works could consume days if you just look at the code. It’s much easier to determine how the code works when seeing it in action, which is precisely why the debugger is an aid to learning. By running the code and stepping through it a bit at a time, you not only discover new programming techniques but also develop a better sense of precisely how C# works.
The example in this section relies on the cribbage application you first discover in Book 3, Chapter 5. However, the techniques work with any code, and you shouldn’t feel the need to work through any particular example. The following sections fill in the details for you.
The first step in working with the debugger as an aid to learning is to set one or more breakpoints in the code. For example, you might want to see how the CribbageBoard_
method works for the Cribbage application. To perform this task, you simply click the gray stripe that runs along the left side of the Code Editor. The breakpoint appears with a red dot in the left margin and the piece of code highlighted (normally in red as well).Paint()
The next step is to start the debugger by pressing F5 or choosing Debug ⇒ Start Debugging. You stop at the first breakpoint that the application encounters, which may not be the first breakpoint you set or the first breakpoint in the code. In fact, it’s often revealing to see where you do end up because the location of a breakpoint tells you how the application starts, including what tasks it must perform and in what order it performs them.
Stepping through the code comes next. You see where the instruction pointer moves as the application executes code. You have three options for stepping through code:
When the application is running, you may not want to single step through every line of code. Yes, you want to stop at a certain breakpoint, but then you decide that you don’t really want to stay there and execute the code line by line. In this case, you can hover the mouse next to the line of code that you do want to stop at next. You see a green right-pointing arrow like the one shown in Figure 2-12.
When you click the right-pointing arrow, the code from the existing instruction pointer location to the location you point to will execute. You see the results of the execution, but not the individual steps. In some cases, this technique will help you get past problematic pieces of code where timing prevents you from truly seeing how the code executes.
Understanding how data changes over time is an essential part of learning with the debugger. You have access to a number of windows to help you do this. For example, you can include Debug
and Trace
statements in your code and then view their output in the Output window. To include Debug
and Trace
statements, you must add using System.Diagnostics;
to the top of your application code. Use Debug
statements when you want to output values only for debug releases of your application. Use Trace
statements when you want to output values for both debug and release versions of your application.
Whenever you execute an application, the Locals window shown in Figure 2-13 contains a list of the local (rather than the global) variables. The local variables are those that are set in the current method and are likely the variables you use most often in well-designed code.
The right-pointing arrow next to a variable entry tells you that you can drill down to find more information. In fact, in some situations, you can drill down quite a few levels before you finally reach the end. Some objects are really complex. Viewing the content of objects helps you understand them better and often provides insights into how to better use them to create robust applications.
If a particular variable doesn't appear in the Locals window or you want to view a particular object element in more detail as the code executes, you can use the Watch window, shown in Figure 2-14, instead. You type the variable you want to watch in the window. As the code executes, you see the value change. The Watch window also allows entry of expressions such as radius * 2
or employee == null
.
Learning to right-click in Visual Studio is a valuable aid in learning. Whenever you see anything that looks interesting, right-click it. The context menu tells you the sorts of things you can do with the object. For example, you might right-click a class and choose one of the detail view entries for it in the context menu. The internal view options you use most are