Visual Studio 2010 Essential Training

Visual Studio 2010 Essential Training

with Walt Ritscher

 


In Visual Studio 2010 Essential Training, author Walt Ritscher demonstrates how to use Visual Studio 2010 Professional to develop full-featured applications targeting a variety of platforms. Starting with an overview of the integrated developer environment, the course covers working with code editors, navigating and formatting code, and deploying applications. Also included are tutorials on running performance and load tests, and debugging code. Exercise files accompany the course.
Topics include:
  • Creating a Visual Studio project
  • Building the user interface
  • Binding to an RSS feed
  • Coding with IntelliSense
  • Creating rich Internet applications with Silverlight
  • Building Windows applications with Windows Forms
  • Integrating with SQL Server
  • Working with Microsoft Office applications
  • Understanding extensibility in Visual Studio
  • Working with data, ADO.NET and datasets
  • Using source control

show more

author
Walt Ritscher
subject
Developer
software
ASP.NET , Silverlight , Visual Studio 2010
level
Intermediate
duration
8h 9m
released
Nov 16, 2010

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:03Hi! I'm Walt Ritscher, and I want to welcome you to my Visual Studio 2010
00:07Essential Training series.
00:09If you write code for a Microsoft platform, like Silverlight, Windows Phone, or
00:14ASP.net, you should be using Visual Studio.
00:17In this course, I'll show you every corner of this powerful tool.
00:20I'll start by looking at the code editors and how to customize them to make
00:24you more productive.
00:25You will see how to break point your code for debugging purposes and explore the
00:29Watch, Threads, and other status windows.
00:32You'll see the Data Source window and discover the in-depth database and
00:36server tools available.
00:37There is a rich testing environment included in Visual Studio, and I will cover
00:41both unit and performance tests in this title.
00:43There is also a section on working with source control and another on
00:46compiling and deploying your finished application.
00:49As a professional programmer, I rely on remarkable tools available in Visual
00:53Studio to build my applications, and I'm eager to show you how they work.
00:57So, let's get started exploring Visual Studio 2010.
Collapse this transcript
Using the exercise files
00:00If you are premium member of lynda.com, or if you are watching this tutorial on
00:04a DVD-ROM, you have access to the exercise files used throughout this title.
00:09I have copied the files to my Desktop. Let's take a look.
00:12Inside this Exercise Files folder, you will see there is a folder for most chapters.
00:17Inside the chapter, there is a folder for most of the movies.
00:20Let's take a look at Chapter 03, Movie 02.
00:24Inside this folder, are two folders:
00:27InfoReaders is the starter project, and InfoReader-Done is the finished
00:32version of the project.
00:33I am going to double-click on InfoReader.
00:36Inside here is an SNL file.
00:38That's the typical way you start up a project in Visual Studio.
00:42Occasionally, the SLN file will be in one of the subfolders, next to the
00:47CSPROJ or VBPROJ file.
00:49If you are a monthly subscriber or annual subscriber to lynda.com, you don't
00:54have access to the exercise files, but you can follow along with your own
00:58projects and assets.
00:59Let's get started.
Collapse this transcript
1. Getting Started
Understanding the Visual Studio versions
00:00Microsoft produces a number of versions of their popular Visual Studio product.
00:04In this movie, I want to spend a couple of minutes talking about the different
00:07versions that are available. Visual Studio also comes in a wide price range.
00:11You might be surprised to learn that there are free versions available for you to use.
00:15On the other hand, you can get out your corporate credit card and spend some
00:18serious cash for the Ultimate edition, which costs up to $12,000.
00:23There are a number of price points in between these two extremes.
00:26Microsoft caters to every niche.
00:28For this title, I will cover the features that are available in Visual
00:31Studio Premium Edition.
00:34And I will sneak in a preview, a really cool debugging tool in the Ultimate edition.
00:39To start your journey with Visual Studio, go to their download sites listed here.
00:43There is also a link to a product comparison page.
00:46Can you imagine the fun you can have reading about the differences between the versions?
00:50The Professional and Premium versions offer all the core features.
00:54Either one will keep you happy while coding.
00:56Premium also offers automated UI testing tools, special database projects, and
01:01code analysis tools.
01:03If you have got the money, then the Ultimate edition is your baby.
01:07It contains every conceivable tool you will ever want to utilize as a developer.
01:12It also has the price tag to match.
01:14The Test Professional version is targeted at your QA team.
01:19It contains test planning and test tracking workflow tools.
01:22It also includes manual testing tools, and works with the new IntelliTrace
01:26features, which I will cover in the debugging chapter.
01:28Team Foundation Server is the collaboration backend.
01:31This server enables your team to integrate all of the individually deployed
01:35Visual Studio tools.
01:36TFS contains centralized databases for storing project requirements, work items,
01:42it can track bugs, and has a source code repository.
01:46It has automated build tools available, and can be configured to have web
01:50portals for each project.
01:52A new tool in the Visual Studio Toolbox is the Team Explorer Everywhere.
01:56This tool exists to collaborate with Java developers.
02:00I copied a portion of the comparison matrix on the Microsoft web site to this slide.
02:05Take a look at the Debugging and Diagnostics section here.
02:08This shows that if you buy the Professional edition, you have the basic debugger.
02:12But if you fork out the money for the Premium or the Ultimate edition, you also
02:16get these additional tools.
02:18When you purchase a version of Visual Studio, you also get an MSDN
02:22subscription level.
02:23Having MSDN is cool, because it gives access to the software download library. The more you pay,
02:29the more software licenses you get.
02:31For example, in the Ultimate edition, you get business software.
02:35You can download Office 2010 or Project 2010.
02:40A couple of months ago, I was working on a project that required a legacy copy
02:44of Access 2003, and there it was in the library.
02:47If you need Design tools, look for Expression Blend or Expression Design.
02:51In the Developer section, there are copies of Visual Studio going back ten
02:55years, plus other tools like FoxPro Classic and even 1990s era versions of
03:00Visual Basic, and to sweeten the deal, you get access to all the standard Windows
03:04operating system and Server products too.
03:07Microsoft is also interested in bringing the power of .NET to the student and
03:11hobbyist programmer, so they created the Express editions of Visual Studio.
03:15Go to the Express web site to learn more about these free, yet powerful tools.
03:20Yes, low cost all means that they are lacking features that you would find
03:23in the paid versions.
03:24Here is a listing of the free editions.
03:27There is one for each of the major languages,
03:29for example, C#, Visual Basic, and C++.
03:33There are also versions for specific types of development,
03:36for example, the XNA Game edition.
03:40There are so many choices.
03:41I am certain you will find one that fits your needs.
03:43My personal favorite is Visual Studio Ultimate, but that's also an easy choice
03:47for me because it's included in my MSDN subscription.
Collapse this transcript
Setting up your developer computer
00:00This movie discusses what it takes to get your computer ready to use Visual Studio.
00:05There are a number of versions of Visual Studio available for you to download.
00:08The first movie in this chapter provides more details about these editions.
00:12Regardless of which one you choose, you need to download the installer to
00:15your local computer.
00:17Like most .NET applications, you can install Visual Studio 2010 on the same
00:21computer as older versions--for example, Visual Studio 2008--and they will work side by side.
00:27It's not really necessary for you to have Visual Studio 2008 on your computer
00:31however, because Visual Studio 2010 supports the same project types.
00:36For certain types of development, there are additional tools that you must install.
00:39They are not part of the core install, because they shipped at a later time.
00:43Here is a couple of additional tools you might consider installing.
00:47The first one, Silverlight 4 Tools, configures your version of Visual Studio to
00:51create the newest version of Silverlight.
00:53The second one is called Silverlight toolkit for Silverlight 4.
00:56This contains additional controls and tools for Silverlight development.
01:00An exciting twist in the mobile phone wars is Microsoft's new Windows Phone 7.
01:05To write phone apps, you will have to install the phone developer tools.
01:08This supplement to Visual Studio provides the tools you need to create
01:11Silverlight and XNA phone applications.
01:13It even includes a software emulator of the phone operating system, so that you
01:17can test the software without having the physical phone.
01:20Since it takes about an hour to install Visual Studio, I won't bore you with the
01:23actual installing of this software on my computer;
01:26instead, let me walk you through some of the screenshots of the
01:29installation process.
01:30This is the initial screen you will see when you start the installer.
01:33Click the Install Microsoft Visual Studio 2010 link at the top.
01:37Next, it will show you the Setup screen asking you if you want to participate in
01:41the Improve Setup experience.
01:43I have chosen to click the Yes check box here.
01:46On this screen, I have to accept the licensing terms.
01:50It also tells me which items I will be installing on the left column.
01:54Next, like most installers, it tells you the disk space necessary.
01:59There are two basic installs: Full and Custom.
02:01I am choosing the Full install. And as you can see that the bottom, it says I am
02:05going to need 5.9 GB of space on my hard drive.
02:09You can pick Custom, and then you can disable installing certain components or
02:14adding non-standard components.
02:16For instance, some of the C++ tools may not be installed by default.
02:21Now the installation starts.
02:22You can see a list of all the steps that it is going to perform, and at the
02:25bottom of the screen is a progress bar showing you where you are in this current step.
02:30And finally, I get the success screen, which says I have installed the
02:33application successfully.
02:35At this point, I would suggest clicking on the Install Documentation button, so
02:39that you get the appropriate help files installed as well.
02:42Then this screen asks me where I want to install my help.
02:44I think this is pretty self-explanatory.
02:47Here, I can pick which help files I want to install by clicking on the add links.
02:51Once you have finished the install, you can run Visual Studio.
02:54The first time you run, you will be greeted with this screen.
02:57Your decision in this dialog affects the positions of the tool windows and the
03:01keyboard shortcuts that are available to you.
03:03For this course, I am using the General Development Settings.
03:07Don't worry if you have picked one of the other choices.
03:09I will show you how to change these settings later.
03:11You have got some work ahead of you to get your Visual Studio installed.
03:15I would say that you will need an hour or so to download and install the
03:18required files before you are ready to go.
03:20But once you are over that hurdle, you will be able to create some
03:23remarkable applications.
03:24I have already got the tools installed, so let's move on to the next chapter.
Collapse this transcript
2. Exploring the Visual Studio Workspace (IDE)
Creating a Visual Studio project
00:00In this movie, I will spend a few minutes looking at how to create a basic
00:03Visual Studio project.
00:05Elsewhere in this series, I have details on creating the different kinds of
00:08project templates, and provide movies showing how to create an application
00:12from start to finish.
00:14My goal in this movie is to show what happens the first time you run Visual
00:18Studio, and how to create a basic project.
00:21How are you going to start Visual Studio?
00:24Okay, that was rhetorical question.
00:26You know what to do.
00:28In Windows 7, I am going to go to the Start menu, and then I am going to look in
00:32All Programs, and I am going to find my copy of Visual Studio.
00:37That's relatively easy to do because I have just installed it on this computer
00:41and Windows 7 is highlighting that folder in yellow.
00:43I will open this up, and I will find my Microsoft Visual Studio 2010 shortcut.
00:51Before I click on this link though, I would like to add a shortcut to my Start menu.
00:54That's really easy to do in Windows 7, by right-clicking and choosing Pin to Start menu.
01:01The next time I go to the Start menu, it's available up here at the top.
01:09Let's run the application.
01:10I will click on the link, and after about 3 or 4 seconds, Visual Studio starts
01:16and shows me the Start screen.
01:19There are times when Visual Studio needs administration privileges to complete a task.
01:24Here in Windows 7, that is easy to accomplish.
01:27I am going to go back to the Start menu, go up to my shortcut, and I am going to
01:32right-click and choose Run as administrator.
01:40And now I want you to look on the title bar of these two versions of Visual Studio.
01:45The one in front says Administrator in parentheses.
01:48That's how I know that this is running with elevated privileges.
01:53For today though, I only need to one copy of Visual Studio, so let me close this top window.
01:55There is one more thing I would like to show you before we create our first project.
02:03Visual Studio and .NET ship with dozens of additional tools.
02:07Let's go back to the Start menu, and go to All Programs and then back into the
02:13Microsoft Visual Studio 2010 folder.
02:16You can see, in here there are lots of sub-folders.
02:20There is one for Microsoft Windows SDK tools.
02:23There is one for Team Foundation Server tools.
02:26Down here is the Visual Studio tools folder.
02:29As you can see, there are lots of tools to work with.
02:31The one I want to look at is this one here: Visual Studio Command Prompt.
02:35For our convenience, there is this special command prompt hiding here in the Start menu.
02:39I am going to run it and see what it does.
02:43Look at the top of this window.
02:46Up here, it says Setting environment for using Microsoft Visual Studio 2010 x86 tools.
02:52What this does is it brings into scope all of the different programming tools
02:57that are part of the Visual Studio family.
02:59Now I can go out to the prompt and run those from a command line.
03:03So I am going to go up to the top folder by typing in cd\.
03:08And then I am going to type in the name of one of these tools.
03:10There is a tool called MSBuild for instance.
03:11When I press Enter, it shows that it found MSBuild, and now it's asking me for
03:20additional information.
03:23For another example, here is a tool called Visual Basic Compiler.
03:27Again, I will press Enter.
03:28It finds the tool, and since I didn't provide the rest of the settings, it just
03:33shows me the help file.
03:34We are going to be using this tool occasionally throughout this course.
03:39For now though, let me close the command window.
03:41Finally, we are ready to create our first project.
03:46There are a couple of ways to do that from this screen.
03:48One, I can click on this New Project hyperlink. Or if I would rather, I can go
03:53up to File > New, and then choose this Project menu.
03:58Today, I am going to use the hyperlink.
04:00So I am going to go back to the Start screen and click New Project.
04:03Visual Studio shows me a variety of different project templates.
04:09I have more details about these later in the course.
04:12There are Visual Basic templates up here, C# templates, and many more.
04:19Today I am going to look in the Visual C# and then the Windows section, and I am
04:25going to create a basic Console Application.
04:27I will leave the settings as the default, and then I am going to click on OK.
04:35After a few seconds, Visual Studio loads the project template and shows me my Code window.
04:43I would write the code, I would compile the code, I would run the application,
04:46but those are topics for another movie.
04:49That was easy, wasn't it?
04:50Coming up in the next movie is the thrilling story on how to use the Solution
04:54Explorer over here on the right edge of the screen.
Collapse this transcript
Working with Solution Explorer
00:00A complex business application might consist of dozens of projects, thousands
00:04of code files, and hundreds of ancillary items.
00:08With so many files loaded into Visual Studio, you need a way to understand the
00:12chaos and navigate through your assets.
00:14The Solution Explorer is the tool that tames this potential mess.
00:19I'm inside Visual Studio, and I am going to create a brand-new project, and then
00:25we are going to look at it over here in the Solution Explorer, which is usually
00:29docked to the right edge of the screen.
00:31Before I talk about Solution Explorer, let me talk about the difference between
00:35a solution and a project.
00:37A solution, in .NET terms, is an item that can contain zero or more projects.
00:45A project, in .NET terms, is a section that contains many files that are used to
00:52construct a .NET assembly.
00:55Let me show you how that works.
00:57I'm going to go to the File menu and choose New > Project.
01:01I am going to create a C# console application.
01:07Down here at the bottom of the screen, I can give my project--which is a
01:11container for files and compilation instructions--a name.
01:15I am going to call this one FirstConsole, and I am going to save it out in my
01:21Exercise File folder.
01:22So I am going to choose Browse, and then I will go to my desktop and find
01:29Exercise Files, chapter 2.
01:34And I am going to create a new folder in here, 02, since we are in the second
01:39movie in this chapter.
01:40And then I will select the folder. Okay.
01:47Now here is the key part. Down at the bottom is the name of the Solution.
01:51The Solution contains zero or more projects.
01:54In this case it's going to create a project called FirstConsole, and the name of
01:56the solution will be exactly the same.
01:59Over here you can see that I have a choice to create a directory for
02:03the solution files.
02:04There are typically two files affiliated with a solution: an FLN file and an SUO file.
02:10Those will go in a separate directory.
02:12I am going to click OK, and there is our project.
02:17It's a console application.
02:19It shows the FirstConsole project. In the Solution Explorer,
02:23it's inside the FirstConsole Solution.
02:25Another common pattern is to create a stand-alone solution first.
02:30Let me show you how to do that.
02:32File > New > Project.
02:36Then I will scroll down to the Other Projects Types node and then I am going
02:44to choose Visual Studio Solutions.
02:46I will pick Blank Solution, and then I'll choose the name of the
02:51solution: FirstSolution.
02:52If you are following along, click OK.
03:01What's the difference?
03:02Look inside the Solution Explorer.
03:03There is only a solution.
03:05There are no other files.
03:06I'm going to add two projects to this solution.
03:09My favorite way to do that is to right- click here in the Solution Explorer and
03:14choose Add, but there are other ways to do this.
03:16I am going to choose Add > New Project, which takes us back to the same dialog
03:21we saw a minute ago.
03:23This time I'm going to create a Visual Basic application. Again, I am going to
03:27create a Console Application, so I will call this project ConsoleVB.
03:32I'll click OK, and over here in the Solution Explorer, you will now see one project.
03:44Note that the file ends with .vb, which signifies that it contains Visual Basic
03:48code. And now I am going to go up to my Solution one more time, right-click,
03:53choose Add > New Project.
03:55I will pick the C# language, and then Windows, and then I think I'll choose
04:04the WPF Application.
04:05I will call this one WpfAppCS and click OK.
04:21So let's review what I've done.
04:23I created a blank solution and called it FirstSolution.
04:26I added a Visual Basic Console application to it, and I added a WPF
04:30C# application to it.
04:33Now I am going to save this file, and then I am going to go out to my hard drive
04:37and show you how the Solution Explorer mimics the same settings that you have
04:41inside your hard drive.
04:42I am going to right-click on my solution, and I'm going to go down and choose
04:48Open Folder in Windows Explorer.
04:50Here is what I want you to see.
04:53There is an SLN file that you typically use to load the solution, and that
04:59contains instructions to tell it open this folder, which contains the vbproj file.
05:05The vbproj file contains all the information about what to load for this project.
05:11Also in the C# folder here, there is a csproj file, which contains all the
05:18instruction about what to load for that project.
05:19So Visual Studio uses the SLN and csproj and the vbproj and other proj
05:25files to figure out what to actually load into Visual Studio and to show in
05:29the Solution Explorer.
05:30Now, you can see there are some hidden files, like there's this bin folder; that
05:36doesn't show up over here in the Solution Explorer.
05:39That's because Microsoft has decided they don't want to show you that by default.
05:45I can enable that by coming over here and clicking on the Show All Files, and
05:49then you can see the bin and object folder show up there.
05:52So it basically mimics the file system.
05:54Not only can you add files to your project, but one more thing you can do is you
05:59can add references of other precompiled assemblies.
06:04That's why I have this Reference folder.
06:06I can right-click here and choose Add Reference, and now I can go out and add
06:11other .NET libraries of this project.
06:16I have more details about how to do this in other movies in this series.
06:21Creating a project is easy, but it's not something that you do everyday.
06:25I'd say that you are more likely to open an existing project over and over
06:27again, so that's the topic of the next movie in this chapter.
Collapse this transcript
Working with big projects
00:00I don't know about you, but I get tired of seeing trivial little projects and product demos,
00:05so I decided to hunt down a mammoth project to show you.
00:08For this demo, I wanted big.
00:11Not only do I want big, I also want realistic.
00:14So I turned to Microsoft Open Source.
00:17Did you know that Microsoft maintains an open source web site called CodePlex?
00:21It's not as big as other open source sites, like SourceForge, but it has a lot of
00:26.NET projects available to download.
00:28I picked this Silverlight toolkit for today's demo.
00:31This is production-level code that is maintained by Microsoft.
00:36To open a project in Visual Studio, you go to File > Open and then choose the
00:43Project/Solution menu item. Also, you can use Ctrl+Shift+O. For this project, I'm
00:51going to open the Exercise Files folder, Chapter 2, movie 3, and then here is the
01:00source for the Silverlight 4 toolkit.
01:02I will drill down into Silverlight toolkit and then the Source folder, and then
01:08at the very bottom of the screen, I am looking at the
01:12Silverlight.Controls.toolkit.sln.
01:16You might remember from another movie that SLN is the file that loads the other projects.
01:23It just so happens that this project contains 23 projects and nearly 4,000 files.
01:29On my dual core laptop, it takes about five seconds to load the project and
01:33when I compile the code, it takes about 28 seconds to compile, the very first
01:37time I compiled the code.
01:39Now you might remember that the Solution Explorer is there to let you navigate
01:44through your projects and your source code.
01:46So if I go over to my Solution Explorer, you will just see that I have a folder
01:49up here called Source.
01:50Now on your machine, you might need to collapse and open certain nodes
01:54in Solution Explorer.
01:55I have got a Source folder, I have got a Themes folder, and I have got a Testing folder.
02:00Inside the Source folder, I have multiple projects. There's a Controls.Data.toolkit
02:07project and many more.
02:09The one I am interested in is down here.
02:11It's called Controls.toolkit.
02:13When I open that project, I see multiple folders. And here you can see, for
02:18instance, there is the DragDrop folder, the Expander folder, and inside the
02:23Expander folder is the actual code that creates the Expander control.
02:28And it appears that there are four files here: three C# files and one XAML file.
02:35Now if you are not familiar with XAML or X-A-M-L, that is the markup language that's
02:39used by Windows Font 7, Silverlight, and by WPF.
02:47For today, I want to look at the GlobalCalendar.
02:50Now what does this look like when you run it?
02:51If I switch over to a browser--I have Internet Explorer open here--
02:56I've loaded this site: silverlight.net and then this URL.
03:00This is a sample site for the tools I'm showing you currently. And if I open
03:04this up and scroll down to the Calendar section, this is the calendar control
03:09that we are looking at in Visual Studio.
03:11Again, this is production- level code we are looking at here.
03:14So switching back to Visual Studio, you will see that I have a class called
03:19GlobalCalendar, and I then want to go GlobalCalendar.xaml.
03:23Down here, I have a GlobalCalendarButton.cs, which contains the source code
03:28for that button, and the GlobalCalendarButton .xaml file, which contains the XML markup.
03:42There are a lot of things that you can learn by looking at other people's
03:45code, both good and bad.
03:47I encourage you to explore this project and learn more about what Microsoft is
03:51doing with Silverlight.
Collapse this transcript
Taking a tour of the Integrated Developer Environment (IDE)
00:00I spend a good portion of my day inside Visual Studio when I am working on a project.
00:05I need to be able to control the layout of my windows, and get the screens
00:08arranged the way I prefer.
00:10During this movie, I'll show you how to work with the windows inside Visual
00:12Studio, and I'll share some layout tips I've learned over the years.
00:16I am inside Visual Studio, and I have opened this TourIDE solution.
00:22That solution contains one project called ConsoleApplication1.
00:26The first thing I want to show you is how to open a Code Editor window.
00:30I have two code files inside this project:
00:33Book.cs and Program.cs.
00:37If I double-click on Book.cs in Solution Explorer, you'll see that it loads
00:43into the center portion of the screen, and it places a tab across the top of the window.
00:48If I double-click on the Program.cs file, you'll see that another tab is added
00:54in the same location.
00:55It's very easy to close these tabs.
00:58There is an X button on each of the tabs.
01:00Click on the X button to close it.
01:03To reopen it, I need to go back to Solution Explorer and double-click on Program.cs.
01:09Occasionally, you'll end up with many Code Editor files open.
01:13In that case, they may not all fit across the top of this area.
01:18Microsoft provides a dropdown window here that shows all of the available tabs.
01:23If you can't see one of your tabs, be sure and explore this area, to see of it's there.
01:28Of course, in this application, I've only got two tabs open, so I don't need to worry about that.
01:32Visual Studio supports a very flexible layout engine for their Windows.
01:38I can take this Program tab, and I can pull it out into the center of my viewing area.
01:44Now I've got an independent window floating there. And of course, I can size
01:49this window if necessary.
01:52I am on a Windows 7 machine, so if I take this window and I slide it over to the
01:57edge of the monitor, it will automatically dock that to the edge of the window.
02:03Now, keep in mind this is a Windows 7 feature, not a Visual Studio feature.
02:07So if I let go, you see that window is now docked to the side of my monitor.
02:12If you're fortunate enough to have a multi-monitor machine, you can take
02:16this and slide it off onto the other monitor, and then you can dock it over
02:21on the other monitor.
02:22It's hard to show docking on a second monitor though in a movie, so take my
02:26word for it, that will work.
02:29Now I want to get this tab back where it was before.
02:33There is a couple of ways that I can get there.
02:37One thing I can do is I can double-click on this header.
02:40When I double-click on the header, it maximizes the view of this code window.
02:46Watch, double-click, and now I have the entire screen taken up with that window.
02:51That's not what I wanted.
02:52I wanted to put it back where it was originally.
02:55Here is the tip I learned a while back:
02:58you hold down the Ctrl key, and then you double-click on the header. And there
03:03it is, back in its original position.
03:07The other way to get it back is to use this visualizer that's sitting in the
03:11center of the screen.
03:12Do you see it there?
03:14There is this floating icon that looks like it has five work areas.
03:19That represents the five different places that I can put this window as I am
03:22dragging it around the screen.
03:25Look at the tip of my mouse.
03:26When my tip of the mouse enters this area, you'll see a blue highlight show
03:32up in the background
03:34that says now if I let go of this mouse at this point, it's going to put that
03:39tab on that section of the screen, which is not exactly what I wanted.
03:43I want to have it go back where it was originally.
03:47So, I'll come down here and take a look at my other choices.
03:50Split the screen vertically, and put it either on the left or the right, put it
03:55on the bottom half of the screen, or move here to this center item, and that
03:59places it back in the original position.
04:01Again, I am going to let go when my mouse is over that center point.
04:04Now I can do the same thing with other windows.
04:07Look over here in the Solution Explorer.
04:09You see in the bottom of the window here--there are three tabs.
04:12Now your machine may look slightly different than this.
04:14On my machine, I have Solution Explorer, a Team Explorer, and Class View down here.
04:20I can take one of these tabs and pull it out, just like I showed you for the code windows.
04:26I can maximize it and dock it to the left and the right.
04:29If I want to put it back in this location or let's say I want to put it in
04:33another location like down here in this Properties section, I just drag, keep my
04:38eye on those visualizers, and I am going to go down here and use the center
04:43icon, and now I am going to add it as a second tab to the Properties area.
04:46Okay, Class View and Properties.
04:49Pull it out, move out here, and I can put it back.
04:53Well, let me show you one more tip.
04:56You'd likely to make a messier screen at some point and can't figure how to get
05:01something back where it belongs.
05:03Luckily for you and for me, there is a Reset switch for these
05:06embarrassing moments.
05:07The simplest way to handle this is to just go up to the Window menu and choose
05:14Reset Window Layout.
05:17This sets it back to the default settings.
05:20So now you can see the Class view is back over here.
05:25It also added another tab that we didn't see originally.
05:29The default views have this thing called the Start Page open right there.
05:32I'll close the Start Page tab.
05:34There is another feature on the Layout windows I'd like to show you now.
05:39It's called the Auto Hide feature.
05:41If you look on the left side of my screen, you'll see that it says Toolbox
05:45and Server Explorer.
05:47Those are windows that are automatically hid, or shrunk down to a very small
05:50size, and placed on the left or right side of the window.
05:53If I move my mouse over one of them, say the Server Explorer, I can see the
05:59window temporarily. And if I move my mouse away from the edge, it will go
06:04back to where it was.
06:05It's showing me that there is a window I can look at, but it's not keeping it
06:08permanently in my work area.
06:10If I want to dock it to the side of the window, I need to click on this Auto Hide button.
06:15You'll also note that over here in the Class view and in the Properties view,
06:22there is an Auto Hide button.
06:23These ones are showing, but if I where to click on those Auto Hide buttons, it
06:26would collapse those down to the right edge of the screen.
06:29Next, I want to show you, what's called the Full Screen mode.
06:33Sometimes, I just want to remove all of the windows and just concentrate on
06:36one window at a time.
06:38To accomplish that, I go to View > Full Screen.
06:44I don't know if you saw it, but all my toolbars across the top also disappeared.
06:50All I have now is the menu.
06:52I work in my code for a while, and when I am done with the Full Screen view, I
06:56click here on this icon to return back to the original view.
06:59Currently, I have about six or seven windows available for me to work with.
07:05There are other windows that are not shown by default.
07:09You can find more about these windows in the View menu.
07:12For instance, there is the task list and the Output window.
07:17There is also the section called Other Windows, which contains 12 or 15 other windows.
07:23Also, when you're debugging an application, you'll find some extra windows in the Debug menu.
07:28I'll talk more about this in the debugging chapter.
07:30Right now, you can see I have the Breakpoints, the Output, and the
07:33Immediate window available.
07:36For my last tip in this movie, let's look at what happened when you do
07:39a Ctrl+Tab keystroke.
07:41I am going to hold down the Ctrl key, and then I am going to tap the Tab key
07:47without letting go of the Ctrl key, like this--tapping the Tab key.
07:52So basically, all of the files that I currently have opened in the editor are
07:56listed in the Active Files section, and all of the windows I have currently
08:00opened, are available over here--the Tool windows.
08:04So you can see that if I go to Server Explorer by clicking here, my focus will
08:08move over to the Server Explorer.
08:10I have been using Visual Studio for a long time.
08:14Each version gives me some new IDE feature I can't live without.
08:18I think my favorite new feature in this release is probably the
08:21multi-monitor support.
08:24I have three monitors on my Desktop, and this feature is invaluable, but there
08:29are a lot of other good features for you to find.
08:32Have fun exploring, and let me know which one you think is your favorite.
Collapse this transcript
Introducing drag-and-drop UI design
00:00One of the major advantages of designing UI in Visual Studio is the drag-
00:04and-drop placement of elements during the design of both rich client and web applications.
00:09These elements are all available in what is known as the ToolBox.
00:12I am inside Visual Studio, and I've opened a solution, appropriately called ToolBox.
00:18Inside this solution, I have three separate projects:
00:21an ASP.NET project, a Windows Form project, and a WPF project.
00:26The reason I picked these three project types is all three of them support a
00:29drag and drop designer.
00:30I am going to start by looking at the Windows Form App.
00:34I'm going to open this up, and I am going to find this file called Form1.cs.
00:38I'm going to double-click on it.
00:40That will load what's called the designer into this central area of my screen.
00:45Now I can go to the Toolbox, which currently on my machine is down here on this tab.
00:50If you don't see it, you may need to go to the View menu as a View toolbox.
00:55It also may be docked to the edge.
00:57In that case, you need to go over here and click the Auto Hide button to make it visible.
01:01I'm going to open the Toolbox, and now I can do a drag-and-drop operation.
01:07For instance, I can take this button and drag it from the Toolbox onto
01:11my designer surface.
01:13The Windows Form designer has this little rectangle that shows where I am going to drop it.
01:19Now when I let go, it will add the button.
01:22Now behind the scenes, Visual Studio is writing some code.
01:26It's not very obvious where it's writing this code, however.
01:30It's over here in the Form1.Designer.cs.
01:34Other designer surfaces are better at showing you where this code lives.
01:37I'll show you that in a minute.
01:39First, let me show you what happens when you want to work with multiple controls.
01:41I am going to go add another control.
01:46This time I am going to choose the TextBox control.
01:48I will drag that over.
01:50You can also double-click on the Toolbox to add it.
01:52You see that I get this blue line.
01:55The Windows Form designer is using this blue line to show me that I have aligned
01:59the left edges of these controls.
02:01So when I let go over the mouse now, it will snap to that location.
02:05Once I have an item added, I can click on it.
02:09I get sizing handles--which means I can change its size--
02:13I can move it to a new location by dragging it with my mouse, and of course, I can delete it.
02:21Let me show you the WPF designer now.
02:23I'll go to the Wpf project, find this file called MainWindow.xaml, and double-click.
02:34Immediately, you can see that the designers are different from each other.
02:37This designer has a white design area, and at the bottom of the split screen is
02:42a set of XAML, which is Markup Language.
02:45Now watch what happens when I drag a button from this Toolbox over to
02:49the designer surface.
02:52I get a plus symbol.
02:53I get a blue outline.
02:54When I let go, the button shows up, but also in the markup section
02:59you can see that it says <Button, the Height = "23".
03:04So what do you think what happen if I come over here, and I typed in 60 for the height?
03:08Right, the button changes size.
03:10So it's a lot easier to see the relationship between the text here and the
03:15button than you get in the Windows Form designer.
03:18Something similar happens in HTML and ASP.NET editing.
03:23If I go over to my AspNet application and open up a file called Default.aspx by
03:31double-clicking, you see that I have a HTML document here.
03:37If I want to see the drag and drop designer, I need to click on the Design tab
03:40on the bottom of the screen.
03:43Now I can go to my Toolbox, find a control, like this button, and drag it over to
03:50the designer surface.
03:52Now if you look at the source code, you'll see this line, <asp:Button. That shows
04:00where it's instantiating, or creating an instance of that control.
04:03So you see how each designer has a different way of working and representing
04:08the underlying data.
04:10I am going to return back to my Form1.cs designer.
04:15I'll click here in this tab.
04:17Now, what I want to spend the next five minutes or so looking at is what you can do with Toolbox.
04:24For instance, I can rename the elements and move them to new positions.
04:29That's what I want to show you now.
04:31The first thing I want to show you is that there are sections of this Toolbox.
04:35I currently have the Windows Form designer selected, so the Toolbox
04:39is context-sensitive.
04:40It's just showing me only sections of the Toolbox that have to do with the
04:44Windows Form designer.
04:46If I switch to the WPF designer, you will see that the section is changed.
04:51Now it has one for common WPF controls and another for all WPF controls.
04:56If I switch back to the Form1.cs, you will see again that the Toolbox changes
05:01because it's monitoring what I am editing in the center of the screen.
05:06I can go to my Toolbox, right-click, and uncheck this List View, and what you will
05:12see is I now have smaller icons.
05:15It no longer has the Text view anymore, so I have to hover over the icon to see
05:18what control is, but I save a lot of screen real estate by doing this.
05:23To go back to seeing the Text view, I right-click and choose List view.
05:27You can create your own sections of the Toolbox.
05:34You can go down to this General section down here, and I can add other
05:39third-party controls. If you happened to have bought a third-party package of user
05:43controls, you can go and pick the Choose Items menu and then go out and
05:50find those controls.
05:51They might be listed here, or you might have to go and click the Browse button.
05:55But what you will do here is you will find the control you want--say this one
06:00right here--and then click OK, and after a few seconds that control will now be
06:05added to whatever section of the Toolbox you currently have highlighted. In my
06:09case, that would be the General section.
06:10I don't want to do that for this demo, so I am going to go ahead and click Cancel.
06:13While you're working in the Toolbox, you can rearrange the controls.
06:21I can pick this control up and drag it to a new section.
06:26I can rename the control, by right- clicking on it and choosing Rename.
06:33If I make a mess of the Toolbox, I can always click Reset Toolbox, which I am
06:38going to do right now.
06:39On my machine, this usually takes 20 seconds or so to reset.
06:48Some of the controls that you'll find in the Toolbox are not visible at runtime.
06:53Let me show you one of those.
06:55It's called the Timer Control.
06:56I am going to go to All Windows Forms, scroll down to the T section and then
07:04take this timer, and I am going to drag it and drop it on my designer surface.
07:07Now keep an eye on the bottom half of the screen.
07:09When I let go over the mouse button, you'll see that Visual Studio adds a
07:13special section called the component tray, and because the timer is not visible
07:18at run time, it puts a timer on the component tray.
07:21There is a companion window that's often used when working with the
07:23Toolbox and the designer.
07:25I am talking about the Properties window, which is over here on the right side of the screen.
07:29It's usually underneath the Solution Explorer.
07:32It's such an interesting tool that I made an entire movie about it, and it's the
07:36next one in this chapter.
Collapse this transcript
Working with the Properties window
00:00A frequently used tool window in Visual Studio 2010 is the Properties window.
00:05This window is typically docked to the right side of the IDE.
00:08The hotkey for this window is F4.
00:11I'm inside Visual Studio, and I have this solution called PropertiesWindow open.
00:15There is one project inside this solution called WpfApp.
00:19This is really a mixed application.
00:21It has a Windows form and an HTML page and also a XAML page in here.
00:26I am going to start by looking at the MainWindow.xaml page.
00:29I'll double-click on this icon, and that loads the designer.
00:34Now I'm going to add a couple of controls to this designer, by going to my
00:37Toolbox and dragging the Button to the designer surface, and then I am going to
00:45drag the TextBox to the Designer surface.
00:49Next, I am going to the show you how the Property window works.
00:51The Property window is over here in the lower right-hand corner of the screen.
00:55To make it easier to see, I am going to detach it by dragging it out and moving
01:00it into the main part of the window.
01:04You may need to size it to fit on your screen.
01:07Then I am going to sort alphabetically.
01:10By default, when you first start Visual Studio, the property grid is sorted by
01:14Category view, which means it stores the different property settings in these
01:19categories, like Layout.
01:21I don't like this view.
01:22I prefer the alphabetical view, so I am going to switch to the alphabetical view.
01:27Then I am going to show you that when you select an item in the designer, the
01:33property grid automatically monitors that control and loads the property grid
01:38with the settings from that control.
01:41So I'm seeing the properties of this button.
01:44When I click on the text box, I'm seeing the properties of the text box.
01:48You can verify that by looking up here and seeing that I'm working with a
01:51text box, and the name of the text box is textBox1.
01:55There are a lot of properties on the button class.
01:58Let me select it again.
02:01If I want, I can filter the property grid to only show me a subset of the properties.
02:06I do that by clicking in the Search and typing in the first letters of the
02:10item I am looking for.
02:13For instance, I want to look for the Width property, so I am going to type in "wid".
02:17I see the Width property down here at the bottom, but I also see that there was
02:21a MinWidth and a MaxWidth property that also contain those three letters.
02:25When I'm done filtering, I can click on this X button to go back to the entire view.
02:32Each property is listed here in alphabetical order.
02:35One of the cool things about the property grid is that when you select a
02:40property, like this Background property,
02:42it loads a custom editor in this section over here.
02:46Because the background uses a brush, it's loading the brush editor.
02:51So when I click on this dropdown, I get a color editor for changing the color
02:56value of the Button.
03:00When I click on the Clickmode property, I get a different editor.
03:05Let's see what I have over here.
03:07This is an enumerated value, so I get in the list of the three possible
03:10enumerated values for this property.
03:13This is very handy.
03:15If you like to edit your items visually, this is super-easy to use.
03:18There is an indicator down the center of the property grid that shows whether
03:23I'm using the default value or not.
03:25This diamond here signifies I'm no longer using the default value for the
03:28background color of the button.
03:30I can reset it at any point by right-clicking on this icon and choosing Reset Value.
03:36Now this only works in Silverlight and WPF applications.
03:40ASP.NET, the HTML editors, and other designers use a different metaphor for this reset.
03:46For instance, like the ASP.NET editor, it shows if I'm using a non-default value
03:52by bolding the text in this column.
03:56Not only are there properties in this button, but this button also contains events.
04:02The property grid shows me the events when I click on this tab here.
04:07This lists every event that this button is capable of listening for.
04:12So if want to write some code for a click event, I would go to this section,
04:16and I would type in the name of the function that I want here. Or let's say I
04:19want to work with the mouse event, so I'll go down to the Ms and find something
04:23called MouseLeftButtonUp.
04:26Now the way I can write code is I can double-click on this event, and Visual
04:31Studio will pick a name for me and write the code. Let me show you.
04:35I'll double-click.
04:37Visual Studio switches me over to the Code Behind window and shows that it wrote
04:41a function called button1_MouseLeftButtonUp.
04:45We'll learn more about these in the Coding chapter.
04:46I'm going to switch back to the designer.
04:51Now I can see that this event has the name of the event procedure listed here.
04:56If I don't like that name, I can change it here or I can suggest my own name.
05:01Let me go to MouseEnter and type in my own function name, like that.
05:07I will press enter.
05:10Visual Studio switches over to the Code view and writes in this piece of code.
05:15Notice how the name is the one that I selected in the property grid.
05:22When I switch back to the Designer view, if I want to undo the event wiring, I
05:27can come here and delete the word "myCode".
05:31Now when that event fires, myCode will no longer run, however, Visual Studio
05:35does not remove the code from the Code Behind window.
05:38You see it's still here.
05:40Let me put this property grid back in its normal position.
05:43To do that, you'd hold down the Ctrl key and double-click on the header.
05:48Now, it's back in its original position.
05:50Then I'll switch back to the designer, and let's review.
05:54The property grid contains two sections:
05:56Properties and the Events.
06:00In the Events section, you can see the names of the events, you can write code,
06:04you can rename the code, and you can remove the code.
06:08If you're in the Property view, you can monitor the properties of the selected
06:13object and change them.
06:15One thing I didn't mention earlier is that you can select more than one item.
06:19If I drag a second button over here, and select the two of them, then the
06:25property grid, when I make changes over here, it's changing common properties
06:29that are shared among both of these controls.
06:32So if you are a visual person, you are going to love using the Property window
06:35to edit your UI items.
06:37If you're not a visual person, you can always hand-edit the HTML, the XAML, or
06:42the C# code instead.
Collapse this transcript
Looking at Server Explorer
00:00For this movie, I need to run Visual Studio as an administrator.
00:04Let me show you how to do that again.
00:06I am going to go to my Start menu, find my shortcut to Visual Studio,
00:12right-click on it, and choose Run as administrator.
00:20Now that I'm inside Visual Studio, I need to open a project, by going to File >
00:25Open > Project/Solution.
00:29Go to your Exercise File folder > Chapter 2 > Movie 7 and then open the Server
00:36Explorer folder and then load the ServerExplorer.sln file and click Open.
00:45The Server Explorer lives over here on the left side of the screen.
00:48It provides a way to explore hardware resources on your local computer. And if
00:52you are on a network, it also permits access to computers and services on remote computers.
00:57You can see, in my Server Explorer, one computer, which is the local computer.
01:02If I expand this node, you'll see several sections.
01:06There is the event logs, the management classes, messaging queues, and many more.
01:12I am going to show you today the Event Logs and the Data Connection section.
01:17There are lots more to explore in here, however.
01:19Let's start with the event logs.
01:22The event logs exist in Windows to allow applications to store information about
01:26what's happening as they are running.
01:27If I expand his node, you will see the different kinds of event logs.
01:32I'm going to work with the Application event logs.
01:34When I open this up, you'll see each application listed here, and if I open up
01:41one of these--let's say this one done here called Desktop Windows Manager--
01:45you'll see that there are a number of events that have been logged over the
01:51last couple of days.
01:52I'm going to write my own event items into the event log.
01:58I've already got a node from earlier today called Essential Training.
02:01I am going to write to that note.
02:02Let me show you how easy that is to do.
02:05First, I'm going to take this application log, and I am going to drag it over
02:09and drop it on the designer surface.
02:14When I do that, Visual Studio adds an item to this Component Tray down here at the bottom.
02:18Let me close this Output window here.
02:23Next, I'm going to write some code on a button click event.
02:28So I need to open my Toolbox, find the Button control, drag it to the designer
02:34surface, and then to write the code,
02:37I am going to double-click on this button.
02:42This is a Visual Basic application, so I need to write a couple of lines
02:45of Visual Basic code.
02:46I'll start by naming my source for the event log.
02:50I'm going to call it Essential Training, like that.
03:06Then on the next line of code, I'm going to write to the log file itself.
03:18I'm going to say, "Hello log."
03:26Then I'm going to type a comma, and here I'm going to use EventLogEntryType,
03:29and here I get to pick the warning level. Is it an error, a warning, a piece of information?
03:43I'm going to chose Error.
03:45So let's review what I did again.
03:49I wrote a piece of code for the button click procedure.
03:52I told the event log I am going to be writing something into a log called
03:56Essential Training and then the actual data that I wrote was a string "Hello
04:00log", and this is the type of log entry.
04:03It's an error entry.
04:05I'll run the application now by pressing F5.
04:10Then I'll click on the Button.
04:13After a few seconds, I'll close the application. And then I'm going to go
04:17look in the Event Log.
04:19I can use the normal Windows Event Log viewer, or I can go to Server Explorer,
04:24find the Essential Training section and open that up.
04:29As you can see, I have three entries.
04:31You're going to have more or less than I do, depending on how often you've run your demos.
04:36I've already run some demos earlier today when I was setting up this computer,
04:39so you can see that at 3:57 this afternoon I wrote a test in there, and here is
04:45the one we just wrote a few minutes ago: Hello log.
04:48Another favorite of mine in the Server Explorer is the Data Connections section.
04:53This allows me to add connections to databases.
04:56Let me show you how easy that is.
04:58I'm going to click here, Connect to Database.
05:01I get to pick the kind of database I am going to connect to.
05:05For today's demo, I am going to use a SQL Server database file.
05:09I am going to click on Continue.
05:12I am next asked what information is required to find that file.
05:19I need the file name, and I need a login credential.
05:22So I'm going to start by clicking on Browse.
05:26In your Exercise File, on your desktop, is an Assets folder.
05:33Inside that Assets folder is a northwnd database file.
05:38It ends with an MDF extension.
05:40You may or may not see this extension, depending on how you've got your
05:42computer configured.
05:43I'm going to click on northwnd.mdf and click Open.
05:49Then I am going to click the Test Connection to see if I have permissions to
05:52talk to this database, and I do.
05:58Next, I'm going to click on OK, and after waiting a few seconds, a new connection
06:06shows up in Server Explorer.
06:09Now without having to leave Visual Studio, I can open this node and look at the
06:15structure of my database.
06:16For instance, here are all the tables inside the database.
06:20Here are the stored procedures that are inside the database.
06:25When I have a table, I can click on it and learn more about its structure.
06:31This shows me the number of fields that are part of the table.
06:35I can right-click and choose Show Table Data to actually talk to the database
06:41and see the data that's stored inside that file.
06:46On several of the movies in this title, we'll be looking at this database.
06:50The Server Explorer exists to let me look at my local computer and other servers
06:54on my network and discover information about the computer itself and databases
06:59that are out there available for me to use. It's a great tool.
07:01I suggest you check it out.
Collapse this transcript
Exploring the new Help engine
00:00For better or worse, Microsoft has created a new help engine.
00:04The official name is Microsoft Help 3, and it is the de facto help engine in
00:09Visual Studio and MSDN.
00:11The claim to fame for this latest version is that it runs in a browser.
00:16The goal--or so I've heard--is to make it easier to keep the help files up to date.
00:21During a typical install of Visual Studio, you were offered the opportunity to install Help.
00:26Whether you chose this option or not, you can still configure and update Help
00:31from inside Visual Studio.
00:32Let me show you how that is done.
00:35I'm inside Visual Studio, and I have opened a project called
00:38ExploringHelpEngine.
00:40I'm going to click on the Help menu and choose Manage Help Settings.
00:46I've already run Help a number of times on this computer, so I move right into
00:50the Help Library Manager.
00:52If you're running this for the first time, you might see a dialog that looks
00:57like this: Online Help Consent.
01:01You also might see this dialog: select local content location.
01:06As I said, I have already installed Help and used it several times, so I didn't
01:10see either of those dialogs.
01:12Let's return back to Visual Studio and look at this Library Manager.
01:17You can choose whether your help runs locally or online.
01:20Microsoft suggests that you run online, so you always have the latest content.
01:25So I'm going to click on this hyperlink, Choose online or local help, and you
01:30can see I already have I want to use online help.
01:34If I switch over and say I want to use local help, then I have to tell it where
01:38to put the local files.
01:40Now if you run local help, a special help service runs on your computer
01:44and intercepts calls from the browser to ensure that features like search work correctly.
01:48I'm going to go ahead and click OK here.
01:51Now, I need to install some content from online to my local computer.
01:56I'll click on this hyperlink.
02:00The Help Library Manager will go out to Microsoft and show me the available help libraries.
02:05As you can see, there are dozens of these help libraries.
02:10I can then click on one-- let's say this IIS Developer.
02:13The help engine tells me that it's going to be a 23 MB download.
02:17If I click the Update button, it will download a copy of that file to my local
02:21computer, install it, and then tell me it's finished.
02:26I am not going to do that today because I am going to use the online help.
02:30So I am going to click Cancel here.
02:31Then I am going to return to choose online or local help and go back to I want
02:37to use online help, and then I'll choose Exit.
02:43Let's see how the help engine works.
02:44I'm going to open this Program.cs file by double-clicking on it, and I'm going
02:51to scroll down here and look at this System.IO.FileStream class.
02:56I'm going to click on the class and then in Visual Studio, all you need to do is
03:01press F1 to launch Help.
03:03Remember, I have online help configured.
03:06So it opens up my default browser--in this case Firefox--and loads the MSDN help file.
03:13You can see that this is a FileStream class, and I can see a list of information
03:18about the FileStream class.
03:19I can click on this hyperlink to read more information about it.
03:24I can click on this C# tab here to read the C# declaration.
03:28Otherwise, if I was writing this class in C#, this would be the way I
03:31would declare my class.
03:32I can read more about the help down here, see some examples in my code, and then
03:44I can see, at the bottom, the Inheritance Hierarchy, which platform it applies to,
03:51whether it's considered to be thread- safe or not, and which versions of the
03:55framework supported the FileStream class. Virtually every single version that
04:02has shipped has supported this class.
04:04I can also come down here and see the file members.
04:06Here are the constructors of the class.
04:12Here are the methods that are available.
04:13Here are the properties.
04:19Also, I might see some events if the type supports those.
04:22I'm going to switch back to Visual Studio, and this time I am to click on
04:26the Console.WriteLine.
04:28Now there are multiple overloads of this WriteLine function.
04:31This WriteLine function takes a decimal value.
04:35This WriteLine function takes a double value.
04:37So I'm going to click on this first one and press F1.
04:40Now what you see is I'm showing the Console.WriteLine method, and it shows all
04:45the different overloads of that method that I just showed you.
04:48So there should be one here for a double value. There it is.
04:51I'm going to click on this.
04:53That takes me to details about the Console.WriteLine method that takes it double.
04:57There is a new twist in the MSDN help.
05:03If you scroll down to the bottom of the screen, you'll see the section
05:07called Community Content.
05:09Microsoft is trying to make it more like a wiki, so that you and I can change
05:14the help and add supplemental pieces of information to each help page.
05:18So what I could do is if I saw something wrong on this help page, or I wanted to
05:23write a more useful example, I could click on Add, log in with my Windows Live
05:31ID, and then I could submit my update to the help file.
05:36There is one more interesting thing I would like to show you about help.
05:39If I return back to Visual Studio and click on the Help menu, you'll see
05:44there's a number of settings in here: Report a Bug, Samples, Customer Feedback
05:49Options, and many more.
05:51The one I want to look at here is the MSDN Forums.
05:54If I click here, it'll take me to the online forums, where I can ask questions
05:59and read what other people are doing as far as .NET is concerned.
06:02Notice that it has loaded it inside Visual Studio.
06:06There is a browser window built right into the Visual Studio engine.
06:10Here are the different forums that are available for me to ask questions.
06:15So I might click on this Visual Studio setup, and then I can see there are 7,980
06:26different threads running inside this group.
06:29Well, that's about all I have to show you in help.
06:32If you're a veteran Visual Studio programmer, you may need some time to adjust
06:35to this new help engine. But overall, it's a decent implementation, and it
06:39gets the job done.
Collapse this transcript
Setting options for the IDE
00:00Visual Studio is a tool that I use almost daily.
00:03I have strong opinions about the best way to configure Visual Studio for the way that I work.
00:08The Settings Options dialog is a place to tweak the IDE so that it works the way that you want.
00:14The first thing that I want to show you in this movie is how to save your
00:17settings or import existing settings.
00:19To do that, I'm going to open Visual Studio and go to the Tools menu, and then I
00:25am going to scroll down to this section here: Import and Export Settings.
00:30So here's the idea.
00:32When you get Visual Studio configured the way you want, you can come into this
00:35dialog and choose to export your selected environment settings.
00:41When I click on next, I can pick from a list of settings--by default, it chooses
00:48all the settings--and then I can click on Next.
00:52Here's where I pick the location and name of the file that contains my settings.
00:56Pretty easy to do. Just click on Finish. Success!
01:03Later I can come back into the Settings dialog and choose Import Selected
01:06Environment Settings and then click on Next.
01:09It ask if I want to say my current settings, and I say, "No, thank you," and here
01:15I can go out and browse for the file I just saved, or--this is another important part--
01:22I can choose from a set of default settings: General Development, Visual C Sharp development.
01:28So at any point in the future if you want to change from being a Visual Basic
01:31keystrokes to the C# keystroke you just go on here and choose Visual Basic
01:36or C# Development Settings, and that will replace default settings for
01:40those style of developers.
01:44Now that you know a little bit on how to save and import your settings, let's go
01:47and take a look at some setting themselves.
01:49You start by going to the Tools > Options, down here at the bottom. And there are a
01:53lot of settings in here. Obviously I don't have time to cover all of them in
01:58this movies, so I'll just look at a few of them.
02:00I am going to start by looking at General Settings area.
02:03I'm going to go to Environment, and then in here is a section called Keyboard.
02:09This is where you can configure Visual Studio to use the keystrokes that you want.
02:14What I can do is find an Action.
02:16Like here's one called File.SaveAll.
02:24When I click on, I can see that that's assigned to the Ctrl+Shift+S keyboard.
02:29I can come down here and type the new keystroke that I want.
02:32Let's say I want to do Ctrl+Alt+S. I am going to type in Ctrl+Alt+S, and it says
02:37that shortcut is currently assigned to View.ServerExplorer.
02:41If I'm okay with replacing that, I can click on this Assign button, and now
02:44whenever I choose keystroke it's going to run the File.SaveAll.
02:51Another feature I like in here is the Project Settings.
02:56Let me go into Projects and Solutions and pick General, and here are number of
03:01settings like where do we store our projects, the default location where your
03:05templates are located.
03:06All right, one here that I like to turn on and off depending on my situation is
03:12Track Active Item in Solution Explorer.
03:15So what happens here is if I click on a Code tab in Visual Studio it'll go
03:19over to the Solution Explorer and highlight that item, without me having to go find it.
03:24I like to have the Always show solution turned on.
03:28That means that no matter whether there I am in a single project file or a
03:31multiple project file, it always shows the parent node in the Tree View.
03:35One that I like to turn off is Save new projects when created. I am going to
03:40select this one, and show you what that one does.
03:44Now I can go to File > New > Project, and when I create my project--like
03:50this WPFapplication--
03:54I can edit some code in here, make some changes--
04:00let me to make a change to Width here-- and then when I go to close the solution,
04:07it actually hasn't saved it in a permanent location.
04:11Visual Studio is doing all the changes I make in a temporary location, and
04:14at this point it says, "Would you like to throw out this temporary project you have created."
04:18So if I say Discard, it tosses out this trial run.
04:21So it's a quick way to try out some ideas without permanently saving the
04:26project to my hard drive.
04:27Go back. I'll show you one more set of tools. Back into the Tools > Options,
04:34and then we are going to Build and Run. Now, what I like in this dialog is
04:39this Before Building.
04:40Building means to compile your application.
04:42I will have a whole chapter on building and the details of what goes on in that
04:46process, but essentially I can choose what happens if I got a file that I've
04:50opened, and I have made some changes, and I am ready to compile the application--
04:54do you want it to automatically save the changes?
04:56That's what this section means. Or would I like it to prompt me to save changes like this.
05:04Now, I'll create a brand-new project.
05:06Again, I'll choose the defaults, and then I see this prompt me, "Would you like to save
05:18the changes to the following items?"
05:25There are details on all these sections in other movies in this chapter.
05:28Remember to use Export Setting once you get Visual Studio configured the way you want.
05:33That way you won't have to spend a lot of time reconfiguring your environment if
05:37your hard drive crashes or you get a new computer.
05:40That concludes our grand tour of the Visual Studio developer environment.
05:44As you continue in this course, you'll discover more about certain sections of
05:48the IDE, but the chapter goal was to give you a good overview of the general
05:52layout and tools available inside Visual Studio.
Collapse this transcript
3. Building a Simple Application
Creating a simple WPF application
00:00If you've taken a few minutes to look through the table of contents of this
00:03course, you know that I cover a lot of different topics.
00:06For instance, there is an entire chapter dedicated to debugging tools and
00:10another on deployment.
00:12The intent of this course is not the focus on the details, like those chapters,
00:16but to provide an overview of how to build an application from start to finish.
00:20This way you get a chance to see the basic steps to build an app.
00:24The technology I picked for this chapter is Windows Presentation Foundation, often called WPF.
00:30I chose WPF for several reasons.
00:33First, it's a great way to build Windows applications.
00:37By that, I mean applications that run strictly on a Desktop.
00:40Second, WPF contains all the latest advances in graphics rendering as it runs
00:45on top of DirectX framework.
00:47Third, many of the other project types, like Silverlight or ASP.NET, are covered
00:52in depth in other lynda.com titles.
00:54In this chapter, we are going to build a RSS Reader.
00:57I'll start by creating a new project, I'll use the Visual Studio designer to
01:01create a portion of my user interface, and then I'll switch over to the XAML
01:05markup editor to show you how to manipulate the raw XAML text.
01:09You'll write some code to talk to the Bing search client and make sure that
01:12the web calls are non-blocking by using a worker thread and then see how to
01:16listen for .NET events.
01:18Then there is a movie looking at databases, XML binding, and data sets.
01:23Finally, you'll package up your application and deploy it to the user's computer.
01:28So let's get started by building our user interface.
Collapse this transcript
Building the UI with the editors
00:00To get started with my app, I'm going to use the designer in Visual Studio to
00:04create a basic shell.
00:05The word designer has special meaning inside Visual Studio.
00:08A designer is a special window that supports drag-and-drop and mouse
00:11interactions for creating the user interface.
00:14This designer is also responsible for generating code, or XML markup.
00:18I'm inside Visual Studio, and I have opened the solution called InfoReader.
00:23Inside the solution are two projects:
00:25version 1 and version 2 of InfoReader.
00:28I'm going to start obviously in version 1, and I'm going to edit
00:32this MainWindow.xaml.
00:34If you want to know how to create your own solution in your own project, check
00:38out some other movies in this course.
00:40This is the basic WPF Editing window.
00:43This is the designer surface on the top half of the screen.
00:46Usually on the left edge of the screen is the Toolbox.
00:50If you don't see the Toolbox, you may need to go to the View menu and turn on the Toolbox.
00:55It also may be docked to the side over here.
00:58Once you have the Toolbox there, you can then pick up an element--like this
01:02button--and drag it over to this designer surface.
01:05Visual Studio has this split view.
01:08Let me shrink down the view for a minute.
01:10I'm going to click on this Zoom to Fit button, so I can see more of the designer surface.
01:15And then at the bottom of the window you can see there is a text editor, and I
01:19can scroll this up a little bit and then shrink this down again.
01:23There is the designer, and here is the XAML.
01:25So, you can choose whether you want to do only XAML or you want to use the designer.
01:30If you want to hide the designer, you can click on this Collapse pane here.
01:34I'm going to keep them both open for today.
01:36As I size the button in the designer, you'll see I get some sizing handles, and
01:40when I let go off the mouse, you might notice that some values have changed.
01:45If I edit the XAML directly, like if I type in 100 pixels for the Height, you'll
01:49see that it changes in the designer.
01:51So they're linked to each other.
01:53Naturally, I can add other types of elements.
01:55Let me take this TextBox here and drag it over, and let go, and now in my XAML
02:01it has created this element tag here, TextBox.
02:03As I'm dragging the item around in the designer, you'll see little lines show up.
02:07Those are alignment cues.
02:09So here I can see that I'm lined up to the left edge of the button.
02:13Now if I size the button, you'll see I get an alignment on the right side when
02:16they're the same size.
02:17I'm going to make a couple of changes on the window itself.
02:21So I'm going to scroll up to the top of my XAML and look at this Window class up here.
02:26I don't want it to be this small size.
02:27I want it to be maximized.
02:29So, what I'm going to do is come over here and type in "WindowState=", and
02:35then I get a dropdown choice Maximized.
02:38If you'd rather use a designer for this, you can use the Property window over here.
02:42So what you do is you select your window, and then you go to your Property window.
02:47On your computer, you may have the Property window set to Categorized.
02:51I like to switch on the alphabetical view instead by clicking this button, and
02:54then you can scroll down to the bottom and find this property called
02:58WindowState, and then you can choose from this dropdown.
03:01The other thing I'm going to do is I'm going to change the title and not say MainWindow.
03:05I'm going to call it inpho, in.pho. readr, like so--nice modern name.
03:12So here's what I want to do for the design.
03:14I want to have the company name and logo across the top.
03:18So I need to delete this Button, hit the Delete key, and then I'm going to
03:21choose this TextBox and do the same thing, delete.
03:24What I want to have happen is I want to have a top section with the company name and a logo.
03:29So, I'm going to divide this into two rows.
03:32To do that, I select my grid somewhere-- in this case I selected it down here in
03:35the XAML. And then if you notice on the left edge and on the top is a blue area.
03:40If I click on this blue area, I will add columns or rows.
03:45So, I'm going to click over here, and then once I've got the rows in there, I
03:49can use this triangle to size them, or I can come over here and click in these
03:53items to change the sizing standard of this row.
03:58I'm going to leave it at the default for right now.
04:00Inside this area, I will put a dock panel.
04:02Dock panels are used to stick controls to the edges of the screen.
04:06So I will dock the text block to the left side, and I would dock the image to the right side.
04:12And then on the bottom half of the screen, I'm going to put a TabControl down here.
04:15I've already prewritten a lot of this, and it's down here in this
04:19InfoReader Version2.
04:20All right, so let me close this MainWindow.
04:23You may or may not see this Save dialog, and then I'm going to go down to
04:26Version2 and double-click on MainWindow.xaml. And I need to make this my Startup Project.
04:31When I choose to debug this application, I have to pick which one of these two
04:35projects to start with.
04:37So in Visual Studio to tell it which project I want to start, I right-click, and
04:41I choose Set as StartUp Project.
04:43All right, here is my newer UI.
04:46Here's my dock panel on the top right there.
04:48Here is the text block that says in.pho.readr. Here is an image.
04:53I added an image control. And then, in order to get this picture, what I did is I
04:58right-clicked, went to Add > Existing Item, went out to my Exercise Files folder,
05:07and there's an Assets folder down here.
05:10Open the Assets folder, and then make sure you're not looking at just C# files.
05:15So click on this little dropdown here and choose All Files.
05:19And now, I can add this BlueArrow.png file.
05:22Once I added that PNG file, then I can click on the Image Control and go to the
05:27Source Property here.
05:29This is really nice because when I click on this button with the three dots on
05:32it to choose the image,
05:33it shows me all of the embedded images that are part of my application.
05:37I picked BlueArrow.png and then clicked OK.
05:40Then on the bottom half of the screen, I added a TabControl--here it is--and
05:46then I added a few tabs.
05:49And then each tab has this little container down at the bottom.
05:51So like if I click here on History, I can put some items here.
05:54I'm going to put a ListBox in there for later, and then later when you'll learn
05:57about data binding we're going to load some RSS from blog feed.
06:00What I want to show you now, before I end this movie, is something called
06:03styles and templates.
06:05The idea is--let me zoom in a little bit so it's easier to see.
06:08I'm going to click on this TextBlock, and I'm going to go to its
06:11Foreground Property.
06:13Foreground is changing the color of the font by applying a brush to it.
06:17Currently, it has a black solid color brush applied.
06:20I'm going to click on this dropdown, click somewhere in the green area, and then
06:24maybe drag around here till I get the green color I want, and then I'm going to
06:27click outside the dialog.
06:29Now you see I have this green text.
06:31What I can do--let me reselect this-- what I can do now is I can go to the
06:35Foreground Property, and I can save this as a resource.
06:39So I can click on this little black diamond and say Extract Value to Resource.
06:43That creates a GreenBrushKey.
06:47I click OK, and what just happened is at the top of my XAML, it created a
06:52Resources section, and it put a SolidColorBrush in there with an identifying Key.
06:56Now I can go to other items, like say this button here or this TabControl, and I
07:01can go to its Foreground property and click in that same area.
07:06It doesn't have quite the same symbol on it, as you can see. And I'll say I want
07:09to apply a resource, and then I'll pick the resource I just created.
07:13So the power of this, especially if you create styles instead of just these
07:17stand-alone resources, is that somebody can spend a lot of time making the look
07:22of an element--let's say all your buttons--and then they can put it in a style,
07:26and then you can come in and say, "Now, just apply that style."
07:29As you can imagine, it would take a long time to build that up.
07:31So I've already written the XAML for that, too.
07:34So, if you open up this App.xaml-- this is application wide resources--
07:38if I open up this App.xaml file, double -click on it, you'll see that I have a
07:44Resources section in here. Inside the Resources, I have two styles.
07:49I'm clicking these little Plus symbols on the side here.
07:51There are two styles.
07:52Now, a style contains property setters, like that Foreground setter I just showed
07:57you, and it also can contain something called a ControlTemplate.
07:59ControlTemplates allow me to completely change the way a tab or a button looks.
08:05Now right now, the style has a key.
08:09So, if I want to apply the style, I have to know the key name.
08:12If I delete this key, I've now created a style that can be applied to all TabItems.
08:16So, I'm going to do that for the TabItem, and I'm also going to do the same
08:21thing for the button.
08:22Essentially what I've done now is I've created two styles at the global
08:26level that apply to all TabItems in the entire project and all the buttons
08:31in the entire project.
08:32Let's see what it looks like.
08:33I'm going to press F5--or you can also click the Debug > Start Debugging
08:38menu, save my changes--and now you can see that the tabs have a brand-new look on them,
08:45and so do the buttons.
08:46Here is the new button look.
08:47It even has an animation as part of the ControlTemplate.
08:50So if I hover my mouse over the button, you'll see that an animation shows up.
08:54I got these styles off the Expression Blend web site.
08:57There's a place where you can go and download styles from Microsoft.
09:00So you see how changing the template on a control can dramatically change the
09:03way it looks and behaves?
09:05That's just one small portion of what you can do with the Visual Studio designer and WPF.
09:10It's time to move on to the next topic.
09:12Up next, writing some code.
Collapse this transcript
Working with the application code
00:00It doesn't matter how good the UI designer tools are inside Visual Studio.
00:03They can't do everything for you.
00:05At some point, we must write some code.
00:07I'm going to write some code in this project called InfoReaderV3, and I'm going
00:11to double-click on this MainWindow.xaml file.
00:13So let's take a minute to look around our UI.
00:17I have a number of elements in here from the previous movie, and I'm going
00:20to work with these two buttons here: this Search Bing button and this Get RSS button.
00:25So the idea is you're going to type some text in the Search Term textbox, and
00:29this textbook has a name;
00:31it's called searchText.
00:33Names are important because in order to talk to them in your code, they have to
00:37have an identifying name.
00:38The user's going to type something in this text box.
00:41They'll click on the Search Bing button.
00:42Now, as you can see, this button does not have a name,
00:46so I need to provide it a name. And then I'll take the results from the Bing web
00:50site, and I'll put it done here in this control, which is named browser;
00:54it's a web browser control.
00:55So before I continue, I would like to name both these buttons.
00:59I will click on the Search Bing button and then go over to the Properties window
01:02and type in a new name up here at the top.
01:04I'm going to call this one searchAsRSSButton.
01:06Now, by naming this, I'll also get more readable event procedure names when I'm
01:14ready to write my code.
01:15So, now I'm going to go to this button.
01:17I'll show you several ways to add code to your project.
01:20I'm going to double-click on the Search Bing button, and Visual Studio will
01:25switch me over to the Code window, and it automatically stubs in a function with
01:30the name of my button and then _Click.
01:33The other way to stub in code--I'll switch back, I just did a Ctrl+Tab to switch
01:38back to the other item--
01:40I'll click on this RSS button, and instead of double-clicking here, I can go
01:44over to the Properties window and click the Events tab, and then I can find the
01:49event I want--in this case the Click event--and double-click here, or I can type
01:53my own name in, if I'd rather.
01:55I think I'll just double-click on this one to stub the code in here too.
01:59Now I have these two buttons.
02:01I'm going to write one more procedure, my own custom procedure, which I'm going
02:04to call from both these buttons.
02:06It's going to be a private function.
02:07It's going to return a void value.
02:10It's going to be called SendToBing().
02:15I need the two parentheses to signify that it's a method.
02:18Then I move down to the next line, and I type the two curly braces, which signify
02:22the section where I write my code inside the function.
02:24I'm going to pass in a parameter from both of these functions.
02:27The idea is if you click on the searchBingButton, then I'm going to pass in a
02:31false, because I don't want the data returned as an RSS feed.
02:34So I'm going to create a parameter down here called searchAsRSS, and then up
02:40here, I will call that function.
02:43The nice thing about the IntelliSense Engine in Visual Studio is that it knows
02:46that I've just written that function, and it shows me in my IntelliSense list.
02:48I'm going to click the down arrow on my keyboard and then press the Tab key
02:53to have it finish typing,
02:55then an open parenthesis, the word "false", and then to end the line of code in C# you
02:59use a semicolon.
03:01I'm now going to copy this, right-click and choose Copy, and then I'm going to
03:07come down here and do a Ctrl+V to paste, and then I need to change this to true.
03:12Let's review what I did.
03:13I wrote two procedures using the Visual Studio "auto stubbing in of the code" future.
03:18That's not the official name, but I just double-clicked on the button, and it
03:21stubbed in the code.
03:22Then I wrote my own custom function down here, SendToBing, and then I'm calling
03:26that custom function when I click on the two buttons.
03:29Now, you have the basic infrastructure written.
03:31In the next movie, I'll show you how to test this code and also how to retrieve
03:34information from the Bing search engine.
Collapse this transcript
Communicating with the web site
00:00I'd like to show you how to connect to web servers and retrieve information.
00:04The site I'm going to use for the project is bing.com.
00:07I'll start by looking at how Bing queries are formatted.
00:10As you can see, I have the Bing web site open and in the URL, I'm going to
00:14change the query string to show you how our queries really run against the Bing servers.
00:19I'm going to type the word "search" and then a question mark, and then the letter
00:24q= and then my search term, wpf, and press Enter.
00:29So let's examine this search query.
00:31The search means you want to do a search, the q stands for query, and then the
00:35equal sign says to assign the WPF string to the query string.
00:39If I change this to another word, say "lynda", then you see I now have a
00:45different search query.
00:46Now, one of many interesting things that Bing does that a lot of the other
00:49search engines don't do is that if you put an ampersand here, and then say
00:53format=rss, it will return your search results as an RSS feed, and then you can
00:59point your RSS reader at this feed and get a constant update for this topic.
01:03So, I'm going to use this string when I go talk to the Bing server in my code.
01:07So now I need to switch over to Visual Studio. And I'm going to come down here
01:11and double-click on the MainWindow.xaml.cs.
01:15This is the code from the earlier movie.
01:18I did make one little change here. Now, one of the things you can do in C#
01:21when you're making a call to a function, or a method as I like to say, is that
01:25you can use name parameters now in C# 4.
01:27So, this is the name of the parameter, showAsRSS, and I can use that and then a colon.
01:32I find that it makes my code easier to read.
01:35My job now is to write some code in this SendToBing function.
01:39The first thing I'm going to do in this function is verify whether the user
01:42entered a search term.
01:44You might recall that this text box here is called searchText.
01:49So, I'm going to write an if-then statement.
01:51I'm doing a Ctrl+Tab now to switch back to the other screen, and then I'm going to say if.
01:56Now I'm going to press the Tab key twice because "if" is a code snippet, and it
02:01will stub in the basics of an if statement. And then I'm going to say
02:04string.IsEmpty, actually I mean string .IsNullOrEmpty, and then a couple of
02:10parentheses here and then searchText.Text.
02:14So what this says is look at the text box, see if the user has typed something in.
02:19If it's null or empty, then I'm going to show a MessageBox and say, "Please enter,"
02:28something like that, okay?
02:29Now let's verify that this works.
02:32I'm going to compile my application, save my changes--
02:36you may not see that dialog on your machine--and then I'm going to debug the
02:39application by choosing Start Debugging, which will launch my application.
02:44Now, if I click on the Search Bing button, I get my MessageBox.
02:49If I click on the Get RSS button, I see the same MessageBox.
02:53If I type a Search Term here--wpf--and click the button, I don't see the dialog,
03:00but nothing else interesting happens yet because I haven't written the code to
03:02talk to the Bing server.
03:04So let's do that next.
03:05I return back to Visual Studio.
03:06I'm going to close this Output window on the bottom of the screen, and then the
03:11next thing I'm going to do is do is write a little bit of code here in the
03:13SendToBing function to establish a connection to Microsoft's Bing servers.
03:17To do that, I'm going to use a class called the WebClient.
03:20I'll declare a variable and instantiate an instance of this class.
03:25Now I can format a URL and send that to the web client, and I'll get a response
03:29back from that server.
03:31Next, I am going to take this code that I have down here, and I'm going to
03:36uncomment it by clicking on this Uncomment button down here, and then I'm going
03:42to cut that, Ctrl+X, and paste that in here.
03:45So, if you say show as RSS, I'm going to build this string using that Bing
03:50search syntax I just showed you a few minutes ago. And I'm going to take the
03:55data from the text box and then place it in the string right here.
03:59So you see how that equals q= and then whatever is typed in the TextBox.
04:04Then I'll send that off to the Bing servers.
04:07Now, I'm getting some red squiggles here because I need a variable to hold this URL.
04:12So I'll come up here and type in "string url;", and then these red
04:16squiggles should go away.
04:18And then down here at the bottom, I'm going to call the WebClient and say,
04:22"I would like to download a string."
04:25So, what I'm going to do is I'm going to send the information over the server.
04:28The server is going to send me back a result, and I'm going to say, "I want
04:31that back as a string."
04:33And I want to do it on a second thread.
04:35I want to do it on a background worker thread so I don't block the UI, so
04:37I'm going to choose this one here, DownloadStringAsync, and one more
04:50parenthesis right there.
04:52So this says, "Start a second thread.
04:54Go talk to the server."
04:55Here, I'm doing a little magic.
04:57I've got this plain string, but that's not enough for this.
05:00It needs it formatted to something called the URI, which is a special type in .NET.
05:04That's why I'm saying new Uri, and then I'm passing the string, the url
05:08into that Uri class.
05:10I'm about ready to go.
05:11I need to know when Bing returns the information to me.
05:14So, I'm going to add one more bit of code up here at the top.
05:17I'm going to say WebClient .DownloadStringCompleted.
05:23This event fires when the results come back from the Bing servers.
05:27So, what I'm going to do is I'm going to press the Tab key to finish typing,
05:30then the Plus sign, then the Equal sign, and then Visual Studio's going to help
05:34me write the next couple lines of code.
05:36I'm going to press Tab once, because it says press TAB to insert, and then it
05:40says press TAB to generate the wc_ DownloadStringCompleted in this class.
05:46I'll press Tab again, and it just wrote this function for me down here.
05:50This code will run when the download is complete, and all I'm going to do down
05:54here is set my browser to NavigateToString. And I'll pass it in. The piece of
06:03information we're going to get returned from the web server,
06:05it will come in this variable right here.
06:08So I'll say e.Result.
06:10I think I am ready to run this application.
06:17Debug > Start Debugging.
06:19I'll type in "wpf", and then I'll click the Get RSS button.
06:27I need some formatting.
06:30I would need to do some data binding and make sure that this gets loaded in a
06:33list box or a treeView.
06:34I'll show you how to do that in another demo.
06:37Now, let me click on the Search Bing button.
06:39I'll type in "lynda" this time. And for some reason on my machine, I'm getting
06:43these script errors.
06:44I just have to click the No button four times, and now you see my browser
06:50running inside my application is showing the entire HTML results coming back
06:56from the Bing servers.
06:59Now, we've got a working WPF application.
07:01By using the WebClient class, we reached out to the Bing web site and
07:05retrieved the search results.
07:06It would be nice to store the history of my searches in a database.
07:10That's the topic of the next movie, where I show you how to work with data
07:13and data binding.
Collapse this transcript
Connecting your data
00:00A nice improvement for our application would be to store our searches in a database.
00:05Visual Studio contains a number of useful data tools.
00:07We'll look at the Data Sources window, and the drag-and-drop binding in his movie.
00:12I'm inside Visual Studio.
00:13I've opened the Solution called InfoReaderData, which contains one project
00:18called InfoReaderV4.
00:20I've got this MainWindow.xaml file, which is a continuation from the other
00:24movies in this chapter.
00:26I'm going to take the information when you click on either of these search buttons, and
00:30I'm going to store it in the database.
00:32Then I'm going to show you your history of your searches over in this list
00:36box on the History tab.
00:38The first thing I need to do is add a database.
00:41To do that, I'm going to go to my project, right-click, and choose Add > Existing Item.
00:48Then I'm going to navigate out to my Desktop, find the Exercise Files folder,
00:53and look in the Assets folder.
00:56Be sure and change this dropdown to show All Files.
01:00For today, I'm going to use a compact SQL database.
01:05Then name of this database is History.sdf.
01:08So I'll choose this one and click on Add.
01:11Now Visual Studio realizes that I've picked a data source.
01:14It's asking me if I want to make a special type of data access tool called a DataSet.
01:18I'm going to go ahead and say Next on this.
01:21Visual Studio then queries my database and shows me a list of all of the tables.
01:27In this example, I only have one table with two columns.
01:30I will check this first check box, and
01:33then verify that I like this DataSet name down here.
01:36If I do, I'll click on Finish.
01:39A number of things happened when I click the Finish button.
01:42First of all, it made a copy of the History.sdf file in my project.
01:47It also added a connection to my Server Explorer.
01:50If you don't have Server Explorer opened, open it now.
01:53You can find it in the View menu or dock to the side of your window.
01:57You can see I now have this History.sdf connection.
02:00If I expand this, I can see the tables inside that database.
02:05I can even right-click on this History table and choose Show Table Data.
02:09Currently, I have no rows in this table,
02:11so let me put two rows in there.
02:12I'll come over to this column, and type in "WPF", and hit Enter.
02:16Then I'll type in "lynda.com". Press Enter.
02:21Then I'll close this window.
02:22Not only did it add the database, but it also generated this HistoryDataSet.xsd
02:28file, and all of its companion files.
02:30Let me double-click on the HistoryDataSet, and show you the designer.
02:35This is the TableAdapter that is responsible for loading the data into my table
02:39from the database and sending the updated data back to the database.
02:43So you see it has a Fill method.
02:44I'm going to use that to get the data.
02:47To put a new row in the DataSet, I'm going to add a brand-new query.
02:51I'll right-click and choose Add Query.
02:54Then I'll click on Next.
02:55Then I'm going to come down here and choose Insert. It adds a new row to the table.
03:00I'm going to click on Next.
03:01I'm going to go ahead and accept the defaults.
03:03This is the SQL statement that'll be run against the database.
03:07Just choose the defaults and click on Next.
03:09Then I get to name my query.
03:11Again, I'm going to choose the defaults, InsertQuery.
03:15Now when I'm finished, I click on the Finish button.
03:18You'll see at the bottom of the TableAdapter is now InsertQuery.
03:23Then the @p1 means that I have to pass some sort of parameter into that query.
03:29Now I am ready to do some data binding.
03:31Let me switch over to my MainWindow.xaml file.
03:35Switch to the History tab, and select the list box.
03:38I'm going to click on this Zoom to Fit to make it a little bit easier to see.
03:41There is a special window for drag-and-drop data binding available inside Visual Studio.
03:47It's in the Data menu.
03:48It's called Show Data Sources.
03:50I'm going to go ahead and click on this stick pin, the Auto Hide button.
03:54On my machine, it docks itself to the bottom left-hand side of the window.
03:58Notice that my DataSet is showing up here, and the table that's inside my DataSet
04:02is showing up there as well.
04:04Now for drag-and-drop data binding, I can pick this table up and drag it over
04:10and drop it on my list box.
04:12Before I do though, let me point out my XAML that I have down here.
04:16It's only got a few items in here.
04:18Keep an eye on that when I drag this over and drop it on the list box.
04:23You see what happened is that Visual Studio added three new lines.
04:27It wrote a DisplayMemberPath, what to show;
04:30it'll show this column called SearchText.
04:32It shows that it's bound to a source.
04:36This is the WPF and Silverlight way to set up bindings.
04:40This tells WTF to monitor the underlying source and load this list box with
04:45whatever data is inside this DataSet.
04:47Then I also have a SelectedValuePath for this field here, HistoryID.
04:53That's in case the user selects an item.
04:55I can use this as the identifier of the row that they selected.
04:58I'm not going to be using that in my demo.
05:00I'll choose Debug > Start Debugging, save my changes.
05:05Then I'm going to click on the History tab.
05:08There, I can see my two items from my database.
05:12So I've successfully set up a data binding.
05:14The next part of the demo will be when the user types in a string here and
05:18clicks on of these buttons,
05:19I want to add it to the database and make sure that this list box updates.
05:24To do that, I'm going to switch to the code behind.
05:26While I'm inside my designer,
05:28I can press F7, which switches me over to the Code view.
05:32Now when I did the drag-and-drop binding, Visual Studio wrote a lot of code in
05:37the Window_Loaded event.
05:38It's very hard to read.
05:40So I'm going to replace this with a slightly more readable version.
05:43I'm going to delete the code here, by pressing Delete.
05:46Then I'm going to take this prewritten code here and
05:50cut it and paste it right above the Window_Loaded event.
05:55Then I'm going to uncomment the code by pressing Ctrl+K, Ctrl+U. This is going
06:01to create an instance of the DataSet.
06:03It's also going to create an instance of that TableAdapter that knows how to
06:06load and update and insert my data.
06:08I'm going to do that here because I'm going to use it elsewhere in my code.
06:11So I'm doing it outside the boundaries of this Loaded event.
06:15Then I'm going to take this commented code down here and cut it, Ctrl+X, and
06:20paste it in the Loaded event.
06:22Then uncomment it, Ctrl+K, Ctrl+U. What this does--
06:28now, I see I have an error here.
06:31I'll click here, and then do a Ctrl+Period.
06:35Get the smart tag, and then say, using System.Windows.Data.
06:38That will fix my code.
06:40What this does is it looks in my XAML, and finds something that was created by
06:44Visual Studio called HistoryDataSet.
06:46Then it creates a TableAdapter, instantiates one, fills the data set, and
06:52then sets up something special called a CollectionViewSource, and then moves to the first row.
06:56When I call the SendToBing method, I'm going to insert a new row by calling
07:03the ta.InsertQueryp--
07:05that's the query we built earlier today.
07:07We're going to pass in the string from the text box, the search string.
07:11Then to make sure that it updates correctly,
07:14I'm going to call the ta.Fill, which refills the data and then rebinds to the list box.
07:21At this point, I think I'm ready to run the application.
07:23I'm going to press F5 to debug the application. Then click Yes.
07:31Verify that I only have two items in the list box.
07:35Then I'll come over here and type in Silverlight and click the Get RSS button.
07:44There is my RSS coming back from Bing.
07:46When I click on the History tab, my new item has been added to the list box.
07:51If I were to look inside the database, I'd also see that the data is stored in the database.
07:55Now there is one more type of data binding I want to talk about in this chapter.
08:00That is called XML binding, and it's the topic of the next movie.
Collapse this transcript
Binding to an RSS feed
00:00RSS is a very popular format.
00:03In this movie, I want to show you how to bind your application to an RSS feed.
00:07I'm inside Internet Explorer, and I have my blog opened.
00:10It's blog.wpfwonderland.com.
00:13If I go to the end of my URL, and I type in the word "feed" and press Enter,
00:19I get the RSS feed view of the data,
00:23but it's still not the raw XML data that's coming from my server.
00:26What I should do to get the raw feed is right-click and then choose View Source.
00:31I've already done that.
00:32I've put the file inside our project, inside Visual Studio.
00:35So let's switch to Visual Studio.
00:37Here is the feed file I'm talking about, BlogExampleFeed.xml.
00:41I'll double-click on it to load it into Visual Studio.
00:44Then I'm going to make my font a little bit smaller by using the mouse wheel and the Ctrl key.
00:50If I would like to bind part of my UI to this data, I need to figure out how to
00:55get down to this XML element.
00:58You can see that the description is a child of channel, and channel is a child of RSS.
01:03There is a mechanism called XPath built into XML that lets me drill into this
01:07data using these tags.
01:09So that's what I'm going to use.
01:10I'm also going to use something called an XMLDataProvider, which comes from WPF.
01:14Let me switch to my main UI for one minute by double-clicking
01:19on MainWindow.xaml.
01:21Then click on the Your Blog tab.
01:24Then I'll click on this text block that's right here in the UI.
01:28Let's see what's happening here on line 82.
01:31The text for this TextBlock is going to come from a data source.
01:35In WPF, the binding keyword is how we tell the WPF engine to get the data from
01:41this title element and put it in here on the Text property of the TextBlock.
01:44The curly braces are also part of the syntax for WPF and Silverlight.
01:49Notice that this TextBlock is bound to the description.
01:51Notice some dot dot stuff going on here, because I'm going to start in my data
01:57by looking at the individual items.
01:59These are the individual blog posts here.
02:01So I'm going to start my XPath by starting here.
02:04Then I'm going to go up a level to get to the description.
02:08I need to write some code to set up this data binding.
02:11So I'm going to switch over to my MainWindow.xaml.cs file.
02:15Then I'm going to scroll down until I find this section down here where it says
02:19private void Window_Loaded.
02:21First, I need to instantiate the XMLDataProvider.
02:26I'm doing it outside of the Window_ Loaded event, so I can use it elsewhere in my code.
02:30Then after the move current to first bits of code, I'm going to tell that
02:36provider where to find my blog.
02:38So here I've typed in my feed location.
02:44Next, I'm going to tell the provider where to start looking inside that document,
02:50using an XPath query.
02:51So we start with the root element, RSS.
02:54Then we go to channel, and then it's item.
02:57That's where I want to start looking.
02:59I can go up and down from that location in my bindings.
03:03Then lastly, I need to tell WPF to create a DataContext.
03:07So I'm going to go to the tab where all my RSS controls live, called tab4.
03:14I'm going to set the DataContext equal to my provider.
03:18Now what this says is any WPF element that is inside this tab has access to the
03:25data coming from this XMLDataProvider.
03:27Then I'm going to press F5 to run the application. Save my changes.
03:33Then I'm going to click on the Your Blog tab, and I was successful.
03:37I'm pulling the data from my blog, loading it into this list box.
03:41When I click on an item in the list box, it's loading the actual blog article in
03:44over in this area.
03:45I'm also pulling information about the blog name and the blog description and
03:50putting in these text blocks.
03:51I showed you how that was happening in the WPF.xaml earlier.
03:55There is one more thing I would like to make this application do.
03:58I'd like to go to the search feeds, and
04:00when you type in a new search, I'd like to go get the RSS from the Bing site
04:06and pass that in as the source for this page.
04:09It's relatively easy to do.
04:12I just need to go up to this area where I'm downloading the string from Bing.
04:16I'm going to say provider.Source.
04:20I'm going to change from my blog source to the new source.
04:24I think that code should do it.
04:28Debug > Start Debugging, or F5.
04:31I will come over here and type in my new search "Databinding",
04:36click on the Get RSS button, and
04:39then I'm going to on the Your Blog tab, and see if I've pulled the data from Bing.
04:45So these are the top ten sites from the RSS feed that have the word "databinding" in them.
04:50There are plenty of details I'm leaving out about databinding in this overview.
04:54You'll find more material about binding elsewhere in this course.
04:57At this point, our application is nearly complete.
05:00We just need to determine how to deploy it to the end user's computer.
Collapse this transcript
Packaging and deploying the application
00:00You have made it to the last step in creating the simple RSS application.
00:04You built and tested info reader, and are ready to deploy it to your
00:07customer's computer.
00:08I'll show you two techniques to deploy your application.
00:11Before we ship our application, we want to switch from Debug build to Release
00:15build. This will ensure that our code is optimized.
00:18To do that, I need to be inside Visual Studio. So, I have opened the Visual
00:20Studio and have opened the solution called Info Reader, which has a project
00:26called InfoReaderVFinished.
00:28To Release mode, I go to this dropdown here and choose Release.
00:34Now when I click Build, it's going to take and compile the application and put it
00:40in a Release-specific folder. I can see that folder by going out to my hard
00:45drive, right-click, choose Open Folder in Windows Explorer, and all of my compiled
00:53code lives in this folder here, the bin folder, which stands for binary. And down
00:58here, of course, is where my Release build is.
01:00There is my exe, the most important file for this application, and this is my database file.
01:06So what I am going to do is I am going to copy this Release folder and put it
01:11somewhere my hard drive.
01:12This is what your user would do.
01:14They would take this folder, put it somewhere on their computer--let's say their
01:18desktop--and then they will double- click on this InfoReaderFinished.exe, and
01:27there's our application.
01:29Let's see if works.
01:29I will type in "RIA" and choose to Search RSS, check the blog section to see if
01:36it's working correctly--it looks like it is--and look at my history. Excellent!
01:40It's working. So, really, it's that simple to deploy.
01:44The user can change the name of the folder,
01:46they can move it to a new location. It just works.
01:48Of course, there are some drawbacks to this simple matter of deployment.
01:52If you upgrade the application, you would have to make sure that each user gets
01:55the newer copy. Also, the simple copy doesn't add any desktop icons or shortcuts
02:01to the Start menu. Visual Studio provides a normal Windows installer and a Click
02:05once installer that address these issues.
02:08I'd like to show you how to create a Clickonce installer.
02:10I am going to switch back to Visual Studio, and then I am going to double-click
02:13on this Properties node in the Solution Explorer.
02:16Next, I'm going to go down to the Publish section, and then I'm going to click
02:21on the Publish Wizard.
02:23I have many more details about how to create Clickonce applications in another
02:27section of this title.
02:29For now though, I am just going to click the Publish Wizard.
02:31This is the location on my hard drive where the files will be published, a
02:35folder underneath my project. I will click Next.
02:38I will choose to install from a CD-ROM, and that my application will not check
02:44for updates, and then I am finished.
02:45I click the Finish button, wait about two seconds, and then Visual Studio
02:50compiles my application, publishes the application, and then opens up Windows
02:54Explorer and shows me the files.
02:56Here is the file I would give my user, and then they would double-click on the
03:00setup.exe. And Clickonce sees that I have not digitally signed my executable, so
03:07it's telling the user, "I can't verify this publisher. Are you sure you want to
03:11install this application?"
03:13They're going to click on Install.
03:14Now, here's the application.
03:16This application has a shortcut in the Start menu.
03:20Now before I compiled this, I should've added our company name and our publisher
03:26information or web site. I didn't do that, so I got the default values.
03:29But let's go find out where it's stored.
03:31If I look into Start menu, here is my Info Reader.
03:34I can click on this link to run it.
03:36It's also stored in All Programs. Because I used the default, it's currently stored
03:43in the Microsoft folder. There it is again.
03:47Since it was a regular install, it also is un-installable by the user if they go
03:53to Features and click Programs and Features, and this shows all the installed
03:58applications, and my Info Reader application is down here.
04:03It shows the date it was installed and who the publisher is.
04:06Now the user can go and can use Uninstall or Change, choose to remove
04:11the application, and now all traces of this application are gone from their computer.
04:15That's the last topic in his chapter on building a simple WPF application.
04:19We created a user interface, wrote some C# code, and determined how to use
04:23data within the database, and read RSS feeds from the Internet.
04:27Just when you thought it couldn't get any better, we had the big finale.
04:30We created a simple Clickonce deployment application.
04:33Here's a tidbit for you before I go:
04:35you will find a lot of details about each of these phases elsewhere in
04:38this course.
Collapse this transcript
4. Exploring the Code Editors
What languages are supported in Visual Studio 2010?
00:00Here's an amazing fact. The .NET framework has over 140 languages that can be
00:05used to write applications.
00:07You heard me correctly--140 languages. Visual Studio contains about 14 built-in editors.
00:15A code editor is a window that lets you write code. A good editor assists you by
00:20including autocompletion tools, syntax checkers and re-factoring tools.
00:25There are editors in Visual Studio for all the major Microsoft languages.
00:28C# and Visual Basic are two of the most popular .NET languages, and both
00:33have superb editors available.
00:36If you are a C++ programmer or are interested in the new F# language, you
00:40will find editors for those languages too.
00:42Visual Studio is extensible, so third parties can create Editors and plug them into the IDE.
00:48Microsoft has taken that route and created editors for their IronPython
00:52and IronRuby languages.
00:54On this screen is a list of all of the default editors included in Visual Studio.
00:58One of my favorite editors is down near the bottom, the XAML Editor.
01:04That's the one I use when I am creating WPF or Silverlight applications.
01:08I don't know about you, but I think using a language is more exciting
01:12than talking about it.
01:13So let's move onto the next movie, where I will show you how to work with
01:15the code editors.
Collapse this transcript
Exploring basic settings for the Code Editor
00:00As a programmer, you spend a sizable portion of your workday writing text. Oh, sure.
00:05We call it code, but think about it for a minute--
00:07your code is just specialized text.
00:10And you probably write other text documents too. For example, HTML and XML files
00:15are common in software projects.
00:17So it won't surprise you to learn the Visual Studio has lots of text editors.
00:20In this movie, I want to show you some basic settings that you can configure
00:24for any text editor.
00:26I'm inside Visual Studio, and I have opened the BasicEditor project, and then
00:31I've open the Program.cs file.
00:33I am going to start by showing you some navigation aids along the left edge of the screen.
00:39I am going to edit them by going to Tools > Options.
00:44You need to scroll down in Tools > Options till you get to the Text Editor
00:48section. Here you can see all of the different editors that are available inside
00:52Visual Studio. For example, down here is the XAML editor. Up at the top is some
00:58General settings, and that's where I am going to start.
00:59I am going to start by looking at these two items here: Selection Margin
01:03and Indicator Margin.
01:05Selection Margin displays a vertical margin along the left edge of the
01:09editor's text area.
01:10You can then click in this margin to select an entire line of text, or click and
01:14drag to select consecutive lines of text.
01:17Right now, it's enabled,
01:19so if I click on OK, I can go in my code window, right along the edge here,
01:25and then hold my mouse button down and slide down the page to select multiple lines.
01:30I can disable that, and then it will just collapse that section down. Next to the
01:33Selection margin is the Indicator margin.
01:35It displays a vertical margin outside the edge of the editor's text.
01:39I can click here to add things like breakpoints.
01:42This is also where we see symbols for things like bookmarks.
01:45Let me come down here and click right in this area, and you can see there is a
01:50red ball that shows up there.
01:51That's a breakpoint, and I'm able to turn that on and off by clicking here in
01:55the indicator margin.
01:58These are on by default, both these settings.
02:01One of my favorite features in the editors is line numbering, so let me show you
02:05how to enable at that. That is off by default.
02:07I am going to go back in the Tools > Options, and then I am going to come down
02:10here and click on All Languages.
02:12This is going to affect all editors in Visual Studio.
02:15I am going to click here and choose Line Numbers and then click OK.
02:22And that's easy, self-explanatory, I now have line numbers in every editor.
02:27I find is really handy when working in code reviews.
02:30Another less popular feature, but one that I like nonetheless, is the ability to do word wrap.
02:36I am going to write a little bit of code here in a comment. I'm going to select it
02:45with my keyboard, do a Ctrl+C, and then paste in another line or two here. And as you can
02:52see, the text scrolls off the right edge of the screen.
02:56Now, a lot of editors that you work with-- like for instance Microsoft Word--have
03:00the ability to wrap that line down to the next row, and Visual Studio has that setting too.
03:05I will go to Tools > Options > All Languages, and then I am going to click here, Word wrap.
03:15I can also put check mark here where it says, "Show visual glyphs for word
03:18wrap." What that does is it puts a little indicator icon on the right edge of the screen.
03:22Let me show you what I mean.
03:23I am going to click OK. Notice now that line 15 as a blank here and then line 16.
03:31So this is considered to be all of line 15, even though it crosses two rows the
03:36editor. Over here on the right it is this little indicator glyph that says well
03:40you have moved on to the next row.
03:42If I make the window narrower, you see that it adjusts the text to fit on the second row.
03:47I'll make the window a little bit wider for the next demo, and then I am going
03:53to go up, and we are going to talk about a couple of other features. Tools >
03:57Options > All Languages.
04:02At the top of the window is an autolist member and a parameter information.
04:07These are on by default, and I suggest you leave them on. Autolist members means
04:11that when you're typing your code, it'll automatically give you a dropdown list of
04:15all the coding items that are in scope at the moment.
04:18Parameter information is used when you're writing a method call and the method
04:22has multiple parameters, you can get a tooltip that will pop up in your window, showing
04:26you information about that particular parameter.
04:29At the bottom of the page is something called Navigation bar.
04:32Navigation bar turns on this top area in your Coding window. So if I
04:38unselect this and go back into my editor, you will see that those little
04:42dropdowns have disappeared.
04:44Those are interesting for navigating through your code.
04:46Let me show you how those work.
04:48Tools > Options and select the Navigation bar check box and then click OK.
04:55How this works is this dropdown shows me all of the entities like the enumeration
05:01and the class that I have in my code.
05:03So I can click here and go to my file type enumeration down here, or I can go
05:08back to my program. And then when I am in this class, this class has two
05:13functions: Main and Save Files. So I can go over to this dropdown and use that to
05:18navigate back and forth between the Main and Save File function.
05:22Naturally, in a real application we have more lines of code than this.
05:25This is a much more useful feature.
05:28Now that we have seen the general options available for text editors, the rest
05:31of the movies in this chapter focus on editors for each specific language.
Collapse this transcript
Writing a C# program
00:00C# is a very popular programming language for writing .NET applications.
00:04Many people consider it the de facto language for .NET, and rarely consider using
00:08the other in-box languages.
00:11For most of the examples in this course, I will be using C#,
00:15so this is a good time to explore the basics of the C# language and its code editor.
00:19I'm inside Visual Studio, and I've opened this project called CSharpEditor.
00:24I'm going to double-click on this Program.cs file to open it up in the code
00:28editor, and then we're going to start writing our code here on my machine on
00:32line 11 in this static void Main method, which, since this is a console
00:37application, is considered the starting point of the app.
00:40I just pressed the Enter key to enter a new line of code.
00:42You'll notice that on the left margin there's a yellow marker over there now.
00:47That signifies that I have made a change since I've opened this file, but I
00:51haven't saved the changes yet.
00:58If I save the file by clicking on this Save button up here on the toolbar,
01:03you'll notice that it changes to a green bar.
01:05That signifies that I have made some changes since I opened the project, but I
01:10have currently saved them to the hard drive.
01:12So it's a status symbol.
01:14I'm going to write some code to read and write from the console.
01:17I'm going to start by writing "hello" to the console.
01:20I'll type in the word "Console".
01:23Notice that I get IntelliSense;
01:25I get this dropdown window.
01:27I have more details on IntelliSense in another movie later in this title.
01:31And then I'm going to press the Tab key to finish typing.
01:33Then I'll type dot, and then, the word "WriteLine".
01:39I've got enough of the word WriteLine written now,
01:43so I can just press the Tab key again.
01:45Then the open paren, close paren, and the semicolon.
01:49In C# you always end your lines of code with your semicolons, and you always
01:54write your code within curly braces.
01:56What do I want to write?
01:59I need to put a string in here, so I'm going to say "Hello".
02:03If I come up and I run the application at this moment by saying Start Debugging,
02:08the application will start and run, and immediately stop.
02:13It ran so fast you might not even have seen that flash on the screen.
02:17I need to wait to see the results of the application.
02:20So, Visual Studio provides this mechanism, Debug > Start Without Debugging.
02:25Let's see the difference.
02:27I'll say Start Without Debugging.
02:29It prints my application.
02:30My application is now terminated, but it leaves it on the screen so that I can
02:34see the end results.
02:35When I'm done looking at the end results, I can press any key on my keyboard,
02:39and it'll then terminate that window.
02:41If I'd rather not run using Start Without Debugging, I can write my own line of
02:44code to listen to the users keystrokes by typing in "Console.ReadLine". And what
02:53this does is it stops, waits for the user to type something in the keyboard, and
02:58then press Enter before it continues.
03:02Let me modify this a bit. "Hello.
03:05What is your name?"
03:09Then on my ReadLine, I'm going to declare a variable to hold the user's name.
03:13I'm going to do that here on line 15 on my computer.
03:16I'm going to say "string name =", and then I'll copy this code; Ctrl+C. Paste it
03:28in; Ctrl+V. So, what this is going to do it's going to say "Hello.
03:31What is your name?" and then I'm going to read from the Console whatever the
03:36user types, and then I'm going to wait for them to type something else in.
03:41That way, the application won't end immediately.
03:44And then down here, I am going to output whatever the user typed in back to the console.
03:50Now, down here, I'll say "Console.WriteLine", and then I need my semicolon at the end.
04:03So, what am I going to do here?
04:04I'm going to say "Hello", and then this is the concatenation operator, meaning
04:12I'm going to take one string from the user and I'm going to add it to the end
04:16of the other string from my code.
04:17I'm going to say "Hello "+ name.
04:21I think that's good; let's try it out.
04:23Debug > Start Debugging. It says, "Hello.
04:27What is your name?"
04:28I'm going to type my name in, "Walt Ritscher", and then press the Enter key, and
04:33then I see my name mirrored back to me, "Hello, Walt Ritscher."
04:36It's now waiting for me to press the Enter key to terminate the application.
04:42I showed you one way to create a variable.
04:43There's another way to create a variable that's very popular now in C#.
04:46You can use the var keyword.
04:48The var keyword says to the compiler, "I'm going to declare a variable," demo, "and
04:54I want you to figure out the type of the variable."
04:56So, if I say var demo = 6;, that's telling the compiler to look at this number 6.
05:03Let me hover my cursor over it. And that is an Int32, which means it's a 32-bit
05:08integer, and I want you to make this variable of type Int32.
05:14This is the same as me typing in "Int32 demo" like that.
05:22These are equivalent lines of code.
05:24I'm a personal fan of using the var keyword.
05:27I like using it a lot.
05:30Sometimes you're going to work with code that has long names.
05:33Let me show you another example:
05:34var fs = new System.IO.FileStream();.
05:43I'm going to press the Tab key to finish typing here.
05:46So again, I'm asking to the compiler to figure out the data type of this variable.
05:49It's going to look at the right side of the assignment operator and see that I'm
05:53asking for it to make a new instance of the FileStream class.
05:58So it's going to make this variable type FileStream.
06:00There is a way to limit how much typing I have to do.
06:03I can eliminate having to type this namespace at the beginning of the word
06:07"FileStream" by going to the top of my code, up here at the top of my Code window,
06:11and typing in what's called a using directive, "using System.IO;".
06:19Now, down here in the body of my code, I can eliminate the word "System.IO".
06:24So this cleans up my code quite a bit and makes it a little more readable while
06:28I'm writing my application.
06:30Now you've seen the basics of reading and writing information to the console.
06:33C# is a powerful language and a perfect companion to working in .NET.
06:38This is not a C# tutorial, however,
06:40so if you are new to the language, you will need to dig into the documentation.
06:44Be sure and check lynda.com for other movies related to this topic.
Collapse this transcript
Writing a VB program
00:00Visual Basic is the other leading language for programming .NET.
00:04You may know it by its VB acronym or as Visual Basic .NET, but it's officially
00:09known within Microsoft as Visual Basic.
00:12C# and VB share a lot of features and are more similar than different in .NET 4.0.
00:18According to my sources at Microsoft, these two languages are tremendously
00:21popular and account for the highest percentage of .NET programmers.
00:25In this movie, I want to show you some basics of the VB language and its editor.
00:30I'm inside Visual Studio, and I have open up this project called VBEditor.
00:34I have one code file here called module1.vb.
00:37The vb extension signifies that this code file contains Visual Basic code.
00:42I'll double-click on Module1.vb and load it in the editor.
00:47In Visual Basic when you're working in a console application, you have to specify
00:51a Sub Main, a main entry point for the application.
00:55So you call it Sub Main, and you put it inside a Module.
00:59I'm going to write some code to read and write information out to this console window.
01:04I'll start by writing a piece of information to the console. To do that, I
01:08use the Console Class.
01:10Notice that dropdown IntelliSense I have get here inside Visual Studio is
01:15showing me the available types I can work with.
01:18At this point, I've typed enough for Visual Studio to recognize the word "console",
01:22and I can just press Tab to finish typing.
01:25Then I'll type in period and then Wri, there I find a recognition for Write and WriteLine.
01:34I am pressing the up and down arrows on my keyboard right now to get this.
01:37Now I am going to press the enter key while WriteLine is selected.
01:40Visual Basic knows that this is a method call, so it puts the open and close
01:45parentheses automatically on that line.
01:47This is something that VB does that the C# editor does not do.
01:51This would write a blank line.
01:53I'd rather write something out of the console so I am going to come up to this
01:56line and type something in the parentheses.
01:59I am going to type in, "Hello.
02:03What is your name?"
02:07To end a line of code in VB, you just hit the Enter key. Nothing special is
02:11required, like the semicolon in C#.
02:13So I am going to move down to the next line, and I am going to show you what
02:16happens when I run the application at this point.
02:19If I'm inside Visual Studio and I go up to Debug > Start Debugging, the
02:24application is going to run, my code will run, and then immediately terminate,
02:28so don't blink while you watch this.
02:30Did you see that window pop up?
02:35I don't want that to happen.
02:37I'd rather wait and see what the results are, so there's a mechanism inside
02:40Visual Studio to make that happen.
02:43It's up here in the Debug > Start Without Debugging.
02:46Let's run the application again using this menu item.
02:50You see the difference?
02:52This time the application runs, asks me what my name is.
02:56My application is now terminated, but Visual Studio keeps the Console window
02:59open so it can read the results.
03:02Then I press any key on my keyboard to close this window.
03:05I'm going to press the Spacebar.
03:06I would rather have my own code listening to the keystrokes, so I am going to
03:11come down to line 9 on my computer and type in "Console.ReadLine".
03:22That reads whatever the user types on the console and puts it in a variable. Well,
03:26I don't have a variable yet so let's make one.
03:29In Visual Basic, you use the Dim keyword to declare a variable like this, "Dim
03:36name As string". Enter.
03:41I am going to copy this Console. ReadLine, paste it down here, Ctrl+V, and
03:50then I am going to say on line 12, on my computer "name =".
03:54So let's review what's happening.
03:57I'm writing this string to the console and declaring a variable, I am asking the
04:03user for their name, and I am storing it in this variable.
04:07Then I'm going to write again to the console. Choose copy.
04:13I'll come down here under line 13.
04:16I am going to paste it in, Ctrl+V, and then I am going to concatenate this
04:25variable on the end.
04:26Concatenation just means taking two strings and putting the second string on the
04:31end of the first string.
04:32So I am going to say Hello and then ampersand, which is the concatenation
04:37operator in Visual Basic, name, and then I'll wait for the user to close the application.
04:45Let's try this one. Debug > Start Debugging.
04:50I type in my name, "Walt Ritscher", hit the enter key. It mirrors that back to me
05:00Hello Walt Ritscher, and then it waits for me to press the Enter key to
05:03terminate the application.
05:06Next, I am going to show you how to declare a different type of variable.
05:09I am going to say Dim fs As New System.IO.FileStream.
05:20I am going to press the down arrow to move to FileStream and then press the Enter key.
05:25What this is saying is, "Declare a variable and instantiate an instance of this
05:31class, the FileStream class and store the instance in this variable."
05:34Now to simplify my life, I can eliminate typing in System.IO, so I'm going to delete this.
05:42Notice I get a blue squiggle at this moment because it doesn't recognize the class name.
05:48I will then scroll up to the top of my Code module and type in an import
05:54statement up here, "Imports System.IO". Make sure you spell it correctly.
06:02Now look at line 18 in my code. That blue squiggle that was under FileStream has disappeared.
06:08That signifies that it recognized that as a valid class.
06:12Putting this Import at the top of my code simplifies the body of my code document.
06:16So what I have showed you so far today is how to read and write from a
06:21console in Visual Basic.
06:23Obviously, teaching VB would take a course of its own.
06:25You will see more examples of it later in this course.
Collapse this transcript
Working with C++
00:00C# and Visual Basic are the most popular languages in .NET, but Visual Studio
00:05caters to other language enthusiasts too.
00:08Today I want to look at the support for C++.
00:11I'm inside Visual Studio, and I've opened a solution called CPlus.
00:15Inside that solution are two projects:
00:18ExploringCPlusClr and ExploringCPlusWin32.
00:24There are a variety of different kind of C++ project types.
00:27Let's go take a look.
00:29File > New, and then I'm going to choose the Project menu. And then I'm going to
00:34scroll down to the C++ section.
00:38You can see that there is a CLR Console Application and a Win32 Project.
00:46If you are familiar with ATL or MFC, there are those project types in here as well.
00:52I've already added the projects to my solution, so I'm going to click Cancel at this point.
00:56I'm going to start by looking at the ExploringCPlusWin32 project.
01:03This is an unmanaged C++ project.
01:06It's not my startup project, however.
01:09I can tell that because it's not in bold.
01:11So I'm going to right-click and choose Set as StartUp Project.
01:16That way when I run the application, I'll be sure that my code runs.
01:22Next, I'm going to open this node and find the Source Files folder and then open
01:27the ExploringCPlusWin32.cpp file.
01:32The .cpp extension means that this file contains C++ code.
01:36I will double-click on the file and then show you the code.
01:40I need a starting point for my application for this console application.
01:45That's called _tmain.
01:48Within that function, I'm going to write out to the console, and I'm going to
01:53read some information in from the console.
01:55To do that, I'm going to use the cout and the cin keywords.
02:02cout does what it implies; it's console out.
02:06And I'm using this insertion character here to take this string and insert it
02:11into the output stream.
02:13I'm also going to end the line with the special "endl" keyword.
02:18An alternative way of doing this would be to concatenate like this.
02:22This is a longer sentence and then by using the \n character within the string,
02:28that's also the equivalent of new line, so I don't have to use endl.
02:33Line 18 tells the application to wait for user input, to pause until they
02:39press the Enter key.
02:41Next, I'll declare a variable. That's easy to do.
02:44The data type of the variable is int and then the name of my variable is age.
02:49All lines of code in C++ end with semicolons.
02:53Then I'm going to output this string, and then on line 22, this is going to
02:58take whatever the user types and push it into this variable, using the
03:02insertion characters.
03:06Next, I'm going to declare a variable of type string called fullName.
03:10And there are a couple of ways I can get the information.
03:13One is to use this fflush function and pass it these standard input buffer, or
03:20if I'd rather, I can use this function down here called getline.
03:25Getline takes two arguments:
03:26it takes this input stream from the console, and it takes a variable that I
03:30want to store the data in. And then getline calls cin and pushes the data into
03:35this fullName variable.
03:39Lastly, I'll concatenate them all into a single output.
03:43Would you like to see it running? Good!
03:46I'll go up to Debug > Start Debugging.
03:49Visual Studio realizes that I haven't built this application recently, so it
03:54asks me if I would like to build it.
03:55I'm going to say Yes.
03:59As you can see, there are my two strings output to the console.
04:02It's waiting for me to press a key.
04:05Next, it prompts me for my age, and then my full name, and finally it outputs
04:14that to the console and then waits for me to kill the application by
04:16pressing the Enter key.
04:19Next, I would like to show you the managed version of this, the CLR version.
04:25It's much easier to program, and it's more like C#.
04:28Because it uses the .NET libraries, it's easy to use the thousands of .NET types
04:32that exist in the .NET assemblies.
04:34So I'm going to show you that code.
04:37I'm going to right-click on this project and make it my startup, Set as StartUp Project.
04:44And then I'm going to expand this node, find the Source Files folder, and then
04:49double-click on this cpp file right here, the ExploringCPlusClr file.
04:55Like the other application, I need a starting function.
04:58Here, it's called main.
05:01And then you'll notice that I'm going to be working with .NET classes.
05:06The .NET class I want to work with is Console, and the method, or function, that I
05:11want to call is called WriteLine.
05:13If you're already familiar with C# or Visual Basic, this should look vaguely familiar.
05:17The main difference is you use the colon, colon between the class name and the member name.
05:23I'm also using a prefix here in the string, the L prefix on the string.
05:28So what am I doing in this code?
05:30I'm writing a prompt the user on line 11, and then on line 12, I'm declaring a
05:36variable named fullName, and the datatype of that is the string.
05:41Then I'm prompting the user, and then I'm reading from the console and pushing
05:45the data into this fullName variable.
05:48Once I'm done with that, I will write once again to the console, and I'm
05:51going to do a concatenation operation, putting the two strings together, and
05:55then a blank ReadLine.
05:57Would you like to see that running? Okay, here we go.
06:00Debug > Start Debugging.
06:03Once again, Visual Studio prompts me to build the application. I'll choose Yes.
06:10And there's my prompt and your full name.
06:15And there's my concatenation to the output screen.
06:17I'll press Enter one more time to kill the application.
06:21As you can see, there's still a place for C++ in Visual Studio.
06:25Granted, languages like C# are easier to use and generally more popular with the
06:30current generation of developers, but if C++ is your language of choice, you
06:34should be happy to see it living proudly in Visual Studio.
Collapse this transcript
Working with F Sharp
00:00F# is a new language that ships with Visual Studio 2010.
00:04It is based on the functional OCaml language.
00:08F# is largely a functional language, though it has traces of object
00:11orientation and imperative languages too.
00:14Writing safe concurrent programs has become a concern recently, as multi-core
00:18CPUs have become widespread.
00:20Functional language helps support concurrency by encouraging the use of
00:23immutable data structures that can be passed between threads and machines
00:27without worrying about thread safety or concurrency.
00:30I'm going to show you a brief demo of the FSharpEditor here inside Visual Studio.
00:33I have already opened a project that called FSharpEditor.
00:36I'm going to double-click on this Program.fs file.
00:42You might guess that FS stands for FSharp. That means that this file
00:46contains F# code.
00:47So I'll double-click, and this is the editor.
00:51I'm going to use the printfn function to print information out to the console.
00:57I'm also going to use this #light compiler flag, which makes it a little bit
01:04easier to write the F# code.
01:07This means I don't have to end the line of code with the colon, colon syntax,
01:12which is the normal way of doing that.
01:13On the other hand, it means that indentation takes more significance.
01:18The way you indent your code is more important when used the #light switch.
01:22So I'm going to use the open keyword to resolve namespaces, so that I can do
01:29things like a System.Console.WriteLine.
01:32This means I could leave off this system prefix if necessary.
01:35It's kind of like the import or using statements in Visual Basic and C#.
01:38I'm going to print out of the console the word Hello, or I can use the
01:44System.Console.WriteLine mechanism for doing that. That's familiar.
01:48Again, if you're comfortable with .NET classes, this code should look familiar to you.
01:52Next, I am going to declare a variable.
01:54So the let keyword allows me to that in F#.
01:58There are some differences.
01:59Variables are immutable in F#, which means that once you declare the
02:03variable and you load the initial data into it, you can't change it anymore.
02:07But that also makes it the thread-safe.
02:10So on line 21 what I am doing is I'm declaring a variable called age.
02:14I'm doing a mathematical addition there: 34 + 2.
02:18Notice that I don't have to put anything special on the end of the line here
02:21because of that #light compiler flag I have up there.
02:25Then once this variable is declared, I can use it.
02:28Now if I try to do something like age = 80, I'll get a blue squiggle.
02:33I'm getting that kind of useless message here that says the expression should
02:37have a type of 'unit', but it has a type of 'bool.'
02:40Basically, it's saying you can't assign another value to this.
02:43If I try to use the let keyword, I'll get a different squiggle.
02:48This time it's telling me I have already declared a variable called age, and you
02:52need to come up with a new name.
02:54So let me comment this out, and we'll continue.
02:57So next, I write out, "Enter your full name." And then I'm going to use the
03:02Console.ReadLine to read the contents and store them in this variable.
03:07Then I'm going to print them out.
03:09Then I'm going to use this %s as a token, and this %d as a token to say, take the
03:14full name here and place it within the string where this token is.
03:19Then also do the same thing with age and place it here.
03:23All right, does that make sense?
03:25Let me show you one another feature that a lot of people really like about
03:30F# is that functions are first-class citizens.
03:34You can declare them in line and then pass them around as arguments to
03:37elsewhere in your code.
03:38For instance, you can create a function and then pass that in as an argument
03:42to another function.
03:43So what I'm doing on line 37 is I am declaring a function
03:47called calculateSalesTax.
03:49And then I'm saying that this function expects to have two incoming
03:53parameters: sale and tax.
03:57Then on the right side of the Equal sign, I'm writing the code that's part of that function.
04:02I'm saying, multiply sales by tax.
04:06Now, I can come down here and run that function that I just declared, by using
04:10its name and then passing these two arguments in.
04:13Notice that I'm not separating those with commas;
04:15I'm just passing them in.
04:16That's a common metaphor in F#--not using commas here.
04:19There is an alternative syntax down here on line 42, where I say let
04:24calculateTotalSale, and then I can use what's called a tuple in order to set off items.
04:30By using the parentheses here, I'm saying I have an argument that called sale,
04:36an argument called commission, and
04:38then again, just like before, I write the code for the function on the other
04:41side of Equal sign.
04:42Then I call it the same as before, but now because I declared it like this,
04:45I can pass the arguments in with a comma separating them.
04:48Want to see this run? I do.
04:51I'm also going to put a break point right here by pressing F9.
04:56I'll go up to the Debug menu and choose Start Debugging. Go ahead and say Yes
05:02to save these changes.
05:04You can see I have put a few items to the console.
05:08I'll enter my name.
05:09I do the string concatenation, and then I press Enter, and you can see I just hit my
05:18break point back here.
05:19I have a section on working with breakpoints and debugging, but right now, I'm
05:23just going to step through my code by clicking on this Step button.
05:29Notice what happened.
05:30I declared this function up on line 37.
05:32I'm running line 39, but you can see it just stepped back, and it's showing me
05:36I'm running this code up here: sale * tax, which should be a value of four, if I
05:41hover over that, and this one has a value of five.
05:45Then I'm going to press just Step Into a couple more times.
05:49And then I'll hover over the total, and you'll see that that has a value of 20.
05:53So you see that these in-line functions work exactly the same as if they
05:56were regular functions.
05:59Learning a new language is a great way to appreciate your current
06:02programming language.
06:03I think F# is a great candidate to look at for your next language.
06:07What do you think about giving it a go?
Collapse this transcript
Font and color options
00:00What developer doesn't want absolute control over their coding editor?
00:04Perhaps you prefer a dark background and neon blue text for your code.
00:08When you configure your own fonts and color options, it's one way to
00:11indulge your inner artist.
00:12Let me start by showing you how to increase the font size in your code.
00:17I'm inside a project in Visual Studio called FontAndColor.
00:21I'm going to open these two code files down here: Book.cs--double-click on that
00:26one--and I'm going to open Program.cs.
00:28I'm going to move over to Book.cs, and then I'm going to use a feature of the mouse
00:36scroll wheel in Visual Studio to enlarge this font size.
00:38Before I do that though, let me close this error list on here in the bottom of the
00:41screen, and this output window. There you go.
00:44I'm going to hold down the Control key, and then I'm going to use my mouse
00:50wheel, and I'm going to zoom in to make the text larger.
00:55Obviously, you would do this if you're having trouble reading the code.
00:58If I switch over to Program.cs, and notice that it only affects the one file.
01:04The font size here is the same size as it was originally.
01:06If I switch back to Book.cs, you'll see that it is the large size.
01:11Now if I scroll the opposite direction, what you think will happen?
01:14Ah, you figured it out; the text gets smaller.
01:18If you want more control over the individual words inside your document, then
01:22you need to go to the Tools > Options menu.
01:24Let's go there. Tools > Options.
01:28And then you're going to click on the Environment node, and go to the Fonts
01:32and Colors section.
01:33The first thing I will look at is the basic font, Consolas.
01:37This affects the entire code window;
01:39any text that is in the code window will use the Consolas font.
01:42If you want to change this font, click on this dropdown.
01:44Since programmers tend to like to use monospaced fonts, this dialog shows you
01:50all the fonts in your system, but it points out the ones that are monospaced by
01:54putting them in bold.
01:56Personally, I like the Consolas font, so I'm going to leave it at the default.
02:01Next, I'm going to start changing some of the items in my code.
02:05For instance, maybe I want to change my comments.
02:09I'll press C, which moves me down to the C section. Click on Comment, and
02:14you can see that the normal comment is a white background with a nice dark
02:18green colored font.
02:21I'm going to change my background to a slightly yellow color. I'll click here.
02:25And now these colors make me happy, so what I'm going to do is I'm going to
02:29click on the custom button and choose a light yellow color.
02:35I am thinking that looks pretty good right there.
02:38I'll click OK, and then I'll click OK again.
02:42And you can see that all my comments now have green text with a light yellow background.
02:48I like that.
02:49Next, I want to talk about these curly braces and these parentheses. Those are
02:53called matching braces, so I can color those as well.
02:58Go up to Tools > Options, find the Brace Matching, and I can choose the
03:05brace matching rectangle.
03:07And right now this kind of this light gray color. That's boring.
03:13I'm thinking the some sort of--I had yellow earlier.
03:19I'm thinking maybe some sort of green now for this. How about that color right there?
03:23Looks good to me. Click OK and then click OK.
03:27Now watch what happens if I move my cursor next to the parenthesis.
03:32The matching parenthesis is highlighted in this green color.
03:35If I move down here, the same thing happens. And if I move to this curly brace,
03:42it shows the matching curly brace further down the page.
03:45It shows me that this one on line 28 is the match for this one on line 22.
03:49Very handy for seeing where your beginning and ending sections of your code are.
03:55There are a lot of other changes you can make.
03:56I can change the colors of the line numbers down the side.
04:00I can change the colors of the string; for instance, over here, the color of the
04:05items that are between beginning and ending double quotes can be changed.
04:09This list is endless, as you can see.
04:15So let me show you just one other one though that I like to use a lot, and that's to
04:19differentiate the difference between a value type and a reference type.
04:23So I'm going to scroll down to the User Type section, right here, and I'm going
04:28to pick user types, which would be a class.
04:32And I'm going to change the background of this to light blue. I'll pick Custom.
04:35I'll pick a nice light blue color and then click OK.
04:41And then for the value types--things like enumerations or structures would be a
04:46better example, because there are separate ones for Enums here--
04:49for a structure type, I'll pick a light pink background, like this one.
04:56Click OK and then click OK.
04:58And now let's look what happens in my code.
05:00Everywhere where I declare an insistence of a class, it uses the blue background.
05:05Everywhere where I declare a structure type, it uses the pink background.
05:09When I write code to instantiate those types, it does the same colors.
05:13Here, I'm saying a new book.
05:14Here, I'm saying new file information.
05:17So I find this very handy to keep my mind clear which is a reference type and
05:22which is a value type.
05:24Couple of more settings.
05:25You can go to the Tools, Options and by clicking on this dropdown up here,
05:30you can change all their parts of Visual Studio:
05:33the toolbars, the different windows that are docked to the side,
05:36you can change the fonts that are used for printing output.
05:39Let me show you just one another one in here, the Statement Completion.
05:42This affects the dropdown windows for the IntelliSense.
05:46So if I go right now and choose to change this to a 16-point font, like so, and then
05:52click OK, and go to my code and type in "fi", the dropdown and the tooltip that
06:02are popping up at this moment are now using my 16-point
06:04font, whereas before they were using the 9-point font.
06:07I am just about done.
06:09I would like to reset my environment so that for future movies it uses the
06:13default settings, so I'm going to go back in to Tools.
06:17Of course, you don't have to do this on your computer;
06:19you can keep your own settings.
06:21But for these movies, I'm going to set it back to the defaults.
06:23I'll click on Options, and then I am going to use Use Defaults, and then I'll go
06:29to the Text Editor and click on Use Defaults.
06:34And now my beautiful color comments and all the other configuration changes I
06:38made have disappeared. It's a sad day.
06:41Now, you can spend an hour experimenting all the font settings in this dialog,
06:45and before long, you'll have your own custom code editor
06:47that will be the envy of your peers.
Collapse this transcript
5. Working with Code
Formatting your code
00:00Visual Studio assists you in writing readable code by automatically indenting
00:04and aligning your text.
00:06Many people use the default settings without a second thought, but with a little
00:09effort you can configure the formatting so that you have better control over
00:12how your code is arranged. Let me show you.
00:15I am inside Visual Studio, and I have opened a project
00:18called FormattingYourCode.
00:20This has two C# files: Book.cs-- I'll double-click to open that--and
00:25Program.cs. And it also has a WPF user control, SampleWpfControl, which
00:31contains some XML markup down here.
00:34I'm going to open the Book.cs file, and I'm going to show you the
00:39SmartIndent feature.
00:41Now, SmartIndenting means what happens when you hit the Enter key on one line and
00:45move to the next line.
00:46So to configure this setting,
00:48I'm going to go to Tools > Options, and I'm going to open the Text Editor node,
00:54and then I'm going to click on the All Languages.
00:58This way I can make the changes, and it will affect every code editor.
01:01I'm going to click on Tabs, and I'll start by choosing None for the Indenting.
01:06Now, when I indent my code, like here, notice that when I move to a new line,
01:11the cursor moves to the beginning of the line.
01:13Now if I want to get over here to align it with these other comments, I have to
01:17either press the Spacebar or the Tab key to get over there, like so.
01:25If I used a SmartIndenting, it will automatically handle all that.
01:30I'll use what's called a context-sensitive indent.
01:33It looks at where it belongs--
01:35is it in other curly braces, is it inside an 'if-then' statement?--and
01:38determines where to put it.
01:40So watch what happens now.
01:41When I hit the Enter key, it stays aligned.
01:45Now, the tabs are probably off by a couple of spaces. [0:01:49.17] "This is a demo", like so. Now watch what happens if I write an 'if' statement.
01:52'if' now I pressed 'if' and tab tab twice to stub in some code for me.
01:57Now I'm going to come down here and hit the Enter key.
02:00That's what I mean by context-sensitive.
02:02It knows I'm inside these curly braces, so it indents it at a further
02:06level than it did above.
02:08Now, my comment goes here, and more.
02:12If I move outside the curly braces and hit Enter, it indents it at a
02:16slightly different level.
02:17This is the default setting, and I find it invaluable to have for arranging your code.
02:22You probably have a personal preference on whether you like tabs or spaces.
02:25There is a lot discussion out there on which is better.
02:28I won't argue which is better, but you can choose in Visual Studio which one you
02:31want to use by going to Tools > Options and then coming here and say, when I do
02:36a tab, I want you to Insert spaces.
02:38In other words, if I press Tab and I say that the Tab size is equal to 2, and
02:42Indent size is equal to 2, when I press the Tab key, if the Insert spaces is
02:47turned on, then it will put two spaces in there.
02:49If I have the Keep tabs selected, then it will put a tab that happens to be two spaces wide.
02:55So I'll come down here, and I'll press the Tab key twice. And you see that it
02:58moves over apparently four spaces.
03:01If I go up and I choose Edit > Advanced > View White Space, you can see these
03:07little dots here. Those represent spaces.
03:12I'll go back up and change my settings to Keep tabs, and then I'll move down a line
03:18and press the Tab key. And now you see instead of spaces, I get the little
03:22arrows. That signifies that I'm using tabs, not spaces.
03:25Again, it's your choice. Most often you'll have View White Space turned off.
03:29My personal preference is to use a two- space tab instead of four-space tab,
03:34because I can get more on the screen that way.
03:40I'll turn off View White Space now.
03:41Next, I want to show you what happens when you're working in an editor like C#
03:46and you want to automatically format your document.
03:49There are some settings and tools options specific to different editors.
03:55For example, if I go to the C# editor and go to Formatting, I can set up rules
04:01inside here of how my C# code is indented. The same with the XAML,
04:06I can go down to the XAML section and I specify XAML specific settings.
04:12All right, I'll start with the C# settings.
04:13I'll go up here, choose C#, and I'm going to talk about New Lines.
04:19What this says is, where do you want to put the new line for a brace?
04:23Do you want the brace be on a new line for types?
04:26You get a preview in Visual Studio. Here is a class.
04:29If I place the open brace on a new line for type,
04:31you'll see that this brace here is on the second line.
04:34If I uncheck this, you'll see that it moves up, and the brace stays on the same
04:38line as the class declaration.
04:40Same thing with methods here.
04:41I'll click on moving the Method opening curly brace to this top line, instead of
04:45having it on a separate line like this.
04:47All right, now I'm going to go ahead and unselect these two.
04:49I'm going to return back to my code. Notice that my class up here has the
04:54curly brace where it was.
04:56It doesn't automatically update your code once you make the changes. But if I
04:59were to write a new function right now, I can force a formatting anytime by
05:06going to Edit > Advanced > Format.
05:12I got two choices here: Format Selection and Format Document.
05:15I use this shortcut all the time, Ctrl+K, Ctrl+D. Let me show you how that works.
05:21I just ran that on this document, and now it knows all the methods you are
05:24supposed to have their opening curly brace on the same line as the function name.
05:28I can force that again at anytime.
05:30I'm going to go over here to my XAML.
05:32I'm going to go over here.
05:34I am going to do a Ctrl+K, Ctrl+ D. Now, I want you to keep an eye on this line
05:3810 here, Ctrl+K, Ctrl+ D, Ctrl+ K, Ctrl+D. Nothing happened.
05:43Maybe I need to go change the setting. Tools > Options. Scroll down to the XAML
05:51section, pick Formatting, and then I'm going to go to Spacing.
05:56Right now, the setting is Preserve New lines and Spaces.
05:59In other words, don't touch my XAML.
06:01I'm going to change that to say Position each attribute on a separate line.
06:04I'm going to tell to collapse multiple lines down into a single blank line.
06:09I'll put a few extra spaces in here like that, and now I'm going to do my secret
06:13keystroke: Ctrl+K, Ctrl+D, and keep an eye on line 13.
06:17It will autoarrange my code.
06:20It put each attribute on its own separate line, and those multiple spaces that I had,
06:25it collapsed them down to a single blank space.
06:27Again, try that here. Just several blank lines there, Ctrl+K, Ctrl+D
06:31removes those blank lines.
06:33I can't tell you now many times a day I use Ctrl+K, Ctrl+D to format my code as I work.
06:38I find it indispensable to my sense of order while working,
06:41and I think you will too.
Collapse this transcript
Navigating your code
00:00As soon as you have more than a couple of pages of code in a project, you'll
00:03want to use the Visual Studio navigation features to move intelligently through your code.
00:08Let me show you how that's done.
00:09I'm going to open up Visual Studio and load this NavigatingCSharp project, and
00:14then I'm going to open these two C# files: Book.cs and Program.cs.
00:19The first thing I want to talk to you about is this outlining feature.
00:22This is turned on by default, and what happens in Visual Studio in say a C#
00:27application is that every subsection of your code--like this function here--has
00:31an outline automatically added to it.
00:33So I can click on this Minus sign here, and collapse that down temporarily, so I
00:38don't have to look at the code that's inside that section.
00:40You might notice there are three dots over here.
00:42If I hover over those three dots, I'll get a preview of what's inside there.
00:46So I don't have to open it up just to look at the code that's in there.
00:50Another way of creating sections of code is to use what's called a region.
00:54So what I can do is I can come up here and type in "#region", and then some
01:00string, "hide this code", and then on line 12, I'll add a #endregion--all one word,
01:09and you see what happened over here in the outliner?
01:11It added a new outlining section.
01:14So now I can collapse my custom-made section, and then my custom comment shows
01:19up here for the header of that collapsed section.
01:22This is very handy for hiding code that you're not using anymore.
01:25It's also used to put in things like all your events for a class can go inside
01:30a collapsed section called events, and then you know how to find them later.
01:33To navigate through your code,
01:35there are a couple of techniques you can use.
01:37I'm going to switch over to the Book class temporarily.
01:40I'm looking at this code in this function UpdatePrice, and there are a number of
01:44curly braces in here.
01:45There is a clue that I get from Visual Studio about matching curly braces when I
01:49put my cursor in front of this top curly brace. If you look down on line 21,
01:52you'll see that the curly brace down there has a gray outline on it.
01:57That's how Visual Studio tells me that these two braces are related. But I can
02:00also navigate down to that curly brace by holding down the Ctrl key and tapping
02:06the right square bracket.
02:08Notice how my cursor moved down the line 21. Now, I'll do Ctrl+Right Bracket, and it
02:14moves back up to line 12.
02:16Very handy when you have big code files and you want to quickly move to the
02:19bottom of the code section.
02:21When you're looking at code, there are a multiple ways that you can view
02:25separate views of the code.
02:27So I've got this Book class, and I want to compare it with another file, say
02:31this Program.cs file.
02:33Right now, I can compare them, but I have to keep toggling back and forth.
02:36What I can do is I can pick one of these tabs up and drag it out and place it on
02:42the side or the bottom of the page.
02:44Now I've got this little helper in the center of the window that's showing me--
02:47right now it's going to dock to the left.
02:49And I want to move it down here, so it's docking to the bottom.
02:53Now I've got two separate tab sections, and I can have multiple tabs down here
02:56in this bottom tab section and multiple tabs in the top tab section, and then I
03:00can compare the text.
03:01When I'm done, I can grab this tab and slide it back up and push it back
03:06into the top section.
03:08I can also do the same thing with multiple views of this book class.
03:12There are two methods I can use.
03:14One is I can go to the Window menu and choose New Window.
03:20And since my Book tab is selected, it will create another copy of the book tab.
03:24And then I can use the technique I just showed you to grab this.
03:27And this time I'll move it over and slide it side by side, and now I've got two
03:32separate views of the same physical file.
03:36The other way of splitting the view is to use this little button over here on
03:40the top of the scrollbar, the one that has the two arrows on it.
03:43If I grab that and slide it down, I'm splitting the view.
03:46I still have one tab, but I have two views.
03:49To unsplit it, I can take this and either slide it up, or the fast way to do it
03:54is to double-click on this black line.
03:57Next, I want to show you how to navigate to name sections of your code.
04:01I have this class called Book.
04:02It has a function called UpdatePrice.
04:05It also has a function down here called OtherMethod.
04:07So I'm going to go over here to this Program file, and I'm going to show you
04:12that I am calling UpdatePrice here, and I'm instantiating the Book class here.
04:17So what I can do is put my cursor on Book and then press F12 to navigate over
04:21to that Book class.
04:23If I am on the method and I press F12, then it will navigate over to the method.
04:29See, now I am on line 11.
04:31If I choose a class that I didn't write, like this Console.WriteLine, and I press
04:37F12, then what Visual Studio does is it opens up a text file that contains the
04:42structure of this Microsoft class.
04:44It shows me I was working with this public static void WriteLine method.
04:48Also, there is something called reference highlight.
04:51So if I select this newPrice variable-- it's actually a parameter coming into
04:56this function--then notice that after about a one-second delay, that line 13 and
05:01line 15 light up and show me that I'm using this parameter in those two
05:05sections of my code.
05:06This also works if I click on this variable up here. After about a one-second
05:11delay I see them using it on line 15, and I'm also using it on line 25--a nice
05:16way of visualizing the sections of your code
05:19To continue on with this mode, I'm going to go back to the Program file, and
05:23this time I'm going to right- click on this UpdatePrice method.
05:27I'm going to use this Find All References.
05:29What that does is open a separate window and show me those three places in my
05:33code that have references to UpdatePrice.
05:36The first one, if I double-click on it, is the place where the function is declared.
05:41The second one, it shows me where I'm calling it here on line 11, and when I
05:45double-click of this one, it shows I'm calling it on line 18.
05:47I can also do something called View Call Hierarchy, and this is a
05:51slightly different view.
05:52Now I can see all the calls that are coming into UpdatePrice.
05:55There is a call coming from AnotherDemo and one from the Main function. And I
05:59can also see that within that this method, somewhere within my UpdatePrice
06:04function, I am calling the OtherMethod, which is a method that I wrote, and
06:08I'm also calling the WriteLine function that Microsoft created in their console class.
06:13Finally, if none of these navigation features are making you happy, we can use
06:17our own custom bookmarks.
06:20I can place little tokens in my code and then use those as navigation aids.
06:23First, let me close these two windows anyway.
06:25I'm going to close the Call Hierarchy window, and I'll close the Find Symbol
06:28Results window by clicking on the X.
06:30And then I'm going to drop a bookmark.
06:32I'm going to move my cursor right here on line 9, and then I'm going to use the
06:35bookmark keystroke, which is Ctrl+K, and then I press Ctrl+K a second time.
06:40And if you look on the very left edge of the screen, you'll see that there is
06:43now a gray and blue rectangle over there. That's my bookmark.
06:47I put a couple more in.
06:49I put one here, Ctrl+K, Ctrl+K, and then I'll switch over to my Book class, and
06:54put one here, Ctrl+K, Ctrl+K. And now here comes the power.
06:59I can use those bookmarks I've dropped in there, and I can come up to this
07:02Bookmark toolbar and click back or forward.
07:04So I'll click Back.
07:06It will then cycle me through each of my bookmarks, and those keep going around and around.
07:13This is really handy when you have a big project and you're currently working
07:16on three or four files and you're working in separate areas and you want to
07:19remember where you are.
07:20So you put these temporary bookmarks in.
07:23They affect your copy of the project, not any other developer's copy.
07:28And when you're done, you can just use the same keystroke--Ctrl+K, Ctrl+K--to
07:33turn off the bookmarks.
07:34As you can see, there are many ways to navigate your code besides using the
07:38traditional up down arrows and mouse scrollbars.
07:40Why don't you try a few of these new techniques on your next project?
Collapse this transcript
Using the Task List
00:00I suspect you keep a list of tasks somewhere in your office.
00:03If you want, you can manage some of your tasks from within Visual Studio.
00:07I will show you how to do that.
00:08I am inside Visual Studio, and I have opened this project called TaskList.
00:11There are two files I want to open.
00:14I am going to double-click on Samples.cs and then double-click on Program.cs.
00:19Next, I want to turn on the TaskList window.
00:22If it's not visible on your computer, you need to go to View > Task List.
00:28It then lays down here on the bottom of your screen.
00:30Now it has a normal "add my own task" feature.
00:34To do that, I go to this dropdown, and I choose User Tasks, and then I click
00:39on this Create User Task.
00:41You can see I already have one in here.
00:43You probably won't have this one on your computer.
00:45I'll click here to add a new task, and then type in the word "Demo".
00:49And then when I am finished the task, I can click on this check box to say
00:52I am done with that.
00:53That's not really the task I want to show you.
00:55I am more interested in the code comment tasking that's available.
00:58Let me show you how that one works.
01:00I'll click on this dropdown again, and choose Comments. And then I am going
01:05to go up to this line of code here on line 10, and then I want to write an Event Handler.
01:09So I am going to type the word "TODO" on the beginning of this line, like that.
01:13And after about a one-second wait, you will see that that shows up on my list.
01:18As I continue to add items--I'll go to the Samples file now, and I will put one more in here;
01:23I'll put "TODO" on this line--
01:24I should get another item showing up on my list.
01:27Now I can use this as a navigation aid, I can double-click on this to do, and it
01:31takes me to that section of my code.
01:32I've also got a bug in my code.
01:35I'd like to be able to put bugs on this list.
01:37What I would like to have happen is come over here and type the word "BUG",
01:40and have it show up.
01:41Visual Studio doesn't support that out of the box, but I can configure it
01:45to accept that keyword, by going to Tools > Options, finding the
01:50Environment section, then clicking on this node here, Task List. And then
01:56these are the known tokens.
01:58There you can see the TODO one.
01:58What I am going to do now is type in my own name, BUG, then click the Add button.
02:07Also, I can specify a Priority level:
02:09Low Priority or High Priority.
02:11I think it should be a High Priority and then click OK.
02:14Now you see what happened to my task list on the bottom?
02:16It now shows BUG, and has an exclamation point in this first column.
02:21Now that you've mastered the task list, let's move on to commenting your code.
Collapse this transcript
Commenting your code
00:00There are two kinds of documentation that are frequently written by developers.
00:04First, we have code comments, and second, we have help files.
00:07In this movie, I'll cover code comments, which explain the purpose of the
00:11current code and help make the code self-documenting.
00:13I am inside Visual Studio, and I have opened a solution called Comments, which
00:18contains two projects:
00:20a C# project and a Visual Basic project.
00:23I am going to spend the majority of my time commenting code in the C# project,
00:28but nearly everything I show you works exactly the same in Visual Basic, WPF,
00:32Silverlight, HTML--you name it.
00:35Let's start by open this Book class.
00:38Here I have got a single-line comment.
00:40C# comments are started by using two slashes.
00:43I can also do multi-line comments.
00:45Let me open the Program.cs file.
00:48This is what a multi-line comment looks like.
00:51You start by putting the slash and the asterisks, and then you end the
00:56multi-line comment by using an asterisk and a slash. That comments out all
01:00the code between those.
01:01Another common use of comments is to take lines of code out there that you
01:05are not using anymore.
01:06Let me show you how to do that part.
01:07I am going to go down to these two lines of code here and select them with my mouse.
01:11Now there is a keystroke that I like to use, which is Ctrl+K, Ctrl+U and Ctrl+K,
01:18Ctrl+C; U stands for Uncomment and C stands for Comment.
01:22So let's start by doing Ctrl+K. Visual Studio has this rather odd way of doing keystrokes.
01:29There are so many keystrokes in Visual Studio that sometimes they need to have
01:32multiple keystrokes.
01:33Now I have done a Ctrl+K. If you look on the lower left-hand corner of my
01:36screen, you will see that it says, "Ctrl+ K was pressed, waiting for second key of
01:40chord." And what they're saying is they are waiting for me to type Ctrl+C now. [00:01:44 .84] So let's do that Ctrl+C. Then that little tip on the bottom of the screen goes
01:48away, and the two lines that I had selected are now commented out.
01:52This keystroke also works in Visual Basic. Let me show you.
01:56I'll go over to this VB file,
01:58Module1.vb, and then I will select a few lines of code over here. And then I
02:03will do the same keystroke:
02:04Ctrl+K, Ctrl+C. This time it comments out those three lines, but it uses
02:09the Visual Basic-specific comment character, which is the single quote or apostrophe.
02:14To uncomment, I do Ctrl+K, Ctrl+U, and if I am the kind of person that would
02:20rather use a button on the Toolbar, I can use either of these two buttons.
02:26This button here is the Comment button, and this button is the Uncomment button.
02:30It's really a simple system.
02:31Comment systems are core features in every programming language I've ever worked in.
02:36It only takes a few minutes to show the parts of the comment system here in Visual Studio.
02:40For a more complex documentation story, be sure and watch the next movie in
02:44this chapter.
Collapse this transcript
Documenting your code
00:00Creating an external help file is a common type of documentation. Note:
00:04For this movie, I am not talking about help for the end user of the application,
00:08but rather about code documentation.
00:11This documentation is intended for other programmers to use, to get a better
00:14understanding of a code API.
00:17In order to build the Help files later in this movie, you need to install some
00:20additional open source tools.
00:23These tools are available in your Exercise File folder, in the Installers folder.
00:31There are three tools that you need to install:
00:33GhostDoc, Sandcastle, and SandcastleBuildersSetup.
00:37Sandcastle itself is the help-generation tool from Microsoft.
00:41It only works at the command line.
00:43SandcastleBuilder is a graphics interface that works with Sandcastle, and I am
00:48going to show that later in this movie.
00:50GhostDoc is a plug-in for Visual Studio that helps you write the XML comments you need.
00:56So install those and then continue with the movie.
00:59Now I am in Visual Studio.
01:00I am inside a project called BookLib, which is inside the Documenting Solution.
01:06Both C# and Visual Basic support special help comments called XML comments.
01:11When Visual Studio encounters these XML comments, it can generate a separate XML
01:15file as part of the compile process.
01:17Then this XML file is used by the Help tools as a basis for creating the help files.
01:22My plan is to document this class here, called the Book class.
01:25So I will double-click on it.
01:27It's a simple class that has three public properties, and I see I need to make
01:32this class public for later. And then it has two methods called UpdatePrice,
01:38two overloads of the same method.
01:41So my idea is that I want to document this method and make it easier for a
01:45developer who is using this function to know how it's going to work.
01:48What I do is I go to the line immediately above this method, and I type in three
01:53comment slashes. And you see the Visual Studio stubs in these special XML tags
01:59that are inside these three slashes.
02:01This is a summary about what this function does.
02:04So I will say, "Update the price of a book based on ID."
02:13Then I can come down here and say something about these different parameters
02:19"as a number", right?
02:20Whereas these other overload down here will have a percentage value passed into it.
02:30Now, I am going to compile the application.
02:31It asks me if I want to save the changes.
02:36I am going to say yes. You may not see that dialog on your computer.
02:40Let's look at this from the perspective of a developer who is using this class.
02:44Over here in this code, I have instantiated an instance of the book, and I am
02:47calling UpdatePrice.
02:50Now I am going to move my cursor right here, and I am going to do a
02:52Ctrl+Shift+Spacebar, which launches the IntelliSense.
02:58You can see that it says here "Update the price of a book, based on ID."
03:02And then I am going to go over to this one, and you will see in the bottom it
03:05says, "new price as a number".
03:07So those comments that I just wrote are now showing up in the IntelliSense for
03:11the developer that's calling my method.
03:13So you can go through your application, sprinkling useful comments in there,
03:17and you will get a much better application in the long run, for the other
03:19programmers that are using it.
03:21Now I can formalize this and put this in a Help file too, and then the user could press--
03:26user in this case being another developer--could click on this and press F1 to
03:30launch the Help file and read more help about it.
03:32So what I need to do now is continue to add XML comments.
03:36For instance, I can come up and add them at the class level up here.
03:39Now this can get tedious, so there is a special tool that you can add to Visual
03:44Studio, called GhostDoc.
03:46Let me show you how GhostDoc works.
03:48It's in my Tools menu, right up here at the top.
03:51That's because I installed it earlier today.
03:55I'll click on this public int ID, and then I'll go to Tools > GhostDoc and
04:01choose Document this.
04:03So this is very similar to me writing the slash, slash, slash, but the
04:07difference that GhostDoc give me is that it attempts to write something useful in here.
04:11So it's used the value of The ID, and it's used the words "gets or sets the ID".
04:16That's the main difference in using this one.
04:18So I will do this few more times.
04:19All right, and then I will come up to the class, public class Book, like that,
04:37and now I am ready to generate my Help file.
04:40So to generate the Help file, I need to go into my project, Properties section.
04:45Double-click on this property item over here in the Solution Explorer.
04:48I need to go to Build -- let me close this window down here in the bottom, so
04:52you can see more--and then down here in the bottom section of the Build tab, I
04:57need to check the XML documentation check box.
05:01Now when I do my compile, it's going to generate an additional XML file.
05:05Let me show you that file.
05:07I will right-click on my Project, choose Open folder in Windows Explorer.
05:12I'll go to my compile folder, which is bin > Debug, and there is my executable,
05:20and there is my XML file.
05:22Now I am ready to make the actual Help file.
05:26To do that, I need to run that Sandcastle tool.
05:31So I am going to go out to my Start menu and type in "Sandcastle".
05:36I am using this one here, the Sandcastle Help File Builder GUI. Maximize the window.
05:43Then I will go to File > New Project.
05:46I'll put this on my Desktop, in my Exercise Files folder, Chapter 05.
05:55You can see I have already generated some earlier today.
05:57I'll Click on Save.
05:59I am going to call this one FirstTrial. Oops!
06:03I went one folder too deep.
06:05Let me go back one folder. There we go.
06:06FirstTrial. I am nearly done.
06:09There are a couple of changes you need to make.
06:11One is you need to change your output folder.
06:13If you're storing your files in the Desktop, you cannot compile the Help file.
06:17So I need to click on this sort alphabetical, and then scroll down here to the
06:23WorkingPath. And I need to pick a folder name somewhere that won't get erased.
06:28My C drive. And then can over here I can say BuildHere.
06:32Now what's going to happen is when I compile my Help, it's going to generate
06:35this folder, make all the files, and then when it's done it's going to erase this folder.
06:39So don't point this to any folder that you care about your files, okay?
06:43I am ready to do my Build.
06:45Now I just need to tell it where to find that XML file. So I come over here to
06:48the Documentation Sources, right-click, and choose Add Documentation Source, and
06:53then I need to go over to that bin folder I showed you a few minutes ago.
06:56Documenting > BookLib > bin, now I am going to the Debug folder.
07:01Let's choose this XML file and click Open.
07:04At this point, I think I am ready to make the Help file.
07:07So I go to Documentation and choose Build Project.
07:12My build is done. The Help folder was generating when I did the Build.
07:16You'll see that there is a Documentation.chm file.
07:20I can run that from this folder, or I can run it from the Sandcastle build tool.
07:24I'll run it from here. And as you can see, it's a basic Help file.
07:29It shows I have a class called Book.
07:30There is my description I wrote: "An example of a Book".
07:34I can click on this and see the syntax for the Book, and you see that it
07:38drives from System.Object.
07:40I can go over here and look at the Book methods.
07:42There are two overloads of the UpdatePrice Method. And again, my comments
07:47are showing up here.
07:48If you look at the parameters, you'll see the newPrices. The new price has a
07:53number, so that carried over.
07:55I am getting some warnings, because I haven't provided all of the information in
07:59the XML comment file, so the compiler caught this and said, "You never provided
08:03any information about that other parameter," and there are the Properties.
08:07Creating Help is a critical part of a product lifecycle.
08:09These Help tools are good for creating help for other programmers.
08:13If you are fortunate enough to be building developer tools, then GhostDoc and
08:16Sandcastle are just the tools you need.
08:18If you are building consumer-based applications, these tools are of little value.
08:22For consumer help files, you need to investigate third-party tools.
Collapse this transcript
Using IntelliSense effectively
00:00IntelliSense is one the most helpful features I can think of within Visual Studio.
00:04When you are writing code, it gives you instant, automatic, context-aware help.
00:09I really can't imagine working without its helpful suggestions.
00:12There are some IntelliSense features that are powerful but less
00:15intuitive, however.
00:16In this movie, I will illustrate the countless ways in which IntelliSense helps
00:19you write your code.
00:20To show you IntelliSense, I have to have a code window open.
00:23So I'm going to go into Visual Studio and open this UsingIntellisense project,
00:28and I'm going to open this Programs.cs file.
00:30I've already got some code in here, which I'll use later in this demonstration.
00:34For now, I'm just going to declare a variable of type book.
00:37Book is one of the classes that I have inside this application.
00:40So I'm going to typing "var b = new", and I'm going to start typing the word "book".
00:47I don't even need to get the capitalization correct.
00:49I'll type "bo", and you can see that this IntelliSense dropdown shows that there
00:53are four types that meet my requirements: Book, bool, Boolean. The last one is a
00:59bit of a stretch, but you can see that in the middle of the word is the word
01:02Bound, and that has Bo in it.
01:04To write the rest of my code I'm going to press Tab, or I'll go back here.
01:09Now I backed up a couple of letters, and I don't get the automatic dropdown.
01:13To force the dropdown again, I can use one of my favorite keystrokes inside
01:17Visual Studio: Ctrl+Spacebar.
01:20Hold down the Ctrl key, tap the Spacebar, and there's Book.
01:24The reason Book is selected is because it remembers the last word that I used in
01:29this IntelliSense window.
01:30Now I'm going to type the open parenthesis, and you see what happened?
01:35I didn't have to type O and K. I just had to type the open parenthesis.
01:38It's also works with things like semicolon.
01:41We'll come back here. Do a Ctrl+Spacebar. Book is selected. Now, I'm going to
01:45type the semicolon, and you see it finished typing the word "Book" and then put my semicolon in.
01:50Force yourself to make a habit of learning these shortcuts.
01:53Next, I'm going to call a method on the console class.
01:56So I'm going to close method called WriteLine.
01:59Now it just so happens that the author of the console class wrote 19 different
02:04versions of the WriteLine function.
02:06So the IntelliSense knows that there's 19 different versions of the WriteLine
02:09each one varying slightly by the type of parameters they have, and it's
02:13showing me that there are 19.
02:15I can click on this down arrow to refresh my memory what these different methods are.
02:19For example, this one takes a one value of type char.
02:22I also get some specific text down here telling me more about this function.
02:27Let me delete that line of code.
02:29Next, I'm going to show you something called Smart tags.
02:31"var x = new". And I'll start typing a class that doesn't exist, like the Barn class.
02:42There is no Barn class here.
02:46So the IntelliSense engine is going to warn me that I have done something stupid
02:50by putting the red squiggle there.
02:52If I hover my mouse over it, it says, "The type or namespace Barn could not be
02:56found," and give me some suggestions of what to do.
02:58That's because it's examined all my types and can't find the definition of Barn anywhere.
03:03Now if I select the word Barn, you will see that I get this thing called a smart tag.
03:07Now, smart tags are kind of hard to activate.
03:09You have to move your mouse over this, click here one time, and then I'll
03:14see this dropdown menu.
03:15It says, "Would you like to generate a class for the Barn."
03:19I'll show you how to generate a class later.
03:20Let me just show you how to use this Generate new type.
03:22I'll click here, and I get a dialog, and it's asking me the access scope
03:26for this new type, what kind of type I want: class or struct or an interface or an enumeration--
03:32I think I'm going to choose struct-- and then what file to create barn.cs.
03:37Then I'll click OK.
03:38Now you notice that my red squiggle goes away.
03:41I now have a new file in my Solution Explorer. And if I pop that open and take a
03:46look inside, you'll see that there is a public struct Barn in there.
03:49Now let me return back to the code I was working with a second ago.
03:52I'll go back to Program.cs.
03:54I also have a green squiggle here.
03:56That's because I've declared this variable, but I haven't used it anywhere in my
03:59code--suggesting that either I forgot to do something with it, or I should erase
04:04this line of code, because I'm not using anywhere else in my application.
04:07Next, I want to show you something called autocompletion.
04:10Let me delete this line of code, and I'm going to type in "var x = new".
04:17Now watch carefully what I'm going to do.
04:19I'm going to type a capital S and then a capital L. This is really neat what's going on here.
04:24There is a type available that has a capitalized letter S and an L for
04:29SortedList, and it's picked that out of the list.
04:31There is no word that starts with SL,
04:33so it's showing me the SortedList.
04:35Now all I need to do is press one of those keystrokes I showed you earlier. Space, Tab, Enter.
04:41I'm going to press the Tab key, and it stubs in that new SortedLlist.
04:45This also works if you have your own variables.
04:47I will come over here and say "var ReallyBigNumber = 9".
04:54Then down here I'm going to type in "RBN", and it's finding that in the list.
04:59Even when I only had typed RB, it shows me that there's really big number, and
05:03there is something called EncoderReplacementFallbackBuffer.
05:05So I'm going to type in N, and now I'm going to press Tab key to have it finish typing.
05:11Let Visual Studio do the typing for you, and it'll save you lot a keystrokes.
05:15Next, I want to show you something called generate from usage.
05:18I already have a real customer class over here, but I'm going to sabotage my
05:21application by coming over here and removing this customer class, which of
05:26course will break my code.
05:28Now I get the red squiggle, and I get the smart tag.
05:31I am going to come over here to my smart tag and choose Generate Customer, which
05:34adds this new file, Customer.cs.
05:36Now here is the neat part.
05:38I can go down to the first name and right- click on that or click on the smart tag.
05:43Sometimes it's hard to get these to pop up.
05:44So I can force them to show by using Ctrl+Period like that.
05:48I find that easier than trying to mouse over it.
05:50So I'm going to say, generate a property stub.
05:52I am going to click on this one and do Ctrl+Period.
05:55Do the same thing two more times. Ctrl+Period.
05:59Then on line 26, this is a function call, because of the parentheses.
06:04So when I do Ctrl+Period
06:05down here, it says, "Would you like to generate a method stub for Save."
06:09Now all of my squiggles go away, and if I look inside my Customer.cs file, you'll
06:13see that it's stubbed in some properties and a method.
06:16Now I've still got some work to do, but I no longer have my IntelliSense errors
06:20over in the other file.
06:22My last demo is going to be showing you how to work with these using statements
06:25at the top of my code window.
06:27You might recall that using statements are there so that I don't have to type
06:30all of the names of the class down here in the code section of my window.
06:34What I can do in Visual Studio is I can right-click up here and choose Organize Using.
06:39Then I can sort them in alphabetical order. Or what I'm going to choose is I'm
06:43going to sort them in alphabetical order and also remove any usings that I'm
06:46not currently using.
06:48So that removed three of the four using statement I had at the top of the page.
06:52I can't say enough good things about the IntelliSense support inside Visual Studio.
06:55It's a feature that I simply couldn't live without, because it is so helpful
06:58when I write my code.
Collapse this transcript
Working with code snippets
00:00A few years back, Visual Studio introduced the idea of code snippets.
00:03Snippets are small sections of code that can be automatically generated and
00:06pasted into your code.
00:08They show up in IntelliSense and are customizable, so that you can modify
00:11the injected snippet.
00:12Let me show you how to use a C# snippet.
00:16I am inside Visual Studio, and I have opened this solution
00:18called UsingCodeSnippets.
00:20I have two projects in here: a C# project and a Vb project.
00:24I'll start by opening this Program.cs file, and then I'll add my first snippet.
00:29I will start by typing in the word "if".
00:32Now if you look in my IntelliSense dropdown, you will see the word "if" and to
00:36the immediate left of it is a symbol that looks like a piece of paper with some writing on it.
00:40That is the symbol for a code snippet.
00:42Now if I press the Tab key twice, once will dismiss that dialog, and the second
00:48time I press Tab, it stubs in the code snippet.
00:51In my case, you see it wrote a little bit of code on the line and also wrote two
00:55curly brackets for me.
00:56Some of the code snippets are much bigger than this.
01:00For my next snippet, I am going to use a for snippet.
01:02It's going to be slightly larger than the if statement.
01:04I will type in "for" and then press the Tab key twice.
01:09Now what I want you to see here is that the letter I is in their three places,
01:13and notice how each one of them has some gray around it.
01:15So I am going to type in a new word for i. I am going to type in the word "counter".
01:20Now when I press the Tab key, it's going to move me to the next area, which is length.
01:25Also notice what happened to those i's.
01:27They turned into the word counter.
01:28So they are linked together.
01:30If I go back here and make a change and press the Tab key again, you see that
01:35it's constantly monitoring that first item and changing the other two items. That's very handy.
01:40Next, I will show you an even larger one.
01:42This is a property for WPF.
01:44I am going to type in "prop dp".
01:47That's stands for dependency property.
01:49Now I will press Tab twice, and you can see it's stubbed in about eight or
01:53nine lines of code.
01:54Again, I've got these placeholders.
01:56So I can type in the word "Boolean" here or "bool", and then Tab, "IsReady", and
02:02you'll see that everywhere where it says MyPropertyProperty now, when I press
02:06the Tab key now, those change to say IsReadyProperty.
02:10This property down here is changed to be Boolean, to match what I typed in up here.
02:14So did this one.
02:16Now those clever people over in the Visual Basic team, they are working really
02:19hard to simplify code entry.
02:21They have created about 200 snippets, which is more than the C# team did.
02:25So I am going to switch over to this Module1.vb file and show you how to add a snippet here.
02:30The trigger in Visual Basic is to type the question mark and then the Tab key.
02:36Now, I see several folders.
02:37Each one of these folders has lots of snippets in it.
02:40I encourage you to explore these snippets.
02:43I only have time to show you one.
02:44So I'm going to go down here and show you some Office Development code snippets.
02:48I am going to open this one up and then choose Word and then Formatting, and
02:53here are some of the things I can put in if I'm doing some automation and
02:56adding items to the Word object model. Like I am going to choose this Highlight a Range,
03:00and you can see that it stubbed this piece in.
03:03So I encourage you to spend sometime looking at the vb snippets.
03:06I'd rather spend the time writing our own code snippets.
03:09To do that, I need to write some code, and I need to generate an xml file.
03:14To make it easier, there is a free tool for you to use, called Snippet designer.
03:19I have already installed it on my machine.
03:20Let me show you where it is.
03:21I can go to Tools > Extension Manager, and there you can see that I have
03:27installed extensions.
03:28I have this one called Snippet Designer.
03:30You're not going to see this on your computer probably, unless you've
03:32already installed it.
03:34On your machine, you're going to want to click on the Online Gallery, which takes
03:38you out to a Microsoft web site and shows you all of the available extensions.
03:42I have a separate movie in this title that deals with adding your own extensions
03:47and using extensions.
03:48Later in this course, there's a movie showing you how to write your own
03:51extensions and how to use these pre built extensions.
03:54For right now, just follow these instructions.
03:56Type the word "snippet" over here in the Online Gallery search engine,
04:03then pick snippet designer, and then you are going to click on a download.
04:07I don't have a download button, because I've already installed it, but you will have one.
04:11Click on that, and about 10 seconds later you will have Snippet Designer on your machine.
04:15Now you need to restart Visual Studio.
04:18Once that's done, I can go find some code.
04:21I've got some C# code.
04:22So I am going to switch back to my Program.cs file, and I am going to uncomment
04:26these four lines of text, Ctrl+K, Ctrl+U for uncomment.
04:32Now I'm going to right-click on them and choose Export as Snippet, which will
04:38send them to that extension tool I just showed you.
04:41Here is that Snippet Designer.
04:42Now what I want to have happen when you enter this snippet is I want you to
04:46be able to type in a new value here and replace it on line two and on line
04:51three and on line five.
04:53So what I do is I right-click on this and say Make Replacement.
04:56That puts a token in there called myList, and that tells snippet engine to use
05:02that replaceable syntax. So now I am ready.
05:05I am going to close this.
05:06It says, "Would you like to save the changes."
05:08I am going to say yes.
05:09I need to save this snippet file out in the location where Visual Studio
05:13looks for these snippets.
05:15That would be in Documents > Visual Studio 2010 > Code Snippets > C#, and of
05:22course, here's My Code Snippets folder.
05:24I will open this one up and then click Save.
05:27For some reason, on this computer I keep getting this error dialog.
05:30I'm not sure if you are going to see it or not, but the snippet seems to work correctly.
05:33So I am going to click OK.
05:35Now in order for Visual Studio to load that snippet, I need to restart Visual Studio.
05:39Then I want to create a brand-new project.
05:42I am going to make sure it's a C# project.
05:44I am going to put it out in my movie folder.
05:46I'm in the chapter 05, in movie 7.
05:48And if I did everything correctly, I should be able to use my code snippet.
05:53Right-click. Choose Insert Snippet.
05:56There's my Code Snippets Folder, there's my Snippet File number1, and there's my list.
06:03Then again, if I did everything correctly, I should be able to type in a new name
06:05here, fileNames, and press the Tab key.
06:09The Tab key is not working for me.
06:10How about if I just clicked down here? That worked.
06:13That is Awesome! Look at that!
06:14The file name changed in all four places.
06:16So as you have seen, code snippets are incredibly useful, and they are so easy to create.
06:21Have fun exploring and see what you can do with code snippets.
Collapse this transcript
Refactoring your code
00:00Writing code is a constant push and pull on your development skills.
00:04First, you write some code and test it to see if it works the way you intended.
00:07Then as the project progresses, you think about ways to improve and change the code.
00:11Some people call this rewriting refactoring.
00:14Regardless of how you define it, Visual Studio has refactoring tools to help.
00:17If you are a C# developer, then you don't have to do anything special, as there
00:21are about 10 refactoring tools built into Visual Studio.
00:25If you are a Visual Basic programmer, you should download the free CodeRush
00:28Xpress tool, which provides dramatic code refactoring support.
00:32I'm inside Visual Studio, and I've opened the RefactorYourCode Solution, which
00:38contains a C# and a Visual Basic project.
00:41I will start my refactoring journey by going to Program.cs.
00:45A common refactoring is when you need to rename something.
00:48I am going to rename this variable. So I will put my cursor on the variable, and
00:52I will right-click and choose Refactor.
00:54Then I will choose Rename.
00:57Also note the keystrokes Ctrl+R, Ctrl+R. Now during this refactoring, a
01:03dialog box will appear.
01:04I can type in my new variable name, and then I can choose to preview the
01:09changes inside the dialog.
01:11I can also choose where to search for this word.
01:14I am going to click OK. Here's my preview screen.
01:17It shows that it's going to change line 13.
01:19It's a little bit harder to see, but it shows it's also going to change line 18.
01:23These changes look good to me, so I am going to click the Apply button, and I
01:28successfully changed those two lines of code.
01:30Next, I want to save the method name.
01:32So I'll click on the save book, which I am using twice. I am using it on line 21 and on line 18.
01:38I am going to click here and use the keystrokes Ctrl+R, Ctrl+R. Make a slight
01:44change. Call this SaveTheBook.
01:47Again, choose to preview reference changes, and it shows in my preview screen
01:52several places where this code is going to be updated.
01:54I can click through these different elements up here to look.
01:57I'll click on Apply.
01:58And if I look in the Book.cs file, I also see that it changed it
02:03successfully over here.
02:05Next, I want to go to the MathLib file and show you the Extract Method Refactor.
02:11I look at this code, and I'm thinking, "I want to reuse this in my application.
02:16It shouldn't live in entirely inside this CalculatePaintNeeded Method."
02:19So what I can do is come over here, choose Refactor > Extract Method.
02:25I'll get the dialog box again, and I think the new name should be called CalcArea,
02:32and as you can see Visual Studio wrote a new function for me, copied my code
02:36down there to line 24, and then changed my code on line 17 to call that method
02:41and pass in the necessary information that's needed for this function.
02:45Next, I am going to switch back to the Book and show you the reorder
02:48parameter function.
02:50I have three parameters here.
02:51We decided on our team that this first string variable belongs at the end.
02:56So what I do is I put my cursor on SaveTheBook, choose the Refactor menu, and then
03:02choose this one, Reorder Parameters.
03:05Another dialog crops up.
03:06It shows the three parameters.
03:08I am going to pick this first parameter and then the down arrow, click on it
03:11twice to move it to the end of the parameter list.
03:14I feel good about not previewing my changes, so I am going to click on OK, and
03:18now you see the parameter has moved to the third position.
03:20Now this a bit tricky, because I'm calling this function from a couple of areas
03:24in my code--calling it over here in Program.cs--so let's verify that it changed
03:28correctly over there.
03:29Yes it did, look on line 18.
03:31File path is now the last argument, and this string down here is also the last argument.
03:37My last refactoring for C# is going to be Extract Interface.
03:41That's a common refactoring pattern.
03:42I am going to go to this Book class, and I want to take these two
03:45properties, title and price, and I want to pull them out of the class and
03:48put them in a common interface.
03:50So I will choose the Book class, invoke the Refactor menu, choose
03:54Extract Interface, and then look at this interface name and see if it
03:57meets my requirements.
03:58I like IBook. Next, I come down, and I choose the two properties.
04:02I want to leave the method inside the class and then click OK.
04:06Visual Studio creates a brand-new file over here--IBook.cs--extracts my
04:11interface and then correctly fixes up my code in the Book class to
04:16implement that interface.
04:17If you are a Visual Basic programmer, you don't have refactoring in
04:20Visual Studio by default.
04:22The good news is if you like what you see in Express, it's also available for C# programmers.
04:26If you plan on following along with me in this demo, you need to install the Express tools.
04:31I've included those in the Installers folder inside your Exercise Files folder.
04:37So you go to Installers and then run this Express Installer.
04:40When I ran Visual Studio the next time after installing DevExpress, it asked me
04:44how you would like to run DevExpress, and I told it to let me load it manually
04:49when I start up the application.
04:50So I am going to go to this menu, DevExpress, and pick Load.
04:53And immediately you will see some changes in my C# code.
04:56It's now drawing a red line between my curly braces and other colored lines
05:01between like my if statements here.
05:03It will also do the same thing over in my vb code.
05:06There are more refactoring tools in Visual Studio.
05:08I have only shown a few of them in this movie.
05:11If you like Visual Basic, then check out the VB refactoring title in
05:14this chapter.
Collapse this transcript
Understanding code generation
00:01Often, when writing your application, you will come across large areas of
00:05boilerplate code in which the same code pattern is repeated over and over.
00:09Whenever you find yourself repeating the same mindless task, you should consider
00:13automating the process.
00:15The good news is other people have considered this problem before you and have
00:18written code generation tools.
00:20Code generation is where software-- rather than a human--is used to write program
00:24components automatically.
00:26A number of commercial and free code generators are available in the market.
00:30Visual Studio uses generators in many of the UI designers.
00:34They have the drag-and-drop designers generate the XML or code.
00:39Another popular use of code generators is in object relational mapping tools, or ORM.
00:44ORM tools take the tables and other constructs inside a database and turn them
00:50into language objects.
00:52There is an active community of third- party vendors making code generation tools.
00:56Here is a list of some of the code generators on the market.
00:59The most common pattern I see is for code generators is the following:
01:03You create a template file, which contains your portions of your code.
01:08The template file also contains tokens.
01:12You take your template file and you pass the template file to a generator.
01:17Optionally you pass other external parameters to the generator--say a
01:20database connection string.
01:21Here is an example, on the bottom of this slide, of a token from CodeSmith.
01:27This has an angle bracket, a percent, a word called current time, and then the
01:32closed percent and close angle bracket.
01:34This will tell the code generator to calculate the current time and place it in
01:38your code template at this point.
01:41When you run the generator, you'll pass in parameters.
01:44Some of the things you might pass in are connection strings to open the
01:47database, evaluate database schemas, find files in your hard drive, or read
01:52information from your UI tools.
01:55Then the code generator will replace the tokens with the runtime information.
02:00Visual Studio has a code generator, known as T4.
02:04I would like to show you how that works, and it's the topic of the next movie
02:08in this chapter.
Collapse this transcript
Generating code with T4
00:00Visual Studio includes a code generator that creates files from a simple
00:04template definition.
00:05It has an awkward name;
00:06it's known as the Text Template Transformation toolkit.
00:10Most of us just call it T4.
00:12I am inside Visual Studio, and I have opened the CodeGenWithT4 project.
00:17Let's think about what we are going to do for a minute.
00:19I have code that I write over and over again that has some replaceable bits.
00:23So what I am going to create is a scaffolding of the basics of my code, and then
00:28I am going to have these little replaceable parts that the code generator will
00:32replace on my behalf.
00:33To create this code template, I need to add a T4 template, by right-clicking and
00:38choosing Add > New Item. And I'll type in "template" in the Search dialog and
00:44pick this simple Text Template right here, and then I am going to call this one
00:48SimpleDemo and click Add.
00:53I get warned by Visual Studio that this template may harm my computer.
00:57I am going to go ahead and risk it by clicking OK.
00:59At the top of this template is some instructions of what to build.
01:03It's going to create a C#-specific class, and the extension on the file that
01:08it's going to generate is txt.
01:10So if you come over here and look underneath these SimpleDemo.tt file, by
01:14expanding this node, you'll see that there is a text file there.
01:18I want this to be a C# file, so I am going to change the extensions from .txt to
01:24.cs, and then I am going to build the application. I'll accept the risk.
01:31And if you look over in the Solution Explorer now, you'll see that this it
01:34says SimpleDemo.cs.
01:37Next, I'll create the basic scaffolding.
01:40Some of this text is never going to change, like a Using statement, at the top of
01:44my screen, using System, or public class Customer.
01:52This is known as a text block, which means it's entered exactly as I type it here.
01:57Watch what happens now when I do a build.
01:59I'll choose Build Solution, click through these dialogs.
02:04Then I am going to open the SimpleDemo.cs file, and you see it's starting to
02:08generate some code for me.
02:10There are more sophisticated kinds of instructions you can put in your template.
02:14You can create something called an expression block. That computes the value, and
02:17then inserts it into your output file.
02:19You can also create something called the statement block, which allows you to
02:22run any arbitrary code.
02:24You can also create something called a class feature block, which allows you to
02:27embed code sections within the template.
02:29Let me show you how to do an expression block.
02:32I'll switch back to the template, "public void DoWork".
02:50What the DateTime.Parse () method does is it takes a string and tries to
02:55convert it into a DateTime.
02:56I am going to get the time from the operating system.
03:00So inside these double quotes, I am going to put my expression block.
03:05I need the special symbol for T4, which is the open angle bracket, pound sign,
03:10and then the pound sign and a close angle bracket.
03:13And then I'll use the assignment operator here, equal.
03:15I think I've got my code correct.
03:23Let's see if it compiles. Excellent!
03:27Now let's go back to SimpleDemo.cs, and you'll see that it created this function
03:31and inside the function is this line of code that's declaring a variable, and
03:34here is the piece of data that came from the code template generator.
03:37Rather than write a bunch of code, I am now going to switch over to this
03:40MakeFunction.tt file, which is a more complete example.
03:45Here is the same bit of code I showed you few minutes ago.
03:47I am also creating a variable here and asking the computer for the machine name.
03:51I am then going to declare a variable that's going to hold the list of string,
03:55one of the generic types in .NET.
03:57And then look what is happening here.
03:59I am calling a function that I declared in my template code.
04:03So down here in this section, with this special starter--angle bracket, pound, plus sign,
04:08then it ends here on line 36--
04:10I've declared a function called PrintFileNames, which basically goes out, given
04:14a folder name, and looks to see if the directory exists.
04:17If it does, it walks through all of the files in that folder and prints their
04:22name out to my code file, and then I call that function up here.
04:26Let's see what happens when I run this example.
04:28Here is that code: var fileName = new List<Strings >, and then I am using the
04:33initializer syntax to add these string names to the list.
04:36Another really exciting thing to do with templates is to work with a database.
04:41So in this project, I added a database file, this Northwind.mdf file, and then I
04:46created the data model for that, a linked data model.
04:49Now I can go to this code template, this CreateBusinessClasses code template,
04:55and in this section right here, I am calling that Northwind data using this
05:02thing called the NorthwindDataContext. And then I am foreach-ing over the
05:06database, and finding each table inside that database. And I am looking to see
05:11if it starts with the word "dbo.", and if so, then I am stripping some of the
05:15database characters off of it, so that I have a class name that makes sense in my C# code.
05:20Then down here, I am declaring a class, and I am saying I want that tableName.
05:25Then within the class, I am doing a foreach over the DataMembers--this would be
05:30like the column names of the table--and I am writing a little bit of code here
05:34to create a public property.
05:35So when I run this template, what I end up with is a list of classes that map to the database.
05:41That's why they call it Object Relational Mapping.
05:43There is a table named Categories.
05:45I created a class called Categories.
05:47There was a Territories up here, and it has a TerritoryDescription;
05:50that's the column name inside the database, and that becomes a public
05:53property on my type.
05:54This is a very powerful mechanism.
05:57I've used it very successfully on some big projects, where we generate our
06:00schemas automatically.
06:01And if somebody changes the database schema, the next time we build our code,
06:06let's say they change this column name in the database,
06:09it automatically changes our codes to reflect the real name inside the database.
06:14I think code generation done right, it can be a productivity boost for you and
06:18your development team.
06:20The included T4 Editor is a little sparse, but you can always install a free
06:24Visual Studio extension from a company called Tangible, for better syntax help.
Collapse this transcript
Using the Class View, Class Designer, and Class Diagram tools
00:00Classes and structures are the heart of building an object-oriented application.
00:04When you are creating these classes, you can use a tool called Class Designer to
00:08simplify integration tasks.
00:10If you are looking at existing classes, you navigate through your class
00:12structure with the Object Browser or the Class View.
00:15Let's start by looking at the Class View.
00:17I am inside Visual Studio, and I have opened a project called UsingClassWindows.
00:21You can see I have two folders in here that contain classes.
00:24I have a BookSeller folder, which contains five classes, and a DataServices
00:29folder, which contains this DataShuttle class.
00:32I can investigate my class structure by using the Class View.
00:35To View the Class View, I go to the View menu and then choose Class View.
00:41It lives over here next to the Solution Explorer.
00:44It's usually a tab on the same area of the screen as the Solution Explorer.
00:48If you look at the top of this Class View, you'll see three
00:50different namespaces.
00:52The first namespace contains one class, my console program class.
00:56The second namespace,
00:58the BookSeller namespace, contains these five classes I showed you, and then
01:01this last namespace here contains one class.
01:05Now if I double-click on one of these classes, it takes me over to the source code.
01:09I am thinking that I put this class in the wrong namespace,
01:13so what I can do is come up here and edit my code, and say this belongs in
01:16the BookSeller namespace. And after a few seconds, when you look over in the
01:20Class View, you'll see that the third namespace is disappeared. And now if I
01:24expand this section, you'll see that the DataShuttle is now part of the
01:28BookSeller namespace.
01:29The Object Viewer is another way to View the classes and their members.
01:33The difference between the Object Viewer and the Class View is that the Object
01:36Viewer provides a drilldown into the objects in any reference .NET assembly,
01:40not just the ones that you have written.
01:42Again, I am going to return to the View menu.
01:45This time I'll choose the Object Browser.
01:48It lives in the center of the screen.
01:50This shows all of the reference namespaces that I have in my project.
01:54I can search for something like, say the Console window, and once I find the
01:59type that I am interested in, I can click on it and in this section of the
02:03screen, I can see all of its members.
02:04There are all the methods that are part of System.Console, and here are all of the properties.
02:09When I click on one of these properties, I can read about it down here in this
02:13bottom section. Same with the methods.
02:16So it's another way of examining the classes that somebody else has
02:20written. Very handy.
02:22The Class Designer exists to make it simple to diagram and model your classes.
02:26It provides a visual representation of your classes and the relationships
02:30with other entities.
02:31It also provides a way of modifying those classes.
02:33Here is how you create a Class Diagram.
02:35You find one of your types, say this Book class. Before I go any further, let me
02:40close this Object Browser. Close that window.
02:42Right-click on this Book.
02:44This time I am in the Class View, and I will choose View Class Diagram.
02:48What you see is a visual representation of your class, the structure of your
02:52class, not the code itself, but the structure of this class.
02:55I can click on these little chevrons here, and see the Properties on my type.
02:59I can also, on the bottom of the screen see the methods, properties, and fields
03:03that are part of this type.
03:04An alternative way of adding a Class Diagram is to go to your solution,
03:09right-click on your project, choose Add > New item, type in the word "class" here
03:14in the Filter box, and then pick this Class Diagram.
03:17That adds this file here, BookSellerDiagram.cd.
03:25That ClassDiagram1.cd was created when I right-clicked on the book class and
03:29told it to view the Class Diagram.
03:32Now let's look and see what it says here. "The Class Designer allows you to
03:35visualize and work with types in your code.
03:38Create new types by dragging items from the Toolbox."
03:41If you go over and open your Toolbox-- you may not see it on your screen, but you
03:44can go to View, Toolbox--
03:45here is a listing of the things I can create.
03:47I am going to come back and show you how to use that in just a minute.
03:50It also says you can, "visualize the existing types by dragging items from the
03:53Class View or Solution."
03:55That implies I can take this book, and drag it over and drop it on the
03:58surface, and then I could take this Person class and this Employee class and drag those over.
04:05Because the Employee and the Person Class have a object relationship, the
04:10diagram shows this relationship arrow.
04:13If I double-click on the Employee model, I can see that Employee derives from Person.
04:19I can use the Class Diagram to diagram other classes that aren't part of my project.
04:25To do that, I need to go over to my Class View, and then up here in the Search
04:28dialog, I'll type the name of a namespace or type that exists in one of the .NET assemblies.
04:33For instance, I am going to look at System.IO.
04:35Now, I'll slide these over and make some room.
04:38I'll open the System.IO namespace, and then I am going to take this File class
04:43and drag it over, or this Stream class and drag that over. And then I can click
04:48on this dropdown to learn more about the type, and I can see its methods and
04:52other details in the bottom of the screen.
04:54Let me switch back to this ClassDiagram1 that was generated earlier today.
04:58I am supposed to be able to go over to the Toolbox and pick up one of
05:01these items over here--
05:02this Interface, or this Class, or this Delegate, and these Inheritance
05:06arrows--and I am supposed to be able to take these and drag them over to my designer surface.
05:11When I drag the Class over, it says, what kind of class would you like to create?
05:15I think I'll create a Customer class.
05:16It's going to go in this file, Customer.cs.
05:19Then I can come down to this bottom section and add Methods, like the Save
05:23method. Or I can add a property, FirstName, and so on.
05:29And if you double-click on this model, it takes you over to where the code is
05:33being generated, and I can see that it created a class with this property
05:37and this Save method.
05:39So let's review what we have seen.
05:40If you want to look at objects in compiled assemblies, use the Object Browser.
05:45If you want to examine or modify the classes in your own code, use the Class
05:49View or the Class Designer.
Collapse this transcript
Refactoring VB with CodeRush Xpress
00:00If you are a Visual Basic programmer, you don't have refactoring in
00:03Visual Studio by default.
00:04There is a free CodeRush Xpress tool, however, that is sanctioned by Microsoft.
00:08If you plan on following along with me in these demos, you need to install the Xpress tool.
00:12I have included a copy of the installer in the Exercise Files folder, and then
00:17the Installers folder.
00:18This is the file you want to install, CodeRushXpress.
00:21Once you've installed CodeRushXpress, you need to restart Visual Studio. Then
00:25the next time you run it, you may need to enable DevExpress.
00:29On my machine, I have to go to DevExpress load.
00:32For my first refactoring, I am going to open this Program.vb file, and I am
00:37going to do a simple rename refactoring.
00:39I am going to click on the letter b, and I am going to invoke the Refactoring menu.
00:44It's down here.
00:45It has an exclamation on end of the name. That's how we know it's the CodeRush Refactor.
00:51The refactoring that I want to do today is Rename Local.
00:53I am going to press the Enter key, and notice that there are two blue bounding
00:57boxes added to my window: one on line 7 and one on line 12.
01:02Those two variables are now linked, and if I type a new name here at the top, it
01:07appears online 12 as well.
01:09Later, if I go back and click on this item again, you'll see that the link still exists.
01:14I can then choose to rename it.
01:16Next, I am going to do an extract method refactor.
01:19To do that, I am going to open this file, MathLib.vb.
01:23This refactor, I am going to take this small calculation here, and
01:26I am going to extract it out into a separate method, because I think I might
01:29reuse it later in my application.
01:31I am going to select this text and then choose refractoring menu,
01:34Refactor!.. > Extract Method.
01:37Now you can see a lot of things happening here.
01:39There are some blue arrows pointing to my code and some green arrows pointing to my code.
01:42I am going to just hit the Enter key, and then I get this red line that lets me
01:47choose where I want to put the new method.
01:49I think I want to have the new method go down here on line 12.
01:51I will press Enter, and again, you see I get those blue bounding boxes.
01:55This time I can type in my new name, "CalcArea".
02:01Now I want you to notice one more thing: on line 9 there is a blue triangle up there.
02:06That is a marker.
02:07It was dropped when I did the refactoring.
02:09Now I can press the Escape key. Keep an eye in line 9 when I press the Escape
02:14key, and you'll see that the cursor moves back there.
02:16I am pressing it now.
02:17It was also a little shrinking blue circle that shows I will move to that point.
02:22Next, I would like to refactor this method over in the Book class.
02:27I am going to switch to the Book class.
02:29I am going to reorder the parameters in this method.
02:32I am going to invoke the Refactor
02:34key on my keyboard, which is Ctrl+ Backtick character, and then I am going to
02:39choose Reorder Parameters from this list, and then press Enter.
02:42Now again, you see there is a blue bounding box, and this time there is an arrow
02:46that is pointing to the right.
02:48What I can do now is I can press the right and left arrows on my keyboard to
02:52move these parameters.
02:54I am going to move this parameter over to the next position, and then when I am
02:58happy with this, I can press the Enter key.
03:00Now of course, I can keep going back and forth.
03:03Right now, I am happy with it as a last parameter,
03:05so I am going to press the Enter key, And then the Refactoring tool takes me
03:09over to my Main() method and shows me that on line 12,
03:13I made a call to that function, and it needs to be rewritten.
03:17If I approve this change, I click on the check mark. Then it moves to line 15
03:21and says, "Do you want to change the code here," and I approve that one as well.
03:26Up at the top of my Book class, I have an autoimplemented property.
03:31There is a refactor that lets me turn this into an implemented property.
03:34I'll choose Title and then Ctrl+Backtick.
03:37I'll choose to Create Backing Store.
03:39Notice the red mark there showing it's going to replace that text.
03:41I will press Enter, and you see it's rewritten it to about six lines of code.
03:46Later, if I want to switch it back, I could choose this item and then switch it
03:50back so it would look like this property down here.
03:53There are lots of other refactorings. Let me show you just one more.
03:56I am going to show you the Extract Interface.
03:58I will click on Book, choosers Refactor!, choose to extract the interface, and
04:04you see that it pull the Interface out, used the code down here to implement
04:07the interface, wrote the properties for me, Title, and made sure it says
04:12Implements IBook.Title, and it has also got the blue bounding markers, showing
04:15that there is a link.
04:16So if I want to change the name of the interface to something like IWhatever,
04:21it's changing all the linked locations.
04:24So as you see, the Xpress tools take a more dramatic approach to refactoring.
04:28If you write C# code, then you'll be happy to know that CodeRush has refactoring
04:31for your language too.
Collapse this transcript
6. Understanding the Project Types
Working with project and item templates
00:00When you create a project in Visual Studio, it creates a lot of files for you.
00:04The number of these files and the file types are determined by a project template.
00:09Simply stated, a project template instructs Visual Studios which files to
00:13generate and load into the IDE.
00:16I'd like to start this movie by looking at the existing templates.
00:19I am inside Visual Studio, and I am going to create a new project.
00:22I will go to File > New > Project.
00:26I am going to select the Console Application.
00:31This is one of the templates that's available.
00:33There are Visual Basic templates and C# templates, C++ templates, and many more.
00:40I am going to go to C# and then Windows, and I am going to choose
00:45Console Application.
00:47Next, I have to pick the location on my hard drive where to put these files.
00:51Choose a location that makes sense for your application.
00:54For my application, I am going to browse out to my Chapter 06 folder and then Chapter 01.
01:02I will call this one ConsoleApplication1. Accept the defaults.
01:10Now what I want you to see is that the template loaded a file called Program.cs,
01:17and there is also another code file that's hidden inside this Properties folder
01:23called AssemblyInfo.cs.
01:25So it created two files for me.
01:27Now if I create a different kind of application--let's say I add another new
01:31project and choose WPF--
01:37this template loads more files.
01:40It loads something called App.xaml and App.xaml.cs.
01:46It also created two files that start with the name MainWindow. And if I create a
01:52web application, like this ASP.NET web application--again, accepting the
02:01defaults here--I get dozens of files.
02:05I get a folder called Account, and one called Styles, and a number of aspx files.
02:12These are all coming from different templates.
02:14So where are these templates stored?
02:18Let's go and take a look.
02:19I am going to switch over to Windows Explorer, and I am going to go into my
02:25Program Files folder.
02:26On my machine, I am using the one that's entitled x86. And then I am going to
02:32scroll down and find Microsoft Visual Studio 10 > a Common7 folder > IDE, and then
02:43I need to scroll down and find the Project Templates folder. And now I can
02:48finally see the different template areas.
02:50I am looking for a C# template, and I am looking for the Windows template.
02:58One more folder. The 1033 folder is the English folder, and here is that
03:04ConsoleApplication template.
03:06It's stored in a ZIP file.
03:07So if I open this ZIP file, I'll see that there is a program.cs file and an
03:13assembly.cs file, plus some other files.
03:16So the instructions to Visual Studio are to create all these separate files.
03:20Now let's see what happens if I double-click on this program.cs file.
03:25It loads it inside Visual Studio.
03:27On your machine, it may not load into Visual Studio.
03:29It might use a different tool to open the file.
03:32It should use some text editor, though.
03:34Now let's take a look between this file and the program.cs that was created
03:39when I ran the template.
03:41Here I ended up with a namespace called ConsoleApplication1.
03:46In the template, there is a token sitting in here.
03:50It's replaced when the template is unwrapped.
03:53This means to go figure out what the name of the project is and put it here.
03:57Also, note there is 'if' statement up at the top that says, "If the
04:00targetframeworkversion is greater than or equal to 3.5, then add an
04:05additional using statement."
04:07So in my file, you can see that System.Ling was added, because I created a 4.0
04:13version of the ConsoleApplication.
04:14Now the nice thing about templates is that you can create your own templates.
04:19I have some friends that have a consultancy, and they create the same projects
04:22over and over again for their clients.
04:25If you were to find yourself in this situation, you might consider making your
04:28own custom project or item templates.
04:31I am going to start by creating a custom item template.
04:34I am going to go over to my ConsoleApplication. And I think before I do that I am
04:38going to close all these windows.
04:39So on Visual Studio, I can go to the Windows menu, and then choose Close All Documents.
04:44Then I am going to go to my ConsoleApplication, and I am going to say Add > Class.
04:51I am going to call this one BoilerPlateClass.
04:59Next, I am going to put some of the known template tokens in here.
05:05I am going to create a comment that says, "created on", and then I will use one
05:11of the tokens, move down to the next line, and then here I will put in another token.
05:28This one is registeredorganization, and then lastly, I am going to replace this
05:36class name with the token itemname.
05:45Let me save that file.
05:47To make a template out of this, I am going to go to File > Export Template.
05:54I am going to create two templates.
05:56The first one I am going to create is an Item template.
05:58So I'll select Item template, I will click on Next, and I will pick my
06:03BoilerPlateClass as the Item template, and then click Next.
06:09Here, I can add references.
06:10When the user uses my template,
06:12it's going to add these DLLs in the references folder.
06:15Let me pick those three.
06:18Then I will click on Next.
06:21Here I give my template a name.
06:22I am going to call this one BoilerPlateClass, and then I'll click Finish.
06:37This file actually got copied to two different places.
06:40It got copied to the My Exported Templates folder.
06:43It also got copied to my Visual Studio folder inside the My Documents folder.
06:48Now, let's close the solution,
06:54create a brand-new project--
06:56I am going to use a ConsoleApplication-- and then I am going to right-click over
07:02here and say, Add > New Item, and there is my BoilerPlateClass.
07:07So I will choose that one, click Add, and as you can see, it replaced the
07:14date and it replaced the class name.
07:18Next, I want to create a project template.
07:20So let me close this solution, and not save the changes, and then I am going to
07:27go and open an existing project that I already have.
07:29I am going to say Open > ProjectSolution.
07:32I am going to open this project in the InfoReader folder, called InfoReader.sln.
07:43Next, I am going to make a template out of it.
07:45You might recognize this.
07:46This is the one I did in Chapter 03, where I created the Info
07:49Reader application.
07:51It's got a database and a picture and an xsdf file and a xaml file.
07:57So I am going to make a template out of this.
07:58I am going to go to File > Export Template, make it a project template, call it
08:05InfoReader or lynda.com or whatever you want to call your template.
08:10Click on Finish, and then in the future I can come in and say I want to do File >
08:16New > Project. Come over here and type "info".
08:24There is my InfoReader project.
08:25I click on OK, and there is my InfoReader1, and you see how it automatically
08:30brought in all of my files.
08:33So as you can see, the Export feature makes it dead simple to create your own
08:36item an project templates.
Collapse this transcript
Creating a console application
00:00Since the dawn of computer programming, there have been
00:03command-based applications.
00:04The Windows operating system still supports command-line applications, and
00:08you won't be surprised to learn that Visual Studio has a console application template.
00:12The main reason I see for creating console apps is when you have a utility
00:15program that doesn't need a fancy user interface.
00:18I'm inside Visual Studio, and I've opened up a solution called
00:21CreateConsoleApplication, which contains two sample apps.
00:25I'm going to start by looking at the standard Program.cs that came from the
00:29Visual Studio console template.
00:30I'm going to double-click on Program.cs.
00:33This file has a static void Main, which is the entry point for all .NET applications.
00:38Now, console applications have standard inputs, standard outputs, and standard errors.
00:45If I want to do the standard output, I use Console.Write or Console.WriteLine.
00:53The main difference between the two is that Console.WriteLine will output text
00:57and then move to a new line.
00:59The text that I want to print to the Console is going to say, "Type a file name."
01:09I want to run the application at this point.
01:12To do that, in Visual Studio, I can choose Debug > Start.
01:15Now since I have a really short application, it's not going to stay on the
01:18screen for very long.
01:20Watch what happens.
01:24The screen shows up and then immediately disappears.
01:27To mitigate that, Visual Studio has this menu called Start Without Debugging.
01:31Now, my text shows up, and I'm given an opportunity to watch the last results
01:36before closing the window.
01:37I'll press the Spacebar to close this window.
01:41If I want to control that situation myself, I can use Console.ReadLine, and
01:49while I'm doing this, I might as well use this to grab the file name.
01:52So I'm going to create a variable called fileName =.
02:00There are some fun things you can do with the console.
02:02I'm going to copy this little bit of text down here at the bottom of the screen.
02:05I'll copy this and then paste it right up here.
02:09I'm going to change the ConsoleColor before I output the text.
02:13Let me show you what that looks like.
02:14Debug > Start Debugging.
02:19As you can see, I now have yellow text in my command window.
02:22I've seen some interesting console applications that use characters and
02:24colors successfully.
02:26Check out the Heroic Adventure application in the exercise folder to see an example.
02:30I'm now going to press any key.
02:34Sometimes you don't want to output your information to the standard console out.
02:38You'd rather output it to another area on your computer.
02:41A common example here is to output to a file stream.
02:44So, I'm going to take this code down here and copy it, and paste it right here--
02:50Ctrl+V to paste--and then I'm going to uncomment the code by pressing Ctrl+K,
02:54Ctrl+U. Now, this little bit of code I just wrote,
03:02it says, "Get the current output," which is a TextWriter, "and store it in this variable.
03:07Now switch to a new StreamWriter based on this file name that the user is going to type in."
03:13Then I'm going to set the console to use the StreamWriter to that file.
03:20Now we'll no longer write to the standard output, but we'll write into the file.
03:24So I write a line of code here, which goes into the file.
03:27I flush the file so that the information is stored in the file.
03:30I switch back to the old output on this line of code, and then I want to see the results.
03:35So what I'm going to do on line 25 is I'm going to start a process.
03:40.NET has a class called Process, which I can use to launch any application.
03:44Here I'm launching an app based on the file name that I send.
03:48So if I chose the file name mydemo.xls, it would open up Excel.
03:53Let me show you this application running.
03:55Debug > Start Debugging.
03:57I'm going to type a file name Demo.txt, which is a text file.
04:05So when it launches the process, it's going to run in notepad. There is Notepad.
04:10As you can see, my output successfully ended up inside the file. Close Notepad.
04:18Press the Enter key to close this window.
04:21Next, let me switch over to this second project.
04:23I'm going to set this as my Startup by right-clicking and then choosing Set as StartUp.
04:31Then I'm going to double-click on Program.cs.
04:34Here's the standard pattern you see quite often.
04:38You go out to your file system and you type in "xcopy" and then a file name and a location.
04:47This utility will take this file and copy it to this secondary location.
04:52So you see what I'm doing is I'm passing two arguments into this application.
04:57So if I want to mimic this in my application, I can do that by having a String
05:02array as a parameter on a static void Main.
05:05So what I'm going to do in this example is I'm going to check the argument array
05:11up here to see if there's any data in it.
05:14So I check if args.Length is greater than 0.
05:18Then I know there's at least one item in the array.
05:19I then loop over to all the items in the array and I check to see if it
05:25starts with a -file name:.
05:28If it does, then I'm going to split off the backend of it, whatever text
05:32comes after the colon.
05:34That's what I'm doing here.
05:35I'm splitting on this colon, and then I'm grabbing the second part of this array
05:40that was just generated here.
05:42Arrays in C# always start at zero.
05:44So this is second part of the array.
05:46I'll then compile the application, and then I would go out to the command prompt
05:49and then run the application and pass the parameters in.
05:52For convenience sake, Visual Studio has this nice little feature where I can go
05:56to my Properties, double-click here, go to Debug, and what I'm saying here is
06:03whenever you run a Debug session, here is the arguments I want to pass into the
06:07application, which means I don't have to go out to a command prompt to do this.
06:11So you see I'm passing an -file name:
06:14and then file.txt, and then I'm passing in a second argument right here.
06:18So now when I come up here and I choose to Debug > Start Debugging, it's going
06:24to pick up those parameters, pass them into my program--
06:27I will kill the application-- and then what did it do?
06:36Then it used that file name to change the standard output and write to that file.
06:42You can see that file if you go over here and choose Open Folder in Windows
06:48Explorer, and go to the bin folder and then go to the Debug folder,
06:53there is the file that was created for me, file.txt. So let me review.
06:57The main reason you create a console application is when you are creating a
07:00utility that doesn't need a UI, except for a standard console screen.
Collapse this transcript
Creating a class library
00:00It is common nowadays to create a separate DLL that contains shared logic for applications.
00:05That way you have a centralized assembly that contains the compiled code.
00:09You also have one project where you can write and debug that shared code.
00:13In .NET, you create a class library to accomplish this.
00:16When you compile that class library, it generates a DLL.
00:20I'm inside Visual Studio, and I've opened this Solution
00:23called CreateClassLibrary.
00:25There are two projects already in here.
00:27There is a Visual Basic console application, and there is a WPF application
00:33that's written in C#.
00:35Now I'm going to add a shared library.
00:38I'm going to go to the solution, right- click and choose to add a new project.
00:43I'm then going to choose this Class Library Template and then type in the name
00:49TextLibrary for the Name, and then click OK.
00:57Now, I'm going to compile the application to show you what happens.
01:01When I build the solution, it's going to compile all three projects. And if I
01:07right-click on the TextLibrary and choose Open Folder in Windows Explorer,
01:11you'll see that if I drill down into the bin folder and then the Debug folder,
01:17that it's creating this DLL.
01:19Now, I'm going to come back to this folder in a minute.
01:22Let me switch to Visual Studio.
01:25I'm going to generate a class called LoremGenerator, which generates random
01:29sentences of Latin words.
01:31So, I think I should rename this Class file.
01:33So I'll come over here to my project and type in "LoremGenerator", and then press Enter.
01:44Visual Studio then prompts me and asks me if I would also like to rename the
01:48class inside that file.
01:50That seems like a great idea to me.
01:51So I'm going to click Yes.
01:52Now you see that it has this new LoremGenerator class.
01:56I'm not going to write all the code today live.
01:59I do have the code up here in this Assets folder.
02:03There is a TXT file in here, which I'm going to double-click on.
02:06Then I'm going to copy all of the code in this class.
02:14Back to my LoremGenerator.cs file.
02:17Then I'll replace these three lines of code, Ctrl+V. I won't go through all of
02:22the code, but let me just point out that there are two methods I'm going to use:
02:26the GetWords method where I pass the numberOfWords in, and it gives me back say
02:31eight words, and a GetSentence, which has the same parameter, but this time of
02:36course it's going to put a period on the end of that sentence.
02:38Now I'm ready to use this.
02:40This is a C# library.
02:42I'm going to go to my Visual Basic Application and add a reference to this library.
02:47I'm going to right-click, and I'm going to choose to add a reference, and I'm
02:53going to click the Browse button, and then I'm going to go back to Windows
02:57Explorer and copy this path to that Debug folder.
03:01Copy it, Ctrl+C. Switch back here, and I'm going to paste it down here where
03:06it says File name, and then press Enter. And there is the DLL that I want to
03:12have the reference to.
03:13So I'll choose that one and then click OK.
03:14Now, just for good measure, I'm going to recompile my application and click Yes here.
03:21So now when I compile this application, it's telling it to make sure that I can
03:27use the code that's inside that DLL. Let's use it.
03:30I'm going to double-click on Module1.vb, and then I'm going to go here, and I'm
03:35going to declare an instance of that generator, Dim g As New TextLibrary.
03:42There is my namespace. There is my class.
03:45Now I'm going to write to the console, and I'll also read from the console.
04:02Next, I'm going to go onto line 6, and I'm typing in "g.GetSentence", and let's
04:15say I wanted eight words out of that.
04:16Let's test this out. Debug > Start Debugging, and there is my randomly generated
04:23sentence, including the period on the end.
04:27Next, I'm going to switch over to the C# application--
04:30that would be this one down here WpfClient--and I'm going to open the
04:35MainWindow.xaml.cs file.
04:40Then inside my constructor, I will declare the C# version of that variable, var g = new.
04:49Well, this isn't going to work, because I forgot one step.
04:51I forgot to add the reference to the library.
04:54See, I'm getting a red squiggle here.
04:55So let me comment this code out and then go over to my References folder and
05:00show you an alternative way of writing a reference.
05:03Right-click > Add Reference.
05:07Last time I browsed out and pointed to a physical location, but Visual Studio
05:11can simplify my life by letting me do something called a project reference.
05:15I click here and then I say whenever you compile this TextLibrary, I would like
05:19to know about it and get a fresh copy of that DLL.
05:22So, I click OK here, and then if you scroll down and look at the Reference here,
05:28you'll notice it says Copy Local equal True.
05:31So not only will it update the DLL, but it'll copy the DLL into the same
05:35folder as my application.
05:36So, when I deploy the app, I've got both files.
05:40Now that I've added that Reference, I can go back to this code that wasn't
05:43working before, uncomment it, and then go to this line.
05:50There is the TextLibrary I was looking for, and then period, and there is the
05:54LoremGenerator, and then finally, "var words = g.GetWords".
06:02And there is my new function.
06:03So, the takeaway from this is both the Visual Basic application and the C#
06:12application are sharing the code that's inside that TextLibrary DLL.
06:17As you can see, it is very easy to add references to other libraries in Visual
06:20Studio and that the Class Library template provides a trouble-free way to make
06:24that custom library.
Collapse this transcript
Creating a web site with ASP.NET
00:00Programmers have been creating web applications with Visual Studio for years.
00:04When Visual Studio first debuted in 2002, it included a new way of building web
00:08sites with .NET-managed libraries.
00:11For those of us who were building ASP classic sites, this was a welcome change.
00:15Now ten years later, we continue to see phenomenal growth in web tools from
00:19Microsoft and other vendors.
00:22In this movie, I will show you how to create some ASP.NET web sites. Just so
00:26you know, however, this is a big topic--much bigger than we can cover in a single movie.
00:31But you can find other ASP.NET titles here on lynda.com.
00:35I am inside Visual Studio, and I am going to create a new ASP.NET web site.
00:40To do that, I go to the File menu, and choose New > Web Site.
00:46The default location for this application will be in My Documents folder, inside
00:51the Visual Studio web sites folder.
00:53It may be a different location on your computer.
00:56I want to make sure this is a C# application, and then I will choose ASP.NET Web Site.
01:00Now, creating this kind of web site project in Visual Studio gives you a very
01:07simple folder-based site.
01:09Any files that you add to this folder or its sub-folders are considered to
01:12be part of this site.
01:14You can see I have a Default.aspx page and several other pages in here.
01:20There is also something called a master page.
01:22There are CSS documents, menus, user management controls.
01:28It also includes the jQuery JavaScript library.
01:31All of these items are part of the standard ASP.NET web site library.
01:36To run this application, I can choose to debug it, or I can go over to one
01:41of these items--let's say this Default page-- and right-click and say View in Browser.
01:45I can even select my browser by choosing Browse With, and then it will show all
01:50the registered browsers.
01:52Now you may notice that in the lower right-hand corner of my screen, a dialog is
01:55popped up down here that says ASP.NET Development Server.
01:59This means that I don't have to have a production-quality web server on
02:03my developer machine.
02:04I have this dedicated light-weight developer-only Development Server available.
02:10It's picked a local host and a port on my machine for this web site.
02:15I am going to browse with Firefox and then click Browse.
02:21Choose No to this dialog.
02:24There is my sample page.
02:25Notice I have got a couple of links across the top here.
02:28I can go to the About page or back to the Homepage. And again, it's loaded it with
02:35the correct port number up here in the browser.
02:41There are several other types of web applications you can build.
02:45We can find more here in the File menu, under New > Project.
02:49Remember last time I chose Web Site?
02:51This time I am going to choose Project and then click Web.
02:56Over here, you can see an ASP.NET Web Application.
03:00This is a more developer- centric model of the web application.
03:04It's going to compile some DLLs.
03:06It's a little more sophisticated than the one I just showed you.
03:09There is also a number of other types of ASP projects here.
03:13Model View Controller is one of the very popular new ones.
03:16It's called ASP.NET Model View Controller 2 Web Application.
03:19I encourage you to look at some of these other types down here.
03:23I am going to look at just this one here:
03:24ASP.NET Web Application.
03:27This time it's going to look out on my hard drive, to my Exercise File folder.
03:32Again, on your machine, you can choose wherever you like to put these files.
03:36I am going to go ahead and click OK.
03:37It doesn't look much different.
03:42It's just what happens when you compile the application. And now in order to
03:45add an item to this project, you can no longer just add it to the folder on your hard drive.
03:49You will have to go to your Web Application, right-click, and choose Add > New Item,
03:56so that Visual Studio knows about it.
03:59I thought I would edit a little bit of code.
04:01I am going to click on the Designer, and then I am going to go to this section
04:05right here after this text and hit Enter, which adds a new paragraph, and then I
04:10can add controls by dragging and dropping.
04:12I can also edit this in my HTML, if I'd like.
04:15I am going to add a button, and then I am going to add a hyperlink.
04:18Now, this hyperlink is editable by going to the Properties window.
04:24For instance, I can change the text to say something like "Go to About page", and
04:34then for the location of where to navigate to, I can click on this property, and
04:38choose from this dropdown, and pick any of the pages that are in my web site.
04:42So I am going to tell it to go to the About.aspx page.
04:48Again, I can browse by choosing right-click > Browse With.
04:53This time I will just view in browser, which I believe on this machine will
04:57launch Internet Explorer.
05:03Now, I'll click on the hyperlink, and it moves me to the About page.
05:07I can also put code, and I can debug ASP.NET applications.
05:11Let me show you how to do that.
05:12I am going to close this browser.
05:13I am going to go move to this button, and I am going to double-click on it
05:19which will stub in some C# code for me. Double-click on the button.
05:23It switches me over to the C# view of the code behind page, and inside this
05:28procedure here, I am going to modify the text on that hyperlink.
05:33The hyperlink control is called Hyperlink1, and I am going to change the text to
05:38say "lynda.com", and then I want to change the navigate location.
05:49That's called NavigateUrl. And I think I am ready.
06:04Since I want to debug the application, I no longer need to go over here and
06:09right-click and say View in Browser.
06:11I need to go up here to Debug and say Start Debugging.
06:14That will attach the debugger to the browser. So let me do that.
06:20I will choose Debug > Start with Debugging. Save my changes.
06:25I have the Development Server launched.
06:33When I click on the button, it changes to text, and now when I click on
06:36the hyperlink, we no longer go to the About page, but we go to the
06:40lynda.com web site.
06:43I told you I could debug this,
06:44so let me switch over to Visual Studio, and I am going to put a breakpoint here
06:48by clicking on this margin.
06:50If you don't have the margin, you can press F9.
06:52Now, there is a breakpoint there.
06:55I will switch back to my browser, click on the button, and notice what happens.
07:01I switch over to Visual Studio, and there is now yellow marker on the line of
07:05code where I had the breakpoint.
07:07Now, I can step through my code,
07:11I can hover over these values to see what's currently stored in there,
07:14I can edit the values,
07:16I can rewind my code, and re-run that line of code again by dragging the yellow
07:20arrow up to the previous line.
07:23So as you can see, there are lot of ways of working with web sites in Visual Studio.
07:27The key point today is that Visual Studio provides all the tools you need to
07:31edit, build, debug, and deploy those web sites.
Collapse this transcript
Creating a rich internet application with Silverlight
00:00Silverlight is one of the shining jewels in the Microsoft toolkit.
00:03If you are new to Silverlight, this movie will give you a quick overview.
00:07If you want to learn more about Silverlight, there are a number of titles here
00:10on lynda.com that provides more details about this rich platform.
00:13What is Silverlight?
00:15It is Microsoft's framework for building complex, visually rich, cross-browser
00:20and cross-platform applications.
00:23Developers can create web applications in familiar languages, like Visual Basic and C#.
00:28Unlike HTML-based application, Silverlight has zero browser compatibility issues,
00:34but it does require a browser plug-in to be installed on the user's computer.
00:38Silverlight is also one of the programming platforms for the new Windows Phone 7 devices.
00:43I am going to create a brand-new Silverlight project inside Visual Studio.
00:47To do that, I need to go to File > New > Project and then choose the
00:54Silverlight section.
00:55If you want to do a Silverlight 4 project, make sure you have the Visual Studio
00:59extensions installed.
01:02Next, I'll choose Silverlight Application.
01:05Make sure I'm in the folder that I want to be in--
01:07in this case, it's the movie folder in my Exercise Files folder--and then click OK.
01:12Visual Studio asks me if I would like a companion web site.
01:18I think that's a great idea,
01:19so I am going to click OK.
01:24If you look in the Solution Explorer, you'll see that there are two projects now:
01:28the SilverlightApplication and an ASP.NET application.
01:33What happens when I run the application is it compiles the
01:36Silverlight application,
01:37copies the special files over to the ASP.NET application, launches
01:43ASP.NET, and then loads my Silverlight control onto the page.
01:48Before I run the application, let's put a little bit of user interface in here.
01:51I'm going to go to my toolbox and find the Silverlight Button and drag that over
01:56to the designer surface.
01:58If I'd rather used the XAML editor, I can use the bottom half of the split screen.
02:03Here I can hand type my own controls.
02:06For my case, I think I just might change the text on the button.
02:09So I am going to go to the Content property, and type in Change Text.
02:15Next, I will grab a TextBlock from the toolbox and drag that over and drop it on
02:23my designer surface.
02:25Then I'm going to double-click on this button to stub in some code.
02:29Visual Studio wrote this little bit of code for me. And then I'm going to write
02:34one line of code here, textBlock1.Text = hello.
02:45Now we are ready to test this application.
02:47If you wanted to debug your Silverlight application, you can choose to Debug > Start Debugging.
02:53If you notice in the lower right-hand corner, the ASP.NET Development Server has started up.
03:02Now my ASP.NET page is loaded and then embedded inside that page,
03:07the entire working area of the browser is now a Silverlight plug-in.
03:12I can click on this Change Text button and you see the hello string shows up.
03:16Now this is a rather plain application.
03:19I thought I would show you some interesting applications.
03:21I am going to show two more applications in this movie.
03:25I'll start by loading a project from our Silverlight 4 title.
03:28I'll go to File > Open > Project.
03:32I am going to go to our movie chapter and open this UsingBingMapControl, and
03:41then I am going to load this SLN file.
03:47Next, I am going to open the Silverlight application and find this page
03:52called ChangeMapView.
03:54I also want to verify that this is my starting page, so I am going to
03:57double-click on this App.xaml.cs and make sure that it says, in line 29, that I
04:05want to load the ChangeMapView.
04:10Then I'll switch over to my XAML.
04:14What I am using in this example is a plug-in control called the BingMapControl.
04:19I have written a text block across the top and three buttons and a couple
04:22of text blocks across the bottom that show our current latitude and longitude on the map.
04:27When you click on this New York button, I've written a little bit of code that
04:31changes the map mode and then uses the latitude and longitude position to change the map.
04:37This number here represents the zoom factor on the map.
04:42When you click on the Grand Canyon button, I do something similar.
04:46I change to a slightly different map mode, show the user the Grand Canyon with
04:51a higher zoom level.
04:54I also put a couple of custom controls on the map.
04:57These are called pushpins.
04:58Not only do I put the pushpins on the map, but I also set a tooltip
05:02so when the user hovers over these pushpins, they can read this text.
05:06Let's see this in action.
05:07Debug > Start Debugging and then click Yes.
05:11That was the Save dialog, and here is my Bing map control.
05:20I can drag the map around and look at the map, double-click to zoom in, and then
05:26I can click on this New York button to move me over to the New York coordinates.
05:32Then I can click on the zoom in to Grand Canyon button to move back to the Grand
05:35Canyon, switch to the aerial view.
05:38As I promised, here are the two pushpins, and I can hover over those and see the tooltips.
05:45The second example I want to show you is on the Internet--
05:49this is as an open source project--
05:51I'm particularly interested in this application because some of the pixel shader
05:55tools that I wrote are included in this application.
05:58It's called Fantasia, and it's at this URL.
06:02This is a Silverlight photo editing application.
06:05So, I can draw on this application by choosing different brushes, like this
06:10Basic Furball brush.
06:13I can tint the pictures.
06:17I can use these pixel effects down here, like the Emboss effect, or Invert Colors
06:23effect and some of my favorites are Swirls where I can drag the center point
06:30around to any location. See how fast and responsive this is?
06:33I'll choose cancel on that one.
06:35And let me show you one more before I close: Drain Flush.
06:38That gives me a nice swirling effect.
06:40There is a lot more we could talk about in Silverlight.
06:49Silverlight supports auto browser applications and a host of other
06:52interesting features.
06:53Be sure and check out our other titles here on lynda.com.
Collapse this transcript
Creating a classic Windows application with Windows Forms
00:00Forms have been a part of the Microsoft programming lexicon since the first
00:04version of Visual Basic appeared back in 1991.
00:08Form is the term used to designate an interactive window.
00:11.NET is a replacement for those early com-based systems, but Microsoft continues
00:15the notion of form-based development.
00:17This framework is called Windows Forms.
00:20Windows Forms have been extremely popular in corporate applications, forming the
00:23basis of thousands of useful applications.
00:26They are easy to develop, and within Visual Studio support a drag-and-
00:29drop interface designer.
00:31Despite their popularity, WinForm development is on the wane.
00:36WinForm uses GDI as it's rendering engine, which some consider inferior for
00:41building modern interfaces.
00:43Because of this, Microsoft created a replacement technology for Windows Forms.
00:47This new UI/API is called Windows Presentation Foundation, and it is covered in
00:53the next movie in this chapter.
00:55Even though I'm a diehard WPF fan, I want to show you some of the features of Windows Forms.
01:00It is a powerful tool, and is still used by many development shops as their
01:04leading Windows client development framework.
01:06I'm inside Visual Studio, and I've opened a solution called CreateWindowsForms.
01:12Currently, there are no projects within this solution.
01:15I am going to right-click on the solution and choose Add > New Project.
01:23For today, I'm going to create a Visual Basic template because it has some
01:29interesting Windows Forms templates to use.
01:32So I am going to choose Visual Basic, and then Windows Form Application, and
01:37then I am going to go ahead and leave at its default name of
01:39WindowsApplication1 and click OK.
01:44Here is my main screen of my application.
01:47I would like to add a couple of more Windows templates.
01:50To do that, I'm going to right-click on my WindowsAplication, choose Add > New Item.
01:59If I click on the Windows Forms node here, under Common Items, I can see about 20
02:05different form templates.
02:07I am going to use the Splash Screen, and I am going to add one more: the Explorer Form.
02:19As you can see, the Explorer Form has a complex UI in place.
02:26It has a menu, a toolbar, a split screen and many other features.
02:31That's all I want to show in that designer.
02:35The Splash Screen is designed and pop up when the application starts. To tell
02:39Visual Studio to launch this first and then my main form,
02:42I need to go to My Project, double-click here, scroll down to the bottom of this
02:48Application section, and choose Splash Screen. Choose your appropriate form.
02:56While I'm in this dialog, I'll also make sure that my start up form is correct.
03:00I want to start with Form1 as my main screen.
03:03Now, I'm ready to test. Debug > Start Debugging.
03:09On my machine, it prompts me if I want to save my changes.
03:12There is the Splash Screen, and there is our main UI.
03:19I'll click the Close button to shut down this form, and then I'll close
03:23these two windows here:
03:24the WindowsAplication properties and the SplashScreen.
03:27So, for the next couple of minutes, I'm going to creating some user interface for this screen.
03:33I am going to start by dragging a few items to the designer.
03:38First comes the menu strip.
03:40So I am going to go to my Toolbox.
03:42I am going to open this All Windows Forms section and find the MenuStrip
03:48control and drag that over.
03:51Notice that Visual Studio automatically docks that to the top.
03:55I'll also find the StatusStrip and drag that and drop it.
03:59That gets docked along the bottom edge of my screen. I need a ListBox.
04:07Here it is. I'll drag that over and make it a little bit wider and taller. And then the last
04:14item I want to add is something called the PropertyGrid.
04:16Now, you've seen the PropertyGrid inside Visual Studio.
04:21It lives over here;
04:22at least on a Windows Form Application, it does.
04:26This control is also usable in your applications.
04:29So I am going to find it. Here it is.
04:34I'll drag that over, and the next step I want to take is to dock this
04:41PropertyGrid to the right edge of the screen.
04:43To do that, I'm going to go to the Visual Studio property grid, scroll down to
04:49the Dock property, select it, and choose this dropdown.
04:56This represents the edges of the window where I can dock the control.
05:00If I click on this button right here, it means to dock to the right edge.
05:07And I'll make it about 50% of the width of the screen.
05:11Now, I am ready to write some code.
05:13I need some menu items up here, so I am going to click at the top of the screen.
05:18I currently have no menu text.
05:20There is a very simple way to add a lot of menu items in a hurry in Visual Studio,
05:25by going to this little special icon over here, the black triangle, clicking
05:30once and then choose Insert Standard Items.
05:35See what happened?
05:36It added a File menu, an Edit menu, and the one I'm interested in--the Tools menu.
05:43Now, I am going to add my own menu item right here, so I just click once and start typing.
05:53Double-click on this one, and it switches me over to the Visual Studio editor,
05:59and it has stubbed in this Visual Basic code.
06:01Now, what I want to do is go out to a folder in my hard drive.
06:05I am going to go to my Desktop folder, and I am going to search for text files,
06:10files that end with a TXT extension.
06:12Now, I have already created two empty text files on my Desktop.
06:17If you're following along, make sure that you point your example to a folder
06:21that contains some files with a TXT extension.
06:24I am going to start by getting my desktop folder.
06:28We'll declare a variable called location, and then I'm going to go to the .NET
06:34engine, and I'm going to query the Environment, using this method called
06:40GetFolderPath. And watch what happens when I hit the open parenthesis--Visual
06:43Basic suggests this enumerated value.
06:48I am going to be looking in my Desktop folder.
06:53So this is saying to .NET, "Somewhere on my hard drive is a Desktop folder.
06:59I don't know the exact location of it.
07:01Would you look that up and find the path of that and store that here?"
07:04Now next, I want to write a link query to talk to that file system and query
07:10the data that's here.
07:11Now rather than hand type that, I have an Assets folder here that contains a
07:15text local link code.
07:17I am going to open up this text file, and then I am going to copy this text and
07:23paste it into this code window right here. And I see I have this blue squiggle.
07:30This happens occasionally.
07:31It means I am missing an Import statement at the top of my code window.
07:34That should fix it.
07:41So what this says is I'd like to start a query and store the query in this
07:45variable. Go out to the hard drive and walk through all of the files in this
07:50folder--remember that's my Desktop folder--and then here is the query part.
07:55Do a Where clause on that and say I'm only interested in files if they have a
08:00TXT extension on them.
08:02Once you've done that query, I want you to select out the information and store it
08:07in this data type called the FileInfo.
08:09Now FileInfo contains information like when the file was last saved and what the
08:13file name is and things like that.
08:15So I am going to select that out.
08:18On line 12, I am going to execute the query by calling ToList, which returns the
08:21results of the query as a list.
08:23I am going to assign them to the list box as data source, and then I am going to
08:27tell the list box which property on the FileInfo class I'd like to show.
08:31I want to show the Name of the file.
08:34The next piece of information comes from the ListBox.
08:39As the items are loaded in the ListBox I am going to click on them, and then
08:43I want to take the data, the details about those files, and load them into this PropertyGrid.
08:47So I need to write an event procedure for the ListBox.
08:50So I'll switch back to my designer, I clicked in the ListBox.
08:53I am going to go over to this property grid and click on the lightning bolt, and
08:58then I am going to find this property called SelectedIndexChanged.
09:01That's an event that fires whenever the user selects a new item in the ListBox.
09:05I'll double-click right here on the edge, right in this column here, and Visual
09:10Studio will stub in this code on my behalf.
09:15If I go to this text file up here, SelectedIndexChanged.txt file, I've got the
09:19code I am going to write in there.
09:22So I'll copy that over and then paste it in here.
09:29What this code says is, "Make sure that the user has selected something in the ListBox.
09:35If they've selected something, then wherever it is they selected assign it
09:39to the PropertyGrid.
09:40The PropertyGrid then interrogates that object and shows me all the details
09:44about whatever that type is."
09:47I think I am ready to run the application. Debug > Start Debugging.
09:57Click Tools > Show Files.
10:01It found two files on my desktop. And hen I click on the file, it loads the
10:06information into the PropertyGrid about that item.
10:09For instance, you can see the file name is abc.txt, and the last time it was
10:16accessed was at 2:15 this afternoon.
10:18There is lot more I can show you with Windows Forms, but that's about all the
10:21time I have in this movie.
10:23Remember this: Windows Forms are still a viable technology for developing
10:27Windows applications, and they are common in many corporate environments.
Collapse this transcript
Creating a dramatic Windows application with Windows Presentation Foundation (WPF)
00:00Windows Presentation Foundation,
00:02more commonly called WPF, provides a rich model for building modern user interfaces.
00:07It contains incredible data binding tools, a superb animation toolset, and is the
00:12best Microsoft technology for building desktop applications.
00:16As you suspect, Visual Studio contains a project template for creating WPF applications.
00:21I'm going to go to Visual Studio's File menu and choose New > Project.
00:28Then I'm going to click on the language of choice.
00:31Today, I'll choose Visual C#, and then I will click on Windows.
00:36There are several templates inside this folder.
00:39First is the standard WPF Application, second is a specialized type of
00:46application called a WPF Browser App-- sometimes called an XBAP--that runs
00:51within your browser.
00:53Then lastly, there are two Custom Libraries down here:
00:56WPF Custom Control Library and WPF User Control Library.
01:01For today, I'm going to choose WPF Application, and then place that in my Movie folder.
01:07I am going to click OK and wait for about two seconds.
01:12Visual Studio opens up the designer surface.
01:16It has a split screen.
01:18On the top is the area where I can drag and drop my controls. On the bottom half
01:23is the area where I can write my XAML.
01:26There is a Zoom to Fit button here, where I can shrink down the screen size to see everything.
01:32Then of course, there is a toolbox to the side.
01:35I don't need this bottom window opened, so I am going to close this document
01:38outline, so I can see more of my toolbox.
01:40Naturally, I can take one of these controls--
01:43Let's say this button--and drag it over to my designer surface.
01:47If you look down in the bottom half of the screen, you'll see that it wrote this
01:52Markup Language for me.
01:54I can edit the XAML directly.
01:55For instance, I can change the content of the button by typing in a string here.
02:01And I can edit it in either place.
02:04I can resize the button, drag to a new location, place other elements on the
02:10screen--for instance a ListBox.
02:12When I drag the list box around, I get snap grid showing me I have the two edges
02:19of these two elements aligned.
02:21Now I can size this list box a little bit bigger to make it align with the other side.
02:27There are helper numbers here showing me the size of this control as I resize it.
02:32I can make this thing look much nicer by adding gradients and transparencies.
02:37Let me show you on the background of this window.
02:40I'm going to click on the window itself or this grid--one or the other.
02:44I'm going to go to a property called Background.
02:50It's called Background and not back color because you can apply any valid WPF brush.
02:57I could paint this with solid colors, with gradient colors, with images,
03:01and many other items.
03:03I am going to click on this little dropdown here, and choose the gradient brush.
03:09Notice there's also a solid color brush and an image brush.
03:13I am going to click on the gradient brush.
03:16Then at the bottom of the screen, you'll see that I have two gradient colors.
03:21I'm going to flip the gradient by clicking on this button here.
03:25Then I'm going to change the color value of this black portion of the window to a dark blue--
03:35something along those lines--and maybe make it a little less saturated.
03:40So I'll move this mouse pointer over here to the left edge.
03:45It's more gray on the side of the window.
03:48I can control how far down the screen the gradient goes by dragging
03:51this gradient stop.
03:52I think that looks nice.
03:55I can click on the button, and I can apply an opacity value, so that I could see
04:00partially through this button.
04:02There's an Opacity property down here in the O section.
04:05I am going to set that to 0.5.
04:12Now, you can see that if I bring that to front, it's covering up the list box,
04:21but I can see a portion of the list box through the button.
04:24This is very hard to do with the other desktop application technology that
04:28Microsoft has, which is called Windows Forms.
04:30Now, this is all I am going to show you of the WPF template in this movie.
04:34The reason is I have an entire chapter devoted to this topic, and I encourage
04:38you to spend some time watching those movies.
Collapse this transcript
Creating a WCF service
00:00There is often a requirement when building an application to communicate with
00:04an external service.
00:05In today's programming environment, that service often lives in the cloud.
00:10Programmers have been working for years to come up with the best way of
00:12communicating with a remote service.
00:15Microsoft has several ideas here too.
00:17For example, they have web services, remoting, and a multitude of
00:22networking protocols.
00:23A few years back, Microsoft decided to create a unified architecture for working
00:28with these disparate technologies.
00:30The outcome of that work is called Windows Communication Foundation, also known as WCF.
00:36The key point about WCF is that it works in a flexible way.
00:40You create a service that provides data or functions.
00:44Through a configuration file, you specify which technologies this service will
00:48use for the communication channel, and how the data will be transmitted.
00:53By changing the configuration files on the client and the server, you can easily
00:57change to another channel.
00:59I am inside Visual Studio.
01:00It's important that you run Visual Studio as an administrator if you want
01:05to test the project.
01:06I have opened a solution called ProvideWcfServices.
01:10This solution already contains a service project called BookServicesV2.
01:15This is the finished version of this service.
01:17I am going to show you how to use the WCF project template to create a project.
01:21I am going to right-click on my solution and choose Add > New Project.
01:27I am going to choose my language of choice--
01:30today it's going to be Visual C#--and then I am going to click on the WCF folder.
01:35Do you see that I have four templates in here?
01:38The one I am going to work with today is the WCF Service Library.
01:42I am going to call this one BookServicesV1, and then I am going to click OK.
01:52This should go in the same folder as the existing solution.
02:00I see I have a typo.
02:02I said BookServices, but I typed BookService.
02:05Let me fix that and then click OK.
02:11Here is my service.
02:12Now remember what I am doing here.
02:13I am writing some code that's going to do some work for my clients, or going to
02:18provide some data for them.
02:20WCF expects you to write an interface, and they have already stubbed in one
02:24interface for you called IService.
02:25Then you're supposed to create a concrete implementation of that service
02:30by creating a class.
02:31So there is also a class here called BookServices.
02:34There is also a class here called Service1.
02:38These are not good names for my services.
02:40I am thinking I am going to be working with a library of books,
02:43so I might want to have it called BookService and IBook and names along those lines.
02:49So what I can do is refactor these by clicking on the IService name here,
02:55changing it in the file to IBook.
03:01Visual Studio notices that I am changing the file and is suggesting that I also
03:04rename the references in code.
03:07I am going to say Yes.
03:09Can you see what happened here on line 11?
03:11Now it says IBook there.
03:13Then I need to rename this service, the concrete class.
03:17I am going to call this one Book, and it will go through the same process:
03:22"Would you like to rename this?"
03:24I am going to say yes.
03:26Next, I need to go into my interface and provide the operational contracts.
03:31I am going to specify what is okay to expose to the network by adding these
03:37attributes in my code.
03:38For instance, you can see I have got this class down here that is a
03:42DataContract, and I've got an interface up here that is a ServiceContract, and
03:47I've got a method here that has an OperationContract on it. That's saying that
03:51this operation or this function is exposable from the web service.
03:56In order to make the application work, I need to set a configuration.
04:01Both the service that's running on a server somewhere and the client need to
04:04have a configuration file saying how they plan to communicate.
04:07To make it easy to create a configuration file,
04:10you can right-click on this App.config and choose Edit WCF Configuration.
04:16Now the details of how to set this up are beyond the scope of this introduction
04:23to web services. Just let me point out this one piece.
04:27Here is this service that I am exposing, BookServicesV1.Book, and underneath
04:32this, I can see the Host.
04:33I am not sure if this is going to work correctly on your machine.
04:37On my machine, I have localhost set up, and I have this hardcoded port, 8732.
04:44I believe if you open this application on your machine, it should use the same port.
04:48If not, you'll need to investigate which port is running on your server and
04:52change this number to match your machine.
04:55Rather than setting up this configuration, I already have a second version of
04:58this application that's set up correctly.
05:00So I am going to close this dialog, and I am going to show you BookServicesV2.
05:10Inside BookServicesV2, I have a class called BookData, which has a BookId,
05:16Title, Author, and Price.
05:18Inside my BookService, which you remember is my Concrete class,
05:23I have a private field that contains a list of BookData.
05:30I have a constructor that fills that list full of instances of BookData and
05:35then down in this section, I have added some methods: AddBook, UpdatePrices, and GetBooks.
05:44You might remember that I said, in my IBook services, I need to set up my contract.
05:49So if I look over here, you'll see that I have a contract that says I have an
05:53Operational contract for the AddBook method, the Update books, and the GetBooks.
05:59So I've got those all set up correctly.
06:01Next, I go to my configuration file, set that up, and then lastly, I am
06:06ready to test this out.
06:07So I am going to start the application.
06:15Notice down here in the lower right-hand corner, it says that my service has been hosted--
06:20that means it's running on the server--and click here to view
06:23detailed information.
06:28That's the one I wanted to see.
06:29I actually have two services running right now.
06:32I have the new service I just added and the existing service, and it took them a
06:37little while for the second service to show up.
06:39This is the one I was showing you earlier today, and here is the one,
06:42the finished version.
06:43I am going to close these dialogs down, and I am going to unload this
06:50BookService, so it's not too confusing.
06:52I am going to right-click on it and choose Unload Project.
06:57Now, when I run the application, it won't compile or run that service.
07:01It shows my service has started.
07:12This is the end point.
07:14If I have a client out there somewhere that wants to call this service, this is
07:17the address that you are going to go to. And because I don't have a client,
07:26Visual Studio creates this testing harness called the WCF Test Client.
07:31This shows that my service exposes three items through the operational contract:
07:35AddBook, UpdatePrices, and GetBooks.
07:39So I can double-click on this GetBooks functions, and then come over here
07:43and click on Invoke.
07:45So I am now taking the role of a client who is going to call that service.
07:49I am getting warned that I am doing something where the information is going to
07:53go over the Internet, and people could see what's in that call to the server.
07:56I am going to go ahead and say OK to this message.
08:00It's contacting the server and then down here in the bottom, it shows it
08:03returned a list of three items.
08:05There is a book with an Author called Walt Ritscher, and another book down here
08:09with Sam Pearson, and down here is one for Sarah.
08:12All right, I can choose this UpdatePrices, enter the new increased amount, 1.2
08:22and click on Invoke.
08:24That method is going to go to the server and change the prices on the book.
08:28So now if I call GetBooks again, and invoke it, I should see some new prices.
08:35I would like to remind you that you have to run Visual Studio as an
08:38administrator if you want to invoke this test client, like I am doing here, or if
08:43you want to try out the service. It's review time.
08:47What have I done in this movie?
08:48I created a service, and I configured it to run on my local server.
08:52I exposed a method on the service through a data contract.
08:55In the next movie, I am going to show you how to use this service from a
08:59client-side application.
Collapse this transcript
Using an existing WCF service
00:00This movie shows how to use an existing WCF service from a client application.
00:05For debugging purposes, I will keep the service project and the client project
00:09in the same solution.
00:11Remember, it's important that you run Visual Studio as an administrator if you
00:15want to test the service.
00:16I am inside Visual Studio, and I've opened a solution called UseWcfServices.
00:22If you look at the top of my Visual Studio, you'll see the word "Administrator",
00:25which means I am running in Administrator mode.
00:28There is a BookService available for my consumption inside this project.
00:32This is similar to the one in the other movie in this chapter. And I have this
00:37WPF application, which is going to serve as my user interface.
00:40Let's review the user interface.
00:42I am going to come down here and look at MainWindow.xaml. Double-click.
00:46There is a list box on the left side of the screen that I wish to populate with
00:53the names of the books that are available from the service.
00:56Then when I click on this Button, I want to add a new book to the collection.
01:00The first thing I need to do is tell Visual Studio where to find my service.
01:05I do that by going to my project and choosing Add Service Reference.
01:12Next, I need to provide the location where the address lives.
01:15In my case, it's living on the same server as my client application.
01:20It might also be out there on the Internet somewhere.
01:22I can type a URL here. Or since I know that my service lives on the same
01:26computer, I can click Discover, and wait for it to define the service.
01:30Then when I click on this expand node, it's going to launch the service--
01:35notice down here it says WcfSvcHost-- and then it shows here is the service,
01:41there is my interface, and there are the tree operation contracts.
01:45There is a method called AddBook, a method called GetBooks, and a method
01:49called UpdatePrices.
01:50I want to be sure that I can call this service asynchronously, so I am going to
01:55click on the Advanced button and then check this Generate asynchronous
02:00operations check box before I continue.
02:05I also need to give my Namespace a decent name.
02:09I am going to call this one BookService.
02:12Everything looks okay, so I will click OK.
02:18At this point, I can call a service as if it were living in a local DLL.
02:22So I am going to switch over to my Code view by clicking F7, and I am going to
02:28instantiate an instance of my service right here.
02:45Then inside my Windows Loaded event, I am going to grab the data from
02:49the GetBooks method.
03:02Here it is, GetBooks, open and close parenthesis.
03:05Then I need to tell the list box which property to show in the list.
03:09So I'll do that here by saying DisplayMemberPath= and the name of field or the
03:17name of the property.
03:18In my case, it's called Title.
03:20I am making this call on the same thread that's running inside this procedure,
03:24which in my case is the UI thread.
03:26So what's going to happen is when the application starts up, it's going to go
03:29out and call the service. And since I am doing it on the UI thread, it's going
03:33to block and wait until the service returns before it runs the next line of
03:36code, which will cause a small delay at my start up time.
03:39I am going to press F5 to test my application.
03:48The service host starts up.
03:50Notice that I have no UI for about two seconds, and then once the service has
03:55returned, this list box now contains the data.
03:58It would be better if I didn't make this call on the UI thread, but to make
04:02it on a worker thread.
04:03So when I click on this button to add a book to the collection, I'll do that on
04:07a background thread.
04:09I'll shut this application down. Return back to my code.
04:13On this line of code, I am going to declare a variable of my BookData type, and
04:35then my last one, I am going to choose a title, since that's what we were
04:38looking for in the list box. Say Title=. Next, I want to add the book,
04:52so I am going to call my service.
04:57You might remember there was a method called AddBook. Because I did the
05:01asynchronous service, you see that I have an AddBook method, an AddBookAsync,
05:06and I also have this event that fires when that method returns, AddBookCompleted.
05:12So I am going to start by setting up my event procedure.
05:14I'll choose this item and choose a plus, equal.
05:17Visual Studio is offering to stub some code in for me.
05:20I am going to press Tab once to complete that line of code, and then I am going
05:24to press Tab a second time to stub in the method.
05:27Now I am going to invoke the service, AddBookAsync, passing in the instances
05:38of my book as a property, and then I am going to go down here and update my
05:42list box when it's done.
05:51This is the same code I had up here, ItemsSource = _service, I gave this.
05:54I think it might be faster if I just copied this and pasted that in down here.
05:59If I did everything right, this service should add a book. Let's try it out.
06:04I am going to press F5, and I will click on the book one time.
06:11There is our new book added. And just for good measure, I'll click on the book a
06:15second time to verify that it's still working.
06:17As you can see, it is fairly simple to add a reference to the service.
06:21Before I end this movie, I want to point out that there are other layers built
06:24on top of WCF, including WCF RIA Services and WCF Data Services.
06:31Both are beyond the scope of this course, but I encourage you to check them out
06:35as you become more familiar with .NET.
Collapse this transcript
Navigation UI designs with the Document Outline view
00:00Navigating through a complex tree of controls in a real world application can be
00:04a frustrating ordeal.
00:06If you have a design that has hundreds of nested controls, it can be a lot
00:09of work just to find the control and select it in the designer.
00:13To aid in this kind of navigation, Visual Studio includes a Document Outline window.
00:18I am inside a project inside Solution Explorer called UsingDocOutline.
00:25It contains three projects:
00:27an ASP.NET application, a Windows Form application, and a
00:32WPF application.
00:33In order to navigate through this HTML page over here in the ASP.NET
00:38application, I need to have my Document Outline window opened.
00:43It may not be showing up on your machine.
00:45In order to make it visible, you need to choose View > Other Windows.
00:52Then on this sub-menu, choose Document Outline.
00:57This window usually lives on the left edge of the screen.
01:00As you can see in my computer, it's over here, and it's docked at the
01:03bottom half of the screen.
01:05I have selected the body tag inside my HTML document, and you can see that it's
01:10selected over here in the Document Outline.
01:13Naturally, I can navigate to the HTML element by clicking on it here inside
01:17the Document Outline. And notice that it selects the entire set of
01:21sub-elements by doing that.
01:23As you might guess, clicking on title will only select that one child element.
01:27There is also a nice little Navigation bar down here in the bottom in HTML
01:32documents where I can see HTML node as well.
01:35That's not part of the Document Outline, but it's another handy navigation tool.
01:40Next, I'm going to switch over to the Windows Forms application and open this Form1.cs.
01:46I probably need to make my window a little bit wider over here to see what's going on.
01:52In this UI, I've got the Tab Control that contains some group boxes and within the
01:59group boxes are some radio buttons and checkboxes.
02:03So of course, I can select this radio button in the group box and see what
02:06happens in the Document Outline?
02:08It shows up as selected over here.
02:11I can then click on this check box here, and it will automatically select it in
02:15the second group box.
02:16This is what I mean by when you have a complex nested UI.
02:19Sometimes you can't even get your mouse to the point where you can click on
02:22something in the designer. But with this Document Outline, I can select it and then see it.
02:28I can also move items from one element to another.
02:31For instance, I can take this check box and drag it and drop it inside this group box.
02:37Now, it has become a child of that group box.
02:42In WPF, I can do the same thing.
02:44Here, I'm in the WPF application.
02:47I am going to open the MainWindow.xaml file.
02:52Again, this is a complex UI here.
02:55WPF has added one extra twist.
02:57When you hover over the item in the Document Outline, you get a Preview
03:01window that pops up that's showing all the children elements of this grid
03:05and what they'll look like.
03:06It's like a miniature thumbnail. And as I drill down into my object model,
03:11let's say I click on this TreeView here or hover over it, you see that I'm seeing
03:15a subset of that UI.
03:16Now, I'm only seeing the TreeView. I'll click here.
03:20It gets selected in my designer and selected in my XAML view.
03:25Also, if I select the items in the designer, you see that they are selected over
03:31here in the Document Outline.
03:32So as you can see, the Document Outline window is a simple concept, but it
03:36provides a handy way to navigate complex UI in many different project types.
Collapse this transcript
7. Digging Into Your Data
Creating a data project with SQL Project
00:00Here is a problem that torments developer teams everywhere.
00:03If you've ever worked with database changes and multiple developers, you know
00:07that keeping your database schema in sync is a never-ending task.
00:12If someone makes a change to the database schema, you must ensure that that
00:15change propagates to each developer's computer, makes it to the test team, and
00:20gets updated on all your production servers.
00:23The trouble is compounded when debugging previous versions of your product.
00:28Now you have to ensure that the database matches the code that is stored in
00:32the source control.
00:33Some teams attempt to solve this problem by having a folder full of SQL scripts
00:38that contain the information to update the schema.
00:40These scripts can quickly get out of sync with the real database schema, however.
00:44Believe me, I know this from first-hand experience.
00:48To solve these problems, Microsoft created a special database project known as SQL project.
00:53Before I continue, I should add that these features are only available in Visual
00:56Studio Premium edition or Ultimate edition.
01:00If you do not have either of these editions, you will not be able to follow
01:03along with my demonstrations.
01:05I'm inside Visual Studio, and I've opened a solution called SqlProjects.
01:11This has a project called UseExisting that I'm going to show you in about two
01:14minutes. But first, I want to show you how to create a brand-new SQL project.
01:18I'm going to right-click on this solution and choose Add > New Project.
01:25The projects I'm looking for are down here near the bottom of the Installed
01:29Templates section, in the Database node, underneath SQL Server.
01:34As you can see, there are several types of projects here.
01:37The one that I want to use today is called the SQL Server 2008 Wizard.
01:43I'm going to come down here and have it generate a new BookSeller database, also
01:52in V1, like that, and then click OK.
01:57Now the wizard will launch and I get the Welcome screen, which I'm going to skip.
02:03The next type is what kind of project do I want to create?
02:06I'll create a user-defined database project, and I'm going to choose to store
02:10the script files by type of object.
02:12Then I'm going to click on Next.
02:16These are the standard settings for the SQL Server database,
02:19so I'm going to leave them as is.
02:22Then I'll click on Next.
02:24At this point, I'm creating a brand-new databases,
02:28so I would be responsible for creating all of the scripts for creating the
02:31tabled, the start procedures, the triggers, and everything.
02:35I bet that you already have some databases in your organization.
02:38If that's the case, you could click on Import existing schema, go over here and
02:44choose your database, make connections to the server, tell it which database you
02:51want, and then when you clicked on Next, Visual Studio will go out and import all
02:55the information from your existing database.
02:57Now since we don't have a full-blown version of SQL Server installed in this
03:02machine, I am not going to demonstrate this.
03:04Later, I'll show you a schema I did get from an existing database.
03:09At this point though, I'm going to uncheck this Import existing schema and then
03:12click on Next, and then verify that everything looks the way I want and click
03:18Finish and click Finish one more time.
03:24At this point, I have created a Visual Studio project that contains all the
03:28information about creating a brand-new database.
03:32This can be stored in source control. Think about this for a second.
03:36This becomes very beneficial.
03:37Imagine this scenario: Each developer in your team works with his own local
03:41server instance of that database.
03:43When one of your developers makes the schema change--let's say he adds a
03:47new column to the table--he'll do this not in the real database, but inside this project.
03:53He'll go over here to Schema Objects > Tables.
04:01We're going to add a table here called Books.
04:07So the change would be made here.
04:10You would write the SQL script here to modify the name of the column, or change
04:15the name of the column.
04:16Then I can check this in the source control.
04:19All the other developers can check out this item from source control and then
04:24they can run the Update Database feature that's inside this project.
04:29That will update their local copy of the database with the changes, and every
04:33developer can do that.
04:34Also, your SQL Server administrators can do the same thing on your production
04:38and your test servers.
04:40Rather than walk through creating a bunch of tables and start procedures, I'm
04:43going to show you the second project that I have here, called UseExisting.
04:48I ran this against the NorthWnd database. So if I come down here and show you
04:53the table section here, you'd see that it contains a number of table schemas.
04:58I'll double-click on this Shippers.table.sql. And this shows how to create the
05:05table inside your local copy the database.
05:09There are plenty of other interesting tools.
05:11There is a Data menu up here that has something called Schema Compare.
05:16What I can do with this is check two different databases.
05:21I can look at this project's schema, and this UseExisting project's schema, and
05:27when I compare the two of them, what's happening is it's looking at the
05:30differences between the tables and all the other entities in this database and
05:34showing me what's missing.
05:35For instance, if I look down here you'll see that I have a Books table in the
05:41BookSeller, but I don't have it in the using existing. So then I can say, create
05:45that table and then the next time when we run this script against the database,
05:48it's going to add that table to the UseExisting database.
05:52There is also another tool called Data Compare.
05:57What this does is you point it at two separate databases, and it looks at all
06:01the actual data that's in the database and make sure that they match up.
06:05And there is also a Create Test Data feature, where I can tell it to generate
06:10fictitious data that I can use on our testing applications.
06:14Well, now that we've got this project created, it's time to see how to create
06:18design time data and modify database objects.
06:21That's the topic for another movie in this chapter.
Collapse this transcript
Clarifying the confusion on .NET Data
00:01Microsoft really loves creating data framework and tools.
00:04They love it so much that they create a new framework every few years. And when
00:08they aren't building a new API, they have teams enhancing and improving existing APIs.
00:13As a result of their ongoing efforts, it can seem overwhelming when choosing a
00:16data access strategy.
00:18My goal for this movie is to explain the general layout of the data
00:21infrastructures in .NET.
00:24There are several areas in .NET of interest to the data programmer.
00:27Here's a list of the upcoming topics in this movie.
00:31Let's start at the beginning.
00:33ADO.NET shipped in the original release of .NET and has seen steady change over the years.
00:39It works with data in many sources.
00:41This is done by creating a specialized data provider class that knows how to
00:45talk to sources like Oracle, SQL Server, Access, and ODBC.
00:50There are many useful classes in ADO.NET.
00:53There are classes that communicate with the server, like Command and Connection,
00:58and there are classes that permit storing a cached copy of the data on the
01:01client, like Dataset, DataTable, DataColumn, and more.
01:06An extension of ADO.NET is called Typed Datasets.
01:10These are generated from a tool and are based on available data schemas.
01:14Because of the generated class structure, they are easier to use in
01:18your application code.
01:19Datasets are generated from the command line by the XSD tool.
01:24In Visual Studio, you can use the Dataset Designer to create and edit the typed datasets.
01:28Here you see a class called Categories, which is a special version of an
01:33ADO.NET data table.
01:35At the bottom of the Categories view, there are details about the
01:38CategoriesTableAdapter.
01:39Table adapters are responsible for communicating with the data store.
01:44Here you can see that we can call the Fill, or GetData method, to retrieve data
01:49from the underlying store.
01:51Also, notice the relationships between the Categories, Products, and
01:55Suppliers data tables.
01:58In recent years Microsoft has created a new querying language that is becoming popular.
02:02It is known as LINQ, which is an acronym for Language Integrated Query.
02:06It is written directly in your C# or VB code.
02:10Here are two examples of a LINQ query.
02:12Both are written in C# language.
02:14On the top query, we are using LINQ to Objects.
02:17The var q represents the potential query that we're going to run at some future point.
02:23As you can see from that first line, we're querying the String class,
02:27calling its GetMethods.
02:29Then we're using familiar words, like orderby and group, to further query the data.
02:34If you look at the bottom query, which is a LINQ to SQL query, you notice the
02:39similarity between LINQ to SQL and LINQ to Objects.
02:43There are many different versions of LINQ, each one optimized for querying a
02:46special type of data.
02:47Besides the two we've seen, there's also a LINQ to XML and LINQ to Entity.
02:52Finally, there is Entity Framework.
02:54This is Microsoft's version of an object relational mapper, often called an ORM.
03:00ORMs are popular because they permit a developer to think in simple terms like a
03:03customer and products, while the ORM maps those objects to the underlying data in the database.
03:09There is more to data access.
03:11Be sure and investigate the services layer in .NET.
03:14There you will find web services, ADO.NET data services and WCF RIA Services.
03:21While it may be a struggle to understand all the nuances of the data layer in
03:24.NET, at least you can be confident that Microsoft is constantly working on
03:28improving their data story.
Collapse this transcript
Using ADO.NET in your application
00:00ADO.NET is the core library for accessing data from relational and ODBC databases.
00:06There are a number of specialized namespaces, but for today's example, I'm going
00:10to use the SQL client classes.
00:12I am going to start by showing you some basic ADO.NET code.
00:14I'll show you how to connect to a database.
00:16We'll execute queries against the database using the command object, and then
00:20I'll show you how to store data in a local cache, known as a DataSet.
00:24I am inside of Visual Studio, and I've opened a solution called WorkingWithAdo.
00:29Inside that is a project that already contains a database called northwind.mdf,
00:35and it contains a form, Form1.cs.
00:37If I open up this user interface, I'll see there are three buttons and one list box.
00:43I am going to start by showing you how to connect to the database.
00:46I'll do that by double-clicking on this Connect button, which will take me to my C# code.
00:53To get a little more screen real estate, I am going to close this output window
00:56on the bottom of the screen.
00:59Before you do anything with the database, you need to create a connection.
01:01I am going to use this called SqlConnection.
01:04I then need to provide a ConnectionString that explains all the details about
01:08where to find the database.
01:10In my case, it's going to look in a folder called DataDirectory, and it's going
01:14to look for a file called northwind.mdf.
01:18To open the connection, I'll take my connection object and call open.
01:25To close the connection, I'll call the Close method. And then to verify that the
01:32connection is open, I am going to type in "MessageBox.Show".
01:37Then I'm going to ask the connection for its current state, like this line
01:47of code. And then I'll copy this message box code and paste it after the conn.Close.
01:55I am going to press F5 to run the application, and then say yes to this dialog.
02:02Then if I did everything correctly, when I click on this Connect To DB,
02:05I see the Open dialog and then immediately after that I see the Closed dialog.
02:16Once you've opened the connection, you can instruct the database to
02:18manipulate the data.
02:20You're probably familiar with the four basic operations you can do in a database,
02:24sometimes called the CRUD operation:
02:26you can create data, read the data, update the data, or delete the data.
02:32In SQL, we use the insert, select, update, and delete queries.
02:37So what I am going to do on this second button click procedure down here is I am
02:43going to open my connection, and then I am going to declare a cmd object.
02:57Next, I specify the CommandText that I want to send over to the database.
03:04This will be my SQL text. So I'll say Select * from shippers, which is the
03:12table I am interested in.
03:17Then I am going to tell the command object to use the connection to talk to
03:22the database, like so.
03:26Now, the rest of this code, I am going to uncomment, Ctrl+K, Ctrl+U. This
03:35executes the reader against the database, which is going
03:38to return some rows.
03:40I checked to see if there are any rows in the reader.
03:43If there are rows, then I call Read, which gives me the first piece of data,
03:48and then I call reader.GetSqlString and I go get the column with the number 1 from the data--
03:56that's the first column of data--and I am going to add that to the ListBox.
04:01Then I have this while loop that continues to read until it's empty.
04:05Then I close the reader, and then down here I've got another
04:10query against a database. I am going to do an update query.
04:13So, I have another command text what I am going to call as Update Shippers Set
04:17CompanyName, or shipper id, and then I am going to create a random number here,
04:22and I'll execute that query.
04:23I don't need to iterate over the results, because I'm inserting data, or updating
04:28the data into the database.
04:30I'll run this application, press F5, and there is our shipper data.
04:41For my last demo, I want to talk about the DataSet.
04:43Ado contains this class called the DataSet.
04:47This acts like an in memory disconnected representation of your database.
04:52Like a database, a DataSet can contain data tables consisting of rows and columns.
04:58You can also define data relationships, constraints, and other database-like
05:01constructs, but remember that the data set is stored locally and is in memory only.
05:08So this last button's code down here is going to create the DataSet.
05:12So here, I declare a DataSet.
05:15Think of this as an in memory database.
05:17I am declaring a table like this shipper table here, and then I
05:25create what's called a Data Adapter.
05:27It's the DataAdapter's job to fill the table and to push the data back into the database.
05:33When I have the shipper data, I can then create a SelectCommand, which is like
05:37the command I created up here, and I am saying here is the Select statement.
05:42Now, unlike up above, this data adapter can hold the SelectCommand, and it
05:48also can hold text for the UpdateCommand, for the inserting of data, and for the deleting of data.
05:54So it can do all four of those operations.
05:56I am doing the same thing down here with the productAdapter.
06:00I am creating a product.
06:01I am saying select start from product.
06:02I'm adding the Shippers table to the DataSet.
06:05I am adding the Products table to the DataSet, and then finally I'm calling Fill
06:11and say, "Here's the DataSet.
06:13Go out and get the data and put it inside the Shippers table."
06:17My last step is to take the data from the Products table and put it in the list
06:23box via the DataSource property.
06:26Let's see if this works.
06:28Press F5, click the Fill DataSet, and there is all our product names from the database.
06:34Now, as you can imagine, this is a lot of work.
06:38I have to create all of my DataSets by hand.
06:41So there is a wizard that is part of Visual Studio that generates these
06:44DataSets on my behalf.
06:46That's the topic of the next movie in this chapter.
Collapse this transcript
Creating typed datasets
00:00Datasets are the basis of most of the drag-drop designer tools in Visual Studio.
00:06The dataset acts like an in-memory disconnected representation of a database.
00:10As you can imagine, creating a dataset by hand is a tedious process. Fortunately
00:15for us, Visual Studio contains a tool for generating datasets. These types of
00:19Datasets are known as strongly typed datasets in some circles.
00:23The benefit of using the tool is it quickly creates all of the Dataset parts.
00:28The benefit of having a strongly typed Dataset is that all the columns are
00:31correctly named and typed, so a column in the database that contains a double
00:35value will have a column in the dataset of type double.
00:39I'm inside Visual Studio, and I've opened a Solution called CreatingDataSets,
00:44which contains one project.
00:46This project contains a Windows Form with three buttons and a list Box.
00:52I'm going to ask Visual Studio to create a typed dataset for me by going to my
00:56project and adding a database.
00:59I'm going to choose add an existing item, and then I'm going to go to my Desktop
01:09open my Exercise Files folder and scroll down to the Assets section.
01:13You need to be sure that your filter inside this dialog is set to All Files, so
01:20that you can see the database file.
01:24This is the database file I'm looking for, northwnd.mdf;
01:27I'm going to click on Add.
01:32Now, Visual Studio recognizes that it can generate a dataset from this database,
01:37so it asks me, would I like to create a dataset or an Entity Data model.
01:41I'm going to create the simpler of the two, which is the dataset, and then I
01:46want to click on Next.
01:47It just went out and talked to that database and has retrieved the list of all
01:54of the resources that are available in that database.
01:56Here is the listing of the tables.
01:59Within the tables, I can see things like the different column names.
02:02There is also a section that shows the Stored Procedures inside that database.
02:09I'm just going to take a couple of tables from the database. I'm going to take
02:13the Products table and the Shippers table, and I'm going to leave the dataset name the same.
02:20Next, I'm going to click on Finish.
02:26Now, inside my app.config file, that wizard generated this connectionString.
02:32I can then use this connectionString to connect to the database.
02:36In fact, all I need to do is talk to the dataset, and it automatically will
02:41connect and talk to the database.
02:42Let me show you what I'm talking about.
02:45I'm going to go to this Fill DataSet button and double-click on it.
02:51I'm going to declare an instance of the products table by typing "var products",
02:58the name of my dataset, which was "northwndDataSet.ProductsDataTable".
03:13This is the line of code where I'm going to create the TableAdapter.
03:21And as you can see there is a ProductsTableAdapter and a ShippersTableAdapter,
03:27I'm working with the products, so I am going to choose this one here.
03:30The TableAdapters job is to talk to the database.
03:34This is where it does the Select queries, the Delete queries, the Update
03:37queries, and the Insert queries.
03:39So next, I'm going to ask the TableAdapter to fill the products table.
03:43So I say ta.Fill and then I pass in the table that I want to fill, which in my
03:50case would be that instance of the products class.
04:00And then I'll tell my listBox to use the ProductName when it's showing the
04:06data. And lastly, I need to make sure that I fill my listBox. My listBox is called listBox1.
04:12I choose DataSource =, and then I'm going to assign the products table.
04:27Now, I see, I forgot to put a set of parentheses after this new line.
04:37It's nice to have IntelliSense.
04:38I'll press F5 to run the application and Y to dismiss that dialog, and I'll
04:45click on the Fill DataSet, and I'm now using that strongly typed dataset.
04:53You can see down, here in this example, I can also use the strongly typed access.
04:58The product has a UnitPrice property on it, which I can use to set or retrieve
05:02the unit price of a particular product. But I'd rather show you for the rest
05:06of the demo is how to create my own extra query against the database.
05:10So I'm going to open this XSD file that was created for me when I generated the dataset.
05:15This contains a designer surface for making changes to my dataset.
05:19I'll double-click on this file, and here you can see the Products table and the
05:26Shippers table, and then at the bottom you see the ProductsTableAdapter.
05:30Remember, this is what gets the data out of the database and puts it into the
05:34products table, and takes the changes of the data and puts it back into the data source.
05:38So I'm going to come down here on the bottom, and I'm going to add a new query.
05:43I like to choose SQL statements, a SELECT query. It's already stubbed in the
05:53code for a basic query. And I'm just going to come down here and say, Where
05:59UnitPrice > some parameter value @price.
06:09So now this is a parameterized query.
06:11I'll pass this piece of information in, and it will return all the products that
06:15are greater than this price.
06:17I'll click on Next.
06:18I'm going to call my method
06:20that is going to be on my TableAdapter GetDataByPrice, and I'll click on Next and Finish.
06:28Notice there is a new row here now.
06:32There is a GetDataByPrice.
06:34So now I'll go back to my form, and I'll go to this Fill Products by Price, and
06:43I just need to write two lines of code. var ta = new.
06:54I need that same TableAdapter, and now that TableAdapter has my new query on it,
06:59so I'm going to say listBox. DataSource = ta.GetDataByPrice. And watch what
07:08happens when I hit the open parenthesis.
07:11Over here it says I need a price parameter to give to the query, so I'm going
07:17to use 40M--M for decimal--and then I need to uncomment this line of code and this line of code.
07:25Here is a little trick. I'm going to highlight all four lines and then do Ctrl+K, Ctrl+U. Press F5 to
07:32run the application, choose to save the data, and there are all the products that
07:39are greater than $40.
07:42Now you have the general idea how to create a dataset.
07:44You still need to write code to work with the data, however.
07:47Watch the Binding movie in this chapter to see how to use the drag and drop
07:50designer to quickly set up data bindings in your user interface.
Collapse this transcript
Using the data binding tools
00:00Showing data from your business models is a common practice.
00:03I'd argue that it is the prime mission for both business applications.
00:07If you are an experienced programmer, you've probably worked on an
00:09application, or you retrieved data from a source, showed the data to the user in
00:13various user controls,
00:15allowed the user to change the data and then saved the data back to the original source.
00:20This is such a common scenario that our industry is constantly building and
00:23refining binding frameworks to simplify these common tasks.
00:27Microsoft is helping us in this area too.
00:28Visual Studio contains a number of data binding tools.
00:32Some of them work in the drag-and-drop manner. Let me show you.
00:37I am inside a solution called UsingDataBindingtools that contains a Silverlight
00:42application and a WinForms application.
00:44I am going to start by using the WinFormsApp for data binding.
00:49Now I have already added a database and a typed data set.
00:54See the "Creating typed datasets" movie to learn more about how to create
00:57these types of sources.
00:59There is a special window called the Data Sources window that permits drag-and-drop.
01:04To see the Data Sources window, you need to go to Data and then Show Data Sources.
01:10This window is usually docked to the left side of the screen, and on my computer
01:14it's occupying this lower left-hand corner.
01:17As you can see, it's automatically picked up on all the tables that are
01:20inside that data set.
01:21I am going to open this form called AddShipperGrid. And here is where the drag
01:28and drop-data binding comes in.
01:29I am going to take this Shippers table, and I am going to drag it over and drop
01:33it on the designer surface.
01:36And a lot a magic just happened.
01:38First of all, it created a data grid and added the correct columns.
01:42It added all of this stuff down here in the bottom.
01:44It added a dataset, and a TableAdapter for the Shippers and some other items.
01:50It added this thing called the BindingNavigator, which is this toolbar across the
01:54top for navigating forward and backward through my rows, and adding rows and deleting
01:58rows and saving them out to the data Source. And it's automatically wired up
02:04all the code to show the data.
02:06So we run the application, and it shows the Add Shippers Grid. You'll see that
02:15it's automatically populated, and I can use these forward and backward buttons
02:20to move to new rows, and so on.
02:24You can also add more complex grids.
02:27Here is a AddParentChild form.
02:29I am going to switch over and show you this one.
02:31I am going to add some existing controls.
02:34I am going to add a list box, and I am going to add something called the DataGridView.
02:44Here it is.
02:46So these already exist on the window.
02:48I can now take this Categories table and drag it over and drop it on this
02:53list box, and the data binding engine will add some sources on the bottom,
02:58and it also correctly configures my list box to have DataSource and
03:04Display name and so on.
03:06I am going to take the Products table and drag it and drop it on the grid.
03:11Now in my DataSource, if I open up the northwinddataset.xsd, you'll see that
03:19there is a relationship between the Categories table and the Products table.
03:22So I am going to exploit that in my code and make sure that when I click an
03:26item in the list box it will show the related product in the other control.
03:32To do that, I am going to click on this list box.
03:34I am going to add an Event procedure by clicking on this lightening bolt, and I
03:38am going to add SelectedIndexChanged Event procedure.
03:42Double-click on this section, and then I am going to say, productsBindingSource--
03:47which is one of those autogenerated items--
03:49.Filter =. And I need a semi-colon here, and then I am going to say, this is a CategoryID=.
04:03So what this is going to do is it's going to create a string
04:22that says CategoryID, and then it's going to use whatever the product name is
04:25here. The CategoryID number is going to put that in here, and it's going to
04:30apply that to the filter.
04:31I'll run the application. Click on this button Add Parent Child view.
04:37Now again, here you see the Beverages at the top.
04:41These are the beverages that we sell.
04:44Here is the condiments that we sell, and so on.
04:46There is the dairy products that we sell.
04:52In my next demo, I am going to show you that you don't have to use data grids.
04:56I am going to use this form called AddShippersControl.
04:58I am going to go back to that Shippers table in the Data Sources, and instead of
05:02dragging it over and getting a table, I am going to tell it to use Detail view.
05:06So I select this dropdown. You can see that the symbol changes over here.
05:12Now when I drag this over, I still get bound controls, only this time I get a
05:17text box that is bound to the source and a text box is bound to the Company
05:21Name, rather than getting a data grid.
05:23For my last example here, I am going to show that when I am doing this drag and
05:29drop--for instance, there is two dates here for this Categories--
05:34when I drag these over, it's going to use a special control called
05:37the DateTimePicker.
05:39I can tell that data sources that I would rather use as an alternative control
05:44when I drag it over to the surface.
05:47So I am going to choose this dropdown, and I am going to come down here and say,
05:50I don't want the DateTimePicker anymore. I want a TextBox.
05:53So now I will switch this over to Details view, and I will get one
05:58DateTimePicker and one TextBox.
06:01There is the DateTimePicker.
06:05There is the TextBox.
06:06That just shows you that you can customize the controls.
06:09So up to this point, we've been doing data binding using a database and
06:13this northwinddataset.
06:17Another source of data is .NET objects.
06:21So I am going to switch over to my Silverlight application.
06:24I will make this my Startup here, this ASP.NET application, and I am going to
06:30switch to my SilverlightApp, and I am going to go to this MainPage. And I want
06:34to drag and drop this class that are right here called CalenderDays.
06:39If you look inside this class, you'll see there is other class called
06:42CalenderDay, and then I wrote a class that returns a list of items.
06:47It derives from the list of CalenderDay.
06:50So what I am going to do is I am going to add a new data source to
06:54this SilverlightApp.
06:55So I will go to Data > Add New Data Source,
07:00tell it that the source of the data is going to be come from an object, tell it
07:04I want to use both these objects--
07:06CalenderDay and CalendderDays--and then click Finish.
07:10Now I am going to go to my designer surface.
07:14Here is my Data Source. I am going to grab that CalenderDays, and drag it over and drop it on my
07:20Silverlight surface.
07:21Now it has a slightly different way of setting up the connection.
07:25I need to go to my code behind window and write a little bit of code.
07:28So let me press F7 to do that.
07:31Here is all the code that was automatically written for me.
07:34It's really hard to read,
07:35so I have created a slightly easier version of that to read down here.
07:38I am going to copy this, go to my Loaded Event, paste it in, and then Ctrl+K,
07:46Ctrl+U to uncomment this.
07:49What this is going to do is it's going to go out to my MainPage.xaml, find this
07:55DataContext that was generated when I did the drag-and-drop--
07:58that's called the Calendar View Source--
08:00it's going to get a handle on that, and then it's going to instantiate an
08:06instance of CalendarDays and apply that to the source.
08:09Now if I did everything correctly, I should get a data grid populated with
08:12the days of the week. Press F5.
08:16I will press Yes. Success!
08:26Are all seven days of the week there? Yes, they are.
08:29Well, that's about all I have for drag and drop.
08:32It's simple to use,
08:33it's customizable, and for simple interfaces, it's just the right tool.
Collapse this transcript
8. Debugging Your Application
Debugging code
00:00Nobody likes to write code that contains bugs, but it is a fact of
00:03the programmer's life.
00:04So in this movie, I would like to show you the debugging tools that are available
00:08inside Visual Studio.
00:09I'm inside Visual Studio, and I've opened a solution called DebuggingCode, which
00:13contains one project called ExploringBreakpoints.
00:15Let me show you the main UI for this application.
00:18I'm going to double-click on MainWindow.xaml.
00:21It's a simple form that contains seven buttons.
00:24Each of these buttons contains one of the demos I'm going to show today.
00:26Before I continue, let's make sure we have one setting enabled in Tools > Options.
00:30I'll go to Tools > Options, and then you want to go down to the Debugging
00:35section, and go to Edit and Continue.
00:39Make sure there is a check mark in Enable Edit and Continue.
00:42This allows us to edit the code while we're sitting at a breakpoint.
00:46I don't believe this works on 64- bit operating systems, however.
00:49So now I'm ready to add a breakpoint.
00:53I'm going to switch to my Code view by double-clicking on the Simple Breakpoint button.
00:58Here is the code that I want to examine under a debugger.
01:02So I can put my cursor on line 37, and then I can press the F9 key.
01:07Notice that the line turns red, and on the left margin is a red circle.
01:12That signifies that there is a breakpoint waiting at this line.
01:15To turn off the breakpoint, I can press F9 again.
01:19Another way of adding a breakpoint is to click on the margin.
01:23Now that I have this breakpoint, I need to run the application under a debugger.
01:28The easiest way to do that is to click the Start Debugging or press F5.
01:35Now when I click on the button, we switch over to Visual Studio, and we're
01:39sitting here looking at the breakpoint.
01:41This line of code has not run yet.
01:43To run this line of code, I need to click on this Step Into button.
01:49Now you can see the breakpoint is still on line 38, but the next line of code
01:52that's going to execute is line 39.
01:56Another way of putting a breakpoint in your code is to write a little bit of code.
02:00I can come down to this section and write a System.Diagnostics.Debugger.Break().
02:05That means that when the debugger encounters this line, it'll
02:08automatically stop.
02:09Now, you probably don't want to leave this in your production code,
02:12so what you can do is wrap it in an if statement, like I'm doing here.
02:16I'm going to uncomment this by doing a Ctrl+K, Ctrl+U. Now, when I run this
02:24breakButton, I'll switch back to my UI,
02:29I'll press F5 to continue running the application, and then I can click on this
02:33button, and you see again I have a yellow marker sitting on that line.
02:36Now, there's no red indicator on this one because that you wrote the code to
02:40stop instead of adding a breakpoint. And then I'll press F5.
02:46If you have an exception in your code, and you don't have an exception handler,
02:50which is called an untrapped exception, then you'll get an opportunity to debug
02:55that if you're running under a debugger.
02:56Let me click on this button here.
03:00So I wrote some code that was attempting to open a file stream, and I passed it
03:04an invalid text file name.
03:07So when .NET went out to open this file, it couldn't find the file, which is
03:10what this exception error is I'm getting here,
03:12"Could not find file 'C:\bogus.txt'."
03:16This is a nice little window that shows me there's an error at this line.
03:21It switched over to a break, so that I can look at the code and see if I can
03:25figure out what's wrong.
03:26I can click on this View Detail button to learn more about the exception, and I
03:34can dismiss that dialog and then make any changes necessary and then try to run
03:39this application again.
03:40I'm going to stop the application for a second and rerun the application.
03:50Now, we're going to look at how to step through our code.
03:53I'm going to click on this button.
03:55I do believe I need a breakpoint on this line.
03:58So, I switched over to my code.
04:02This is the stepping code.
04:04I'm going to add a breakpoint right here on line 75, F9, Alt+Tab back to my
04:11Running application and then click on that button again.
04:13Now, I should say something about breakpoints.
04:16When you save your application, it's going to store your breakpoints in a
04:20special user-specific file.
04:22So if I open this file again tomorrow, my breakpoints will still be here.
04:25If you were to open this file--say I sent you a copy of my solution--you
04:29wouldn't have my breakpoints, because you get your own user-specific Solution file.
04:34I'm ready to run this line of code.
04:36Now, I've showed you earlier, you can click on this button;
04:38you can also press F11 to do a single step.
04:41Watch what happens if I do a single step by pressing F11.
04:44There is a function over here called CalculateMonthlyPayment, and now I've
04:47entered that function.
04:49Now, I can press F11 to walk through my code over here.
04:55Then I'll return back to the previous code.
04:58Another way of walking through your code is to click on this Step Over.
05:02What that does is it looks at this and says, "We'll there are six or seven lines
05:05of code over there, in that method. I'm going to skip over those.
05:09I'm just going to run this as if it was a single line of code."
05:11So, by pressing Step Over, I don't have to walk through that method that I
05:15already have debugged.
05:16See, when I clicked on that button, it immediately went to the next one.
05:19There is another setting.
05:22Let me click on the Step Into.
05:25Now I'm back inside that CalculateMonthlyPayment.
05:27Let's say that I've run a couple of lines of code in here, and I don't want to
05:32step through the rest of the code.
05:33I can use this Step Out button to finish running all the code in here and then
05:38stop on the first line of code back in the calling function.
05:41So let me show you how that one works.
05:43As you can see, it returned back to the calling method.
05:45You remember that Edit and Continue feature I turned on earlier?
05:49I can now rerun some of these lines of code by dragging this yellow marker up
05:53and rerunning that line of code by pressing F10.
05:56Another thing that I can do is I can now edit this code while I'm sitting at a breakpoint.
06:01Let's say I don't like the name of this variable.
06:02I'm going to have to rename it in several places.
06:13But now that I've renamed it successfully, I can press F10 to successfully run that code.
06:17So I'm able to edit the code while I was sitting at a breakpoint.
06:21Let me press F5 and click on the next button.
06:24Now, I've got three more examples I want to show you.
06:27So let me shut this application down and add some breakpoints before I continue.
06:31I'll press F9 while I'm on this DataTips.
06:33I'll press F9 while I'm down here on the visualizerButton, and then I'm going to
06:38add a special better breakpoint down in this area.
06:41I'll come back and show you that in just a second.
06:43Actually, I think I'll show it to you now.
06:45I'm going to add a special breakpoint here.
06:46One of the new things in Visual Studio is that you can add breakpoints that
06:50have special counts.
06:51So, I'm going to add a breakpoint here, and then I'm going to right-click on
06:54it and set Hit Count.
06:57Then I'm going to choose when the hit count is a multiple of 60.
07:01Notice that there's a special Plus sign now on the breakpoint?
07:09Let's run these last three demos.
07:10Again, I'm going to go up and choose Debug > Start Debugging.
07:13There is a special tooltip in Visual Studio called a datatip.
07:21I'm going to press F10 a couple of times to move down into my code, and then I'm
07:25going to hover over this variable.
07:27You can see the datatip.
07:28It tells me that the current value of that variable is 0.0.
07:32And if I come down here and click on this birthDate, it shows that it's 5/5/1992.
07:37Notice there's also a Plus symbol here, where I can see more specific
07:40details about that item.
07:42There is also a little stickpin over here.
07:46Click on this and then it adds the tip to my coding window.
07:49So now that you notice as I move to my code, that little datatip is now stuck
07:53there for me to keep watch on it.
07:54Let me press F10 a few times here.
07:57We'll run through this code.
07:58This is another feature,
08:02I can choose Run To Cursor.
08:03I'm going to click down here and right-click and choose Run To Cursor.
08:07Now it'll run down to the line of code where my cursor is sitting, and then I
08:11can hover over this to see that I have five items inside that SortedDictionary.
08:15Now, I'm going to press F5 to return back to the UI, and I'll show you this
08:20other cool thing called the Visualizer.
08:23I'll press F10 twice to run down to this curly brace.
08:26I've just loaded an XML file into this variable.
08:30If you hover over it, you can see that there is a lot of raw text in there,
08:33but there's this thing called the data visualizer, where I have the magnifying glass.
08:38I can click on this magnifying glass and then choose a different
08:41representation of the data.
08:42I like this XML Visualizer better than the raw Text view.
08:46That's what's currently stored in that variable.
08:48I'll press F5 one more time, and you probably remember just a few minutes ago, I
08:57added that special breakpoint.
08:59Now, I'm going to execute that breakpoint by clicking here, and I have just run
09:03through this loop counter and if you hover over the counter, you'll see that I'm
09:08at 60, because that was my condition I set up for the counter.
09:11If I press F5 again, I'll now have run through another 60 iterations.
09:17So rather than having me step through this over and over again, I can just skip
09:20forward 60 steps in this loop.
09:23These debugger tools that I'm showing you are just the ticket when you have an
09:26unexplained problem in your code.
09:28In the next movie, I'll show you how to use the Watch and other debug windows.
Collapse this transcript
Working with the Watch and other debug windows
00:00In this example, I will look at the watch, locals, call stack, and thread windows
00:05inside Visual Studio.
00:07I'm inside a project called UsingWatchWindows.
00:10This is a WPF application, and I have a little sample UI down here
00:13in MainWindow.xaml.
00:15Let's take a look at it.
00:16There are six buttons in here, and each of these buttons allows me to show you
00:22one of the windows inside Visual Studio.
00:25And before I show you the actual windows, I need to build my application, and
00:30then I am going to attach the Debugger by clicking the Debug > Start Debugging.
00:36Then I am going to click on this Watch Windows button.
00:38Notice that I have this code in here that causes the debugger to stop
00:42automatically if a debugger is attached.
00:45I talked about that in the previous movie.
00:47I am going to press F10 to step through a few lines of code.
00:51The idea behind the watch window is that it's a permanent repository for data
00:54that you want to look at.
00:55So if I want to keep this s1 around, instead of having to hover over this to see
00:59what's stored in there, I can just take it and drag it down and drag, drop it
01:04into the Watch area.
01:06And then when I see the data down here, and I notice that there's an error,
01:09I can fix as while I am at the breakpoint by double-clicking on the value in the watch window.
01:15Typing in the new value, hi, and it shows it red now that signifies I've
01:21just changed that value.
01:23Now if I hover over the s1 variable up here, you see that it now contains the new data.
01:27So I can edit the data and watch the data.
01:30Of course, I can drag more sophisticated objects down here, like this book
01:34object, and then I can click on this Plus symbol to expand the details and read
01:39more about this class.
01:41I don't have to use variables that I have declared.
01:44I can work with the local class.
01:47I'm inside this MainWindow.xaml file right now as I am running. So I could come
01:50down here and type something in, like the word "this," which refers to the current
01:55instance, and then I can read information now about this window I'm inside.
02:02Also, I can see information about some of the buttons in here, and I can also
02:06use completely arbitrary calls out to .NET framework.
02:10For instance, I could say System.IO. Directory.Exists, type in the path to a directory like so,
02:26and then it will evaluate that expression and tell me that no, that
02:29directory does not exist.
02:32Next I would like to show you the locals and autos windows.
02:34I am going to press F5 to finish running the code in here and return back to my UI.
02:39Click on that Locals and Autos buttons.
02:42Now I'm going to take a look at these two windows here and here.
02:46So the autos windows watches your cursor, and as you step through your code--I am pressing F10--
02:53it's keeping an eye on the line of code where the yellow marker is and several
02:57other lines of code around that marker.
03:00So right now I can see three variables.
03:02If I press the F10 key one more time, you will see that those variables change.
03:06Now compare that to the Locals Window.
03:09The Locals Window shows me all variables that are in scope inside this method,
03:14so that would be the sender, which is a parameter, RoutedEventArgs which is a
03:19parameter, plus also these other variables here.
03:22Now as I step through my code, I'm going to get to this line of code right here,
03:2568, and that's going to move me in to another function.
03:28So as I press F11 now, I have now entered the second function, and you will
03:34notice that the Locals Windows changes and shows me only the variables that are
03:37in scope inside that function.
03:40Pressing F11 a few more times, I will leave this function and return back to the
03:44previous calling function, and then again the Locals Window repopulates with the
03:48data from that area.
03:49I will press F5 to continue.
03:52Next, I want to show you the call stack.
03:55In this example I have a function called callStackButton_Click, and it's going
04:00to call a function called CreateLoan.
04:01So if I go over and click on this Call Stack button--and if you don't see
04:05these windows, you may need to go up to Debug > Windows and enable the different windows.
04:11As you can see, there are a lot of other windows I am not going to cover, for
04:15instance, there is the Register's window and the Memory window. So here I am.
04:18I am ready to make this call.
04:23If I look at the Call Stack, I see that I am inside this function.
04:26Now I am going to press F11.
04:27Now I have entered the second function, and the call stack now reflects that
04:31and says, "I am in this CreateLoan," and it was called from this button click procedure.
04:37One of the things I can do in the Call Stack window is double-click on the
04:40previous function, and it will take me over and show me where the code was where
04:45I entered this function.
04:46Then I can double-click on this one with the yellow marker, which takes me back
04:49to the current line of code is ready to execute.
04:52I will press F11 a few times. Now I have entered another class, and you can
04:57see the call stack in now three-deep. And then when I leave this, it goes back to being two-deep.
05:03Press F5 one more time.
05:04I am going to click on this button.
05:08The Immediate window lives over here, and what the Immediate window allows me to
05:12do is run one single line of code and evaluate it.
05:16So right now, I would like to call this function, Financial CreateLoan.
05:20So I come over here, and I will type in "Fin".
05:22There is Financial.CreateLoan.
05:29I pass the person's name in, and then I pass in the value of the loan--$5-- and
05:38I am going to press Enter, and that function just ran, and notice nothing
05:43happened up here, right?
05:44I am running this function independently.
05:46It was happening in this window, and then I get back the results from that function.
05:51I can also print any details I want by typing in the question mark, which is
05:55shorthand for print, and then I can type in something like "this.Width".
06:01Here I am referring to the window that I'm inside, and I can find out that I am
06:05525 pixels wide. Press F5 one more time.
06:10Next, I want to look at the Thread windows.
06:12So I am going to click on this button right here.
06:14I am inside this click procedure.
06:17By going in and choosing Debug > Windows > Threads, I can examine how many
06:26threads I am running.
06:27Now, you may not see the same number of threads that I have here.
06:30I have got two threads.
06:31It depends on whether you have this thing called the VsHost running or not,
06:36which is a special debugger helper.
06:38If VsHost is running, then you'll end up with four or five more threads than
06:43what I currently have.
06:44So I have got the main thread, and I've got one worker thread.
06:49I have another example--
06:50let me press F5--where I am going to spawn a worker thread to go do some extra work.
06:56Let's make this a little bit smaller.
06:59What I am going to do here is I am going to queue up a thread from the
07:03ThreadPool and tell it to go run the code in this OpenTheFiles method.
07:08So currently, I'm on the UI thread, because I'm running inside this WPF application.
07:13When I get down to this line of code, I will be running on a ThreadPool thread.
07:17So I can see the difference in this window.
07:19I am going to press F5.
07:22Now I have hit this breakpoint that's inside the OpenTheFiles function. And as
07:26I can see in this Thread Window, I'm now running on this thread down here, 1352.
07:31As you saw earlier, there are other Debug windows available.
07:34For example, there is the Register Window, which shows the data that is stored
07:38in the CPU registers.
07:40These additional debugging windows are adapted for special debugging tasks and
07:44worth investigating.
Collapse this transcript
Other debugging techniques
00:00Continuing our quest to master the debugging tools in Visual Studio, I am going
00:04to cover trace points, attaching a debugger to a running process, and debugging a web site.
00:10So let's get started!
00:11I am inside the solution called OtherDebuggingTechniques, and it contains two projects:
00:17AttachToConsole and DebugWebApp.
00:19I am going to start by showing you something called a tracepoint.
00:24To do that, I am going to double-click on this Program.cs.
00:27If you've watched the other movies in this chapter, you know that you can add a
00:31breakpoint by clicking in this margin or pressing F9.
00:35A breakpoint is hit if you're attached to it debugger, and you run this line of code.
00:39It will then stop and let you look at your code.
00:42A tracepoint is similar to a breakpoint.
00:44When you establish a tracepoint, you also specify additional action that can be
00:47taken when it is hit. Let me show you.
00:50I am going to right-click on this breakpoint and choose When Hit.
00:55Now, it doesn't actually say the word "tracepoint" in here, but this is where you
00:58would enable a tracepoint.
01:00I click here, and then I get to choose what happens when the debugger
01:07encounters this tracepoint.
01:08First of all, I can have an output information to the Output window.
01:12I can have it do this silently.
01:15If this Continue execution check mark is checked, then it'll print this output
01:20to the Output window, but it won't stop and let me look at the code.
01:23Let me add a custom string at the front of this, so we can find it easily, and
01:28then this is going to print the function name, and this is going to print the
01:31thread ID and the thread's name.
01:33As you can see, there are other variables I can inject in there.
01:36I can also do other actions like run macros, and macros are covered in another
01:41chapter in this title.
01:42Macros are little bits of code that you can run from within Visual Studio.
01:45I am just going to do this basic tracepoint.
01:48Make sure I uncheck this Run a macro first. Click on OK.
01:53Notice it has a diamond now instead of a circle?
01:55That's how you know it's a tracepoint.
01:58Then I am going to do a Debug > Start Debugging.
02:00It ask me to enter my name, it says, "Hello Walt," and now if I come over here to
02:08my Output window and scroll down to the end-- you may not see this window in your computer;
02:14if you don't, you need to go to View > Output--and here at the bottom of the
02:19window is my custom output string.
02:23Notice that it didn't stop my code I am running.
02:30So that's the tracepoints.
02:31Another common task that you might want to do when you are debugging is you
02:35might need to have an application already running before you attach a debugger.
02:39In other words, you don't want to attach the debugger at the Startup Process.
02:42Or another thing that's happening is you're running an application and five or six
02:46hours after you started running it, something happens and it doesn't look right,
02:48and at that point, you want to start attaching a debugger and taking a look.
02:51So, what I am going to do is I am going to run this AttachToConsole
02:55application directly.
02:57To do that, I am going to right-click, choose Open Folder in Windows Explorer,
03:03open my bin folder and my Debug folder, and run this AttachToConsole.
03:10Let me show you how it works.
03:11I'll type in my name, and there is the same code we saw before.
03:16Now, it's sitting there waiting for my input.
03:18I can go back to Visual Studio, go to my Debug menu, and say attach to process.
03:28This dialog shows me all of the running processes on my computer.
03:32I am going to look for one called AttachToConsole.exe.
03:38That's the one that I am currently running. And then I am going to click Attach.
03:49F9 put their breakpoint here, and then I will go back here and press the Enter
03:51key, which cause the next line of code to run. And as you can see it now hit my
03:55breakpoint. And now at this point, I can do any of my normal debugging task.
03:59I can take this yellow marker and move it to a previous line,
04:02I can examine the data in the variables, and so on.
04:07For my next demonstration, I want to show you how to debug an ASP.NET web site.
04:11So let me close this application by clicking this Stop button, and then I am
04:17going to go over to my solution and right-click on the DebugWebApp and tell it
04:21to make that my StartUp object.
04:23Now, this is a ASP.NET application.
04:27So, one of the things that you have to do to make an ASP.NET application
04:30debuggable is add a line of code to your Web.config.
04:36By default, Visual Studio does that automatically for you when you create a web project.
04:41If it doesn't have a debugging setting, then it'll prompt you and say, would you
04:43like to modify the Web.config?
04:45So, I have already done that on this application.
04:48So all I should have to do is open up this window here, and I'll show you the
04:57bit of code I have here.
04:58I have an asp:Button in here that has a Button1_Click procedure. And if I press
05:03F7, you can see the code behind.
05:05This is the ASP page.
05:07Here is the code behind for that, and I have added a breakpoint on this line
05:10here where I am getting the DateTime and putting it in this Label.
05:13I have also written a JavaScript button down here.
05:18Here is an input button. And it has an onclick, which says, you need to run this
05:23JavaScript called showMessage, which is up here. And I have added a breakpoint in
05:29my HTML for this JavaScript.
05:31So, what this is showing is that I can also debug client-side code in
05:35ASP.NET applications.
05:37So we have got two breakpoints, right?
05:38I need to attach a debugger, so I will choose Debug > Start Debugging.
05:44Visual Studio will launch the ASP.NET development server, and then it will
05:50launch my web browser, and it attaches the browser to this instance of Internet Explorer.
05:57Now, when I click on this button, you'll see that it switches me over to the
06:01code behind, and I can hover over the variables here. I can press F11.
06:07I can rewind my code just like before.
06:12Seeing my Output information here. There is all my locals and watch windows all
06:16work just the way you'd expect.
06:17Now, I am going to press F5 to continue, and now I am going to click on this
06:21button here which has the JavaScript code. And again, you'll see it's hitting my
06:25breakpoint in my HTML page. And again, I can hover over these items,
06:32press F11 to step through the code, and so on.
06:36Very handy and a welcome addition to web development.
06:39All in all, I'd say the Visual Studio contains a nice selection to debugging tools.
06:44In the next movie, I will show you the IntelliTrace tool, which changes the way
06:48you think about debugging.
Collapse this transcript
IntelliTrace historical debugging in Visual Studio Ultimate
00:00Visual Studio has an amazing new debugging feature called IntelliTrace.
00:04This feature is only available in the Ultimate edition of Visual Studio.
00:08So if you don't have that version, you will not be able to follow along
00:11with this demonstration.
00:13Now normally, I wouldn't show you features from the Ultimate edition as we are
00:17targeting the Premium edition for this series, but this feature is very
00:20interesting from a developer's perspective, and I thought it was worth a look.
00:24Also, let me add that testers within your organization can use this feature if
00:28they have the Microsoft Test and Lab Manager tool.
00:31So, what is IntelliTrace?
00:34IntelliTrace acts like a debugging recorder.
00:36While you are in a debugging session, it is saving information about the
00:40debugger state as you go.
00:42Later, you can look back at this historical data and replay the
00:46previous debugging session.
00:48To demonstrate this, I'm going to use this program called
00:50DebuggingWithIntellitrace. And I need to set up a couple of settings inside
00:56Visual Studio before I continue.
00:57They are over here in Tools > Options, and then in the IntelliTrace section.
01:04First, let's look in the General area.
01:07In Visual Studio, by default, IntelliTrace is enabled.
01:11Normally, it is set to this IntelliTrace events only.
01:13I've changed it to this second option down here.
01:16If you change to the second option, you're going to have to restart Visual Studio.
01:19If you choose the first option, that means tracing when you enter break mode and
01:23storing individual steps while you're debugging.
01:26If you choose the second option, you will store information about each function
01:30call, including the parameter information and values returned from the call.
01:35The second option we want to look at is in Advanced.
01:39This is the location where my recordings are stored.
01:43I'm going to copy this information, and then I'm going to click OK.
01:47I'm going to go over to my Windows Explorer, and I'm going to navigate to that location.
01:55You need to be cautious about storing files here because they are often deleted
01:59when you close Visual Studio.
02:00You can see there's already one trace in here.
02:03I'm going to come back here and copy this trace in a few minutes.
02:05Now, let's go do some debugging.
02:07I'm going to switch to Visual Studio, I'm going to open this Program.cs file,
02:12and you see I have a normal Visual Studio breakpoint in there.
02:15I'm going to press F5 to debug my application, I hit my breakpoint, and
02:20immediately you see there's something new happening inside Visual Studio.
02:24Over here is a IntelliTrace replay toolbar.
02:28So, what I can do is press F11 or F10 to step through a few lines of code, and
02:33then I can click on this Go To Previous Call and look at where I was a few
02:39seconds ago in my debugging session.
02:42And then I can return to the Live mode by clicking here.
02:44Now it's recording these steps in that IntelliTrace file.
02:47So let's record a few more steps.
02:49I'm going to spawn another worker thread to do some work.
02:52I'm going to ask for your name, and we're going to read the line.
02:56It's going to prompt me for the name, I'm going to type in "Joe", and I'll come
03:01down here and decide that I want to rerun that code.
03:03So I'm going to grab this yellow marker and drag it up and rerun those lines of code.
03:08Again, I'm pressing F10 to rerun those lines of code. And then I'm going to
03:14press F5 to finish the application.
03:16So, at this point, I have a recording in that folder.
03:19I'll go back and look in the folder.
03:21Here is the file I just created, and I'm going to copy this and paste it to my Desktop.
03:28As you can see, I have a few copies here.
03:30Now, here's where it becomes powerful.
03:33Imagine for a second that you've gone home, had a nice three-day weekend, you
03:38come back to work, and there is a bug that you need to look at.
03:42It's in this application we've been working on, this debugging with IntelliTrace.
03:45So you load that in, and then you go out to your hard drive and you take this
03:50recording file and you load it into Visual Studio.
03:53I can just double-click on this iTrace file to do that.
03:59What you see is a summary of all the things that were happening inside that application.
04:03It shows that I had several threads running, and then I can also get information
04:09about the system that was under test.
04:11Here is the system information about this computer.
04:13I can see I'm on a quad-core machine. And then down here, I can find that
04:18information about what was loaded into the memory at the moment.
04:22So there's two files down here:
04:23mscorlib.dll and DebuggingWithIntellitrace.exe.
04:27Now, let me show you where the power really happens.
04:30I can go into this Thread List and pick my Main Thread and then choose Start Debugging.
04:36Now realize, I haven't started a debugger yet.
04:39I've just been sitting inside Visual Studio.
04:41I'm going to click Start Debugging, and it pulls my historical data out of that
04:46file and shows what happened, the last step in that debugging session.
04:51Now, I'm going to load a new window over here from the Debug menu.
04:56It's called IntelliTrace > IntelliTrace Events.
05:01This shows every step that was recorded during that process.
05:04So what I can do is double-click on one of these steps to take me back to that
05:08location, just a single click. I can look at the Call Stack, the Local
05:12variables, a lot of information about what was happening as the tester or the
05:17developer is walking through the code.
05:19And you remember what I did earlier?
05:20I repeated my steps.
05:22I went here, and then I went back up to a line of code and re-ran that line of code again.
05:28So you can see that rewind happening there.
05:31I can also hover over these variables and see the data that was stored in there
05:35historically, remember.
05:36So, if I go back here, it's null.
05:38I'll run this line of code and then hover over this, and there is the previous
05:43version of that variable.
05:45This is really an amazing feature.
05:47Can you imagine your testers filing a bug and attaching the IntelliTrace file to that report?
05:52Now you can run their trace on your computer and see the state of every thread
05:57running on their computer at the time of failure.
05:59This has to be my favorite debugging feature, and I think its well worth
06:03the investment.
Collapse this transcript
9. Testing Your Application
Understanding Visual Studio editions and test tools
00:01In this movie, I want to clarify which testing tools are available within Visual Studio.
00:06There are four versions of Visual Studio that contain testing tools.
00:09Those are 2010 Professional and Premium, and also the Ultimate edition and the
00:15Test Professional 2010.
00:18Microsoft also provides an optional server product, known as Team Foundation Server.
00:22This server is the backbone of many corporate development shops.
00:25It serves as a repository for items considered crucial for your project.
00:29For example, it stores test cases and bug reports,
00:32also product work items.
00:34It also contains source control repositories, workflow generators, and progress reports.
00:39Testers get their own dedicated version of Visual Studio, called Test
00:43Professional, sometimes called Test Manager.
00:46Test Manager has to work in tandem with Team Foundation Server.
00:50It works closely with the items like test cases, test plans, and environments
00:53that are stored inside Team Foundation Server.
00:57Test Manager contains a testing c enter that is meant as a place for testers to
01:00execute their tests and activities.
01:02For example, team managers can do test management and testers can execute their tests.
01:09Test Manager also contains a Lab Center.
01:11The purpose of the Lab Center is to manage a virtual environment.
01:14You can configure different runtime environments.
01:16Individual virtual machines can be used to execute tests and retrieved later to
01:20re-simulate a testing environment.
01:23One cool thing about the Lab Center is that a tester can save a virtual
01:26machine with a bug report.
01:28When the developer checks out the bug, they can also check out the virtual machine.
01:33I'd like to talk about a few of the tests that are available inside Visual Studio.
01:36First, I want to talk about unit testing.
01:38This is available in all four of the editions I've mentioned earlier.
01:42A unit test is a way for a programmer to verify that the code is performing as expected.
01:47I will show examples of unit testing later in this chapter.
01:51In the Premium edition, you get code coverage tests and a light version of the
01:55UI testing framework.
01:58And finally, in the Ultimate edition, you get web performance testing and
02:01load testing tools.
02:03Also, you get the majority of the tools available in Test Manager.
02:07Overall, I'd say the testing tools provided in Visual Studio are practical and useful.
02:11Personally, I wish there were more testing tools in the lower-priced versions of Visual Studio.
02:17If you are on a budget, then consider using open source testing tools instead.
Collapse this transcript
Verifying your code with unit tests
00:00This chapter is about unit testing, which is a very developer-centric type of test.
00:04Let me start with my definition of a unit test.
00:07A unit test is an automated chunk of code that calls a class or a method and
00:11verifies that our assumptions about the behavior of the code under test are correct.
00:16Unit tests are commonly written using a unit testing framework.
00:19They should always be automated and easily accessible.
00:22Anyone on your team should be able to run the unit tests at anytime to verify
00:26that the code under test is working, and that no one has broken the build.
00:30Let's look at the unit testing tools inside Visual Studio.
00:32I am going to be working with this project called WorkingWithUnitTests.
00:35First, let me show you the code I am going to test.
00:38It's inside this Book class
00:40Double-click on the Book class and you'll see that this class has one property
00:44called Title, one property with a getter and a setter, and a property called
00:49Price, and then I have two methods:
00:51UpdatePrizeByPercent and SaveBook.
00:54And here is a question for you:
00:55how do you know about the text in your code?
00:58My general feeling is that you only need to text code that contains logic statements.
01:03So if your method has a loop construct, or an If statement, or it contains a calculation,
01:08it needs to be tested.
01:09Not all code qualifies, however.
01:11It's a waste of time to test simple property getters.
01:14So looking at this code up here, it doesn't make any sense to test this because
01:17there is no calculation going on.
01:19I will right my first unit test by going to the Test menu and choosing New Test.
01:24Within this dialog, I need to pick my testing language.
01:28I will choose C#, and then I will click on this Unit Test template.
01:32Most often, you can write your unit tests and the code at the same time.
01:37There are many in the developer community that advocate writing your test first
01:40before writing any code.
01:42This is known as test-driven development.
01:44I agree with many of their principles, but for this movie, I'm adding tests
01:48to a prewritten code.
01:49I am going to call my unit test BookUnitTest, and then I am going to click OK.
01:54Visual Studio realizes I do not have a test project yet,
01:57so it prompts me for name for my new project.
02:01That looks like a great name for a unit testing project.
02:04I will click on Create and then on OK on this dialog. Several things
02:09happened in my project.
02:11First of all, a new folder was added called Solution Items, with a few test items in there.
02:16Then a new UnitTest project was added, including this C# file down
02:21here, BookUnitTest.
02:22Let's see what's inside here. Double-click.
02:25Here is the class that is going to contain my testing code, and the way we tell the unit testing
02:29framework that this is a test class is by putting this TestClass attribute on
02:33the class itself. And then each method that I want to write that is going to
02:37test my code is going to be marked with this TestMethods.
02:41That's so that the unit testing framework can find this method.
02:44Let me show you another way of adding a unit test.
02:47I will use a wizard this time.
02:49I'll click on this Unit Test Wizard, verify it is going to go in a
02:53UnitTest project, click OK and then over here, I am going to save my
02:58changes to my project.
02:59I'm going to select the class that I want to test.
03:03Test this book class, and I only need to test the SaveBook book and the
03:07UpdatePriceByPercent methods.
03:10So I will unselect everything else and then click OK.
03:14This time, it wrote a slightly better test class.
03:17If you'll notice, my test methods now say SaveBook class, and there is a little
03:22bit of sample test code in here.
03:23Now I am going to delete these and put my own in here.
03:26I have already prewritten the code for these tests.
03:33They are in this Assets folder.
03:35I am going to first start by using this IncreasePrice.txt file.
03:39Double-click on this one and copy all of this code and then paste it in BookTest.cs.
03:46I think I just double-clicked on the title.
03:48I detached this from the window.
03:51The way you put it back is you Ctrl+double-click on the header.
03:54I will paste my code in.
03:57Here is my TestMethod attribute, and the way I like to name my tests is by using
04:01the name of the method that I am going to test, the scenario that I am testing,
04:06and the expected behavior.
04:07It makes for a long name, but any developer that's looking at this test knows
04:11what it's expected to do.
04:13Now, what am I doing in my test case?
04:14I am instantiating an instance of that class, the Book class.
04:17I am trying to verify that this UpdatePriceByPercent function works the way I
04:23want, which is I pass in a value, and it updates the price.
04:26So first, I am going to set a price for the book, then I am going to call my
04:30UpdatePriceByPercent, and then I am going to use one of the test classes, which
04:34is called Assert.
04:35I am going to use that to verify that the results are what I expect them to be.
04:39There are a number of Assert methods.
04:41If you look at the Assert class itself, you can see there are lots of test methods here:
04:47AreSame, there is a method called IsFalse, another one down here called IsNull, IsNotNull.
04:55I have picked one called AreEqual, and the syntax for this is you pass in the
05:01expected value and then you pass in the actual value, and the Assert will verify
05:06whether these are correct or not.
05:08So what am I doing?
05:09I'm asserting that I should have 10 times 0.05, and then I'm saying, now look at
05:16the Book.cs and see if it has that value.
05:18Let's run the test.
05:20To run the test, I will go to the Test menu, choose this Windows submenu, and
05:25choose Test List Editor. I'll then build my project, which will build my test and
05:31my regular project, and it shows two test methods. The one that I just wrote
05:35down here is the UpdatePriceByPercent.
05:37I will put a check mark here, and then I'll come up and run checked tests.
05:42At the bottom-half of the screen you'll see that there is a Passed result.
05:46It says Test run is complete.
05:48I had made one test. One test passed.
05:51You want to have all green lights down here.
05:53Now what happens if a developer comes along and goes to this book class and make a mistake.
05:58They look at this code and say, I am going to refactor this--maybe I should
06:02have been dividing.
06:03So they make this change, and they compile the application.
06:06I did a Ctrl+Shift+B to compile the application this time.
06:10Then when you run the unit test the next time--I will go over here and do
06:13Refresh--and then when I run my unit test, that developer immediately sees that
06:19change they made 10 minutes ago is causing one of our unit tests to fail.
06:23So they are going to back and look at me and say, what did I do to break the build?
06:27Let's go back and fix that code because I want all green lights in a few minutes.
06:31Go back over here.
06:31Let me switch this back to multiple.
06:34And I have got two more text I want to add to BookTest, and what I am verifying
06:38this time is that the SaveBook method is working the way I expect.
06:42So look what happens if I try to save the book.
06:44I look at the price that's currently stored in the Book class and if it's
06:47less then zero, then I throw an ArgumentOutOfRangeExeption.
06:51And if the price is larger than 90, I throw the same ArgumentOutOfRangeExeption.
06:56So when I write my test cases, I need to make sure that if you pass in a
07:00negative value, it throws that exception.
07:03So let's go to SaveBook1, and copy this code.
07:06I will go to my BookTest class and paste it, and then I might as well bring
07:12over the other bit of code while I am at it.
07:14I will go over here and get the SaveBook2 and copy this code and paste it in
07:22BookTest. Then I will build the project, and then I will step through the code and
07:28show you what's it doing.
07:31In this first test, I am instantiating the book, I'm setting a negative price,
07:36and then I'm attempting to save the book.
07:38In the second test, I'm doing exactly the same thing, only I am setting an $80
07:42price and saving the book.
07:44Now, I expect this method to throw an exception, so I put this attribute up here
07:48to tell the testing harness, it's okay if it throws an exception.
07:52In fact, I expect it to throw exception, and this is the exception I
07:54expect: ArgumentOutOfRange.
07:56Now we will go test our code.
07:58Switch over to the test editor.
08:00You see there is two new unit tests listed here.
08:03I'll check both of those and then run my tests. And down at the bottom of the
08:07screen, you see that the UpdatePriceByPercent passed, the negative one passed
08:12because it threw that exception, but this one is not throwing any exception, and why not?
08:17Let's go and look at my test code. Because I didn't pass any number that
08:21was higher than 90.
08:23So if I change this value to 90.01, rebuild my application, switch over to the Test
08:33editor and run my tests again, I get all green lights.
08:37This is what you're looking for.
08:39Every time you write code, you write a unit tests, you write the conditions
08:42that you want to test, and then you run your tests every time you make a
08:46change in your code.
08:47Make sure you haven't broken any of your pre-existing tests.
08:50Now there are other asserted conditions they should explore when you have
08:52time, and in the next movie, I'll show you another type of test called
08:56performance testing.
Collapse this transcript
Running performance and load tests
00:00I've found that many developers are not aware that you can set up and run
00:03complex web load tests for an ASP.NET Application.
00:07You can do this right from within Visual Studio, as long as you have the Ultimate edition.
00:12If you don't have the Ultimate version, you won't be able to follow along
00:15with this demonstration.
00:16I'm going to create two types of tests:
00:18a performance test which is intended to determine how fast some aspect of a
00:23system performs under workload,
00:25then I'll create a load test which is a simulated real-world test that exercises
00:30the web site by running the designated performance tests.
00:33I'm going to use his project called WebsiteToLoadTest to write my tests against.
00:38This is just a simple ASP.NET site.
00:41Now before you run the test, make sure you are running Visual Studio as an
00:44administrator; otherwise you will not be able to perform the load tests.
00:48Next, I'm going to run my application.
00:52Since I'm running on a development server, I need the copy the port number from
00:56my local host before I create my web tests.
00:59So I'll copy this, and then I'll close that browser window.
01:03Then I'm going to go to my Test menu and create a brand-new test.
01:07I'm starting by creating a web performance test.
01:09I'm going to call this one GoToAsp.
01:11I'll pick a language, C#--although it doesn't matter for this example--and
01:19then I'll click OK.
01:19Visual Studio realizes I haven't created a test project yet.
01:23I'll call this one BasicLoadTests and then click Create and then OK to this dialog.
01:31A number of things has happened.
01:33If you look in the Solution Explorer, you'll see there is a Solution folder has
01:36been created, also a new GoToAsp.webtest file has been created, and then Visual
01:42Studio has launched this version of Internet Explorer with this special plug-in
01:47running called the Web Test Recorder.
01:50What I'm going to do is I'm going to paste that URL in that I copied a few
01:53seconds ago. And now I'm going to go back and restart my web server because it's not running.
01:57So we will go back over here and choose Debug > Start Debugging. That looks better.
02:07So now what I'll do is I step through the test steps.
02:10The user is going to click on this link, which takes him to another site, and
02:15they're going to click on the Back button.
02:16Now, I'm done recording the test,
02:18so I'll click Stop.
02:22At this point, you'll see a dialog.
02:24You might have to wait a few seconds for this dialog to disappear.
02:27Next, I'm going to create a second web test by going to Test > New Test >
02:33Web Performance Tests.
02:34I'm going to call these one FailedLogin, place it in the same test project,
02:41verify that my browser is still running. It's not,
02:44so I'm going to have start my browser. And what the user is going to do this time
02:58is they are going to click on this Log In button.
03:02I'll type in their name and a bad password, and click Log In.
03:07It fails and then the user goes back to the homepage.
03:13That's my next test.
03:14I'll choose Stop, and save.
03:17Now I've got my two performance tests.
03:19I'm going to create a load test.
03:20Remember that a load test simulates workload on the site.
03:23Now I'm going to go up to Test > New Test > Load Test.
03:28I will go ahead and leave it as the default name, and then click on OK.
03:32Since I'm simulating users coming to this site, I am going to go through the
03:35Scenario system here.
03:36First, it's going to ask me to give the name for this scenario.
03:39I'm going to pick the defaults.
03:41I'm going to suggest that we have 25 users exercising the site.
03:45This is changeable, naturally.
03:47I'm going to skip over this one, and then I'm going to come in here and add
03:50those two tests I just created.
03:52I'm going to add the FailedLogin and then GoToASP, and then I'll click on OK, and then I click on Next.
04:01Here I can choose how to simulate what type of network connections our users
04:05are connecting with.
04:07So I can come here and choose Add, and say that some of our users are coming in
04:10on a 56K modem--about 10% are using that--and then I'll click on Add and have
04:17other network settings here.
04:18Now I don't want a actually keep these items in here, because if I do, it's
04:22going to install some extra network emulators which I don't want to wait for
04:25that process to happen.
04:26So I'm going to choose Remove, and Remove, and then click on Next.
04:31Here, I can pick the browser mix.
04:33Again, I go to the Add button, and say that we've got some people coming to
04:37you with Firefox 3.
04:3920% of our users are using Firefox 3, and 5% of our users are using Chrome too, and so on.
04:46So I pick these mixes, and I'm going to remove these for testing purposes today,
04:50and then I click on Next.
04:54This one allows me to add other computers to run against the server.
04:58I'm just going to use my single computer today, And then here, I'm going to pick
05:01the test duration time.
05:03I don't want this to be too long,
05:05so I am just going to type in 30 seconds for the test duration.
05:08I will click on Finish, and now I'm ready to run my test.
05:12I think before that, I should build my application.
05:14I will choose Build > Build Solution, and then I'll go to my Test > Window and
05:21choose Test List Editor.
05:22And notice I have three tests.
05:23I am only going to run the LoadTest1.
05:26I don't need to run these two individual ones because they are going to be the
05:28part of the LoadTest.
05:29I also need to verify that my web site is running.
05:32I will switch over here,
05:34go to my Test editor and run the test.
05:39After waiting 30 seconds, the test completes.
05:41I can then click on this hyperlink here to see my test results.
05:46I see that my test passed.
05:47I can also right-click here and choose View Test Results Details and see a more
05:53detailed result screen.
05:54Here, I can see a summary, when the test was run, how long it took, what were
05:59my slowest pages, and then I can look at a set of graphs, a set of detailed table information.
06:04I see that I had 13 total test run on FailedLogIn and 28 tests were run on
06:09GoToASP and how long each one of those test took.
06:13Quite a bit of details here about what's happening on your web site.
06:16There is more to learn about performance and load testing. For example, you
06:19should never run the load test from your developer computer.
06:22Instead, you should set up a testing ring and have it test the site.
06:25This enables you to scale your test to simulate a realistic runtime load.
06:29You can also create simulated users, or use data-driven tests to simulate users'
06:33search strings and other complex test scenarios.
Collapse this transcript
10. Deploying Your Application
Building your application
00:00Before you can deploy your application, you have to create an executable file.
00:04This is done by compiling your application.
00:06Simply compiling your code is rarely enough with modern applications, however.
00:10There may be many operations you must perform to get the source ready for the
00:13compile and tasks that need to be run after the compile is finished.
00:17There are tools available to help this process.
00:20They are known as automated build tools.
00:22Visual Studio contains a build tool.
00:24You use it every time you compile, run, or debug your application from
00:28within Visual Studio.
00:29It goes by the name of MSBuild.
00:32I'll show you how to customize your build process with Visual Studio, but first,
00:36let's look at the basic build tools.
00:38I am inside Visual Studio, and I'm going to create a new project:
00:41File > New > Project.
00:43I'm going to go to our movie chapter, which is out on our Desktop, and then I am
00:53going to create a C# console application. There it is.
00:57I'll just except the default names and click OK.
01:00Every time you press F5 to debug your application, you are actually compiling
01:04the application and then attaching a debugger.
01:07You can see the build process here in this menu.
01:10There are two parts of this menu:
01:12The top part lists the builds items for this solution.
01:15The bottom half selects the build items for the currently selected project.
01:20So right now, since I only have one project, choosing to build the console
01:23application, or build the solution, would accomplish the same thing.
01:26I'll click Build Solution.
01:28It goes through the build process, and when you're done, I now have a compiled
01:31executable somewhere in my hard drive.
01:33If I add a second project by right- clicking on the solution and choosing Add >
01:39New Project--let's say, for this example, I want to create a Class Library,
01:44I'll call this TextLibrary and then click OK--
01:48now when I choose Build > Build Solution is going to compile two different executables.
01:54How do we know what build order those items will run?
01:58You can control the build order by going to your Project menu and choosing
02:03Project Build Order.
02:04Here you can see that ConsoleApplication1 is going to be built first, and then
02:09TextLibrary is going to be built second.
02:11I'm thinking that's the wrong direction.
02:13I'd like to have my library built first, and then once that's finished, I can
02:17then use to code this in that from the Console Application.
02:20So to change the build order, I can click on this Dependencies tab and say that
02:24the Console Application depends on the Text Library.
02:29Now, if I go back to the Build Order screen, you'll see that it's reversed the order.
02:33Now, Text library will be built first, and then Console Application will be built second.
02:37There are two major types of builds you can do in an application, and they're
02:40listed in this configuration dropdown.
02:43You can see one called Debug and one called Release.
02:46Right now, I'm creating a Debug build.
02:49So the results of that are going to go in the Debug folder.
02:51Let me show you where that's at.
02:52I am going to click on this Show All Files button, I am going to open this
02:56bin folder, and right now you can see that my ConsoleApplication1.exe is in this Debug folder.
03:03If I switch over to Release build and then compile my project, you'll see that
03:12it adds a Release folder, and it makes a copy of the release version of my
03:16executable in this folder.
03:18In a real application, you may have even more complex build requirements than this.
03:23You may need to have a test build.
03:24In that case, you can go to the Configuration Manager and add other builds in here.
03:30For instance, I can come down here and say that I have a new test configuration.
03:36I say that in the Debug build, you're going to compile the ConsoleApplication1
03:41with the Debug configuration. And in the Release mode, you're going to build the
03:45ConsoleApplication1 with the Release configuration.
03:48And in the Test build, you're going to build the ConsoleApplication1 with
03:53the Test configuration.
03:54Now, when I come up here and I do a build, you'll see that I end up with another
04:02folder over here, Test, and there is my ConsoleApplication1.
04:05Now this movie is just covering the basics of building your application.
04:09If you watch the build customization movie next, you'll learn how to adapt the
04:13build scripts to your own needs and how to change which folders the physical
04:17files are stored in.
Collapse this transcript
Customizing the build process with MSBuild
00:00In many circumstances, you can use the default build options for your project
00:04and have a successful build.
00:05There are times, however, when you need to adapt the build script to your needs.
00:09I'm going to work with this project called CustomBuildProcess.
00:12It has a simple WPF application, and I'm going to choose several different
00:17compile options for this application.
00:19I am going to start by editing a few of my Tools > Options settings.
00:22Let's open this up.
00:23We are going to look in Projects and Solutions and pick the General section.
00:27Whenever you do a build inside Visual Studio, it can output the results of the
00:31build process to this output window.
00:33So, I make sure that this is checked:
00:35Show Output window when build starts.
00:38I can also control how much information is dumped into that window by clicking
00:42on Build and Run and choosing MSBuild output verbosity.
00:47By default, I believe at Minimal.
00:49You can pick Diagnostic, which gives you a huge amount of information, or you
00:53can pick Quiet, which gives you virtually none.
00:56So I'll choose this one and then click OK.
00:59Now, let me do a build, Ctrl+ Shift+B, and there is my output.
01:03Let's try it again.
01:05Choose Tools > Options.
01:07This time I'll choose Diagnostic, and do a Ctrl+Shift+B, and now you see, I have
01:12got several thousand lines of code, including information about the computer I'm
01:16running on and the environment and many, many details.
01:19Let me set that back to the original settings. Minimal.
01:24Now I have some control over the build process by going to Properties and then
01:30clicking this Build section.
01:32I am going to autohide his toolbox for a few minutes too.
01:35Here I can control what happens when I do a debug build.
01:39I can control all the settings and even the Output folder down here.
01:43Also, this is where I can generate my XML documentation for that folder.
01:47If I switch to the Release build, now I am controlling what happens when I
01:51choose the release build from this dropdown. And in this case, I'm saying to save
01:56the files to a different location.
01:58We can also customize the Build Events, but before I get to this screen, let me
02:01show you a few other options.
02:03MSBuild.exe is the tool responsible for building your application, and it relies
02:07on a XML script for its instructions.
02:09Now here's a little secret:
02:12the build script instructions are stored in an XML file, and they're hiding in
02:15plain sight in your project folder.
02:17Let me show you where they are.
02:19If I were to look at the project folder for this application by
02:22right-clicking on this and choosing Open Folder in Windows Explorer, there is
02:26a file here called csproj.
02:28This is the instruction to Visual Studio of what to load when you open this
02:32project within Visual Studio.
02:34But also inside this are the build instructions for MSBuild.
02:38So if I right-click on this and choose Edit with some editor, like Notepad++,
02:42which is the free tool we have on our computer, you'll see that this is nothing
02:46more than an XML file.
02:48Now I can open this file inside Visual Studio if I know the secret.
02:53Return back to Visual Studio. I right-click on this CustomBuild project and choose Unload.
02:59Now all the files that were part of that project are no longer usable inside Visual Studio.
03:04Next, I right-click and I choose EditCustomBuildProcess.
03:09There is that file, but now we are looking at it as the text file as opposed to
03:12the loading it in the Solution Explorer.
03:15If you look through this, you'll see that it's just settings for the build
03:18engine, where to output the Release build, and it's right here.
03:22That's the Output Path.
03:23That's the same thing I was setting just a few minutes ago in
03:26the Options dialog.
03:27Here is all the references to add for the DLLs, and then down here are the
03:32individual files that need to be compiled. It needs to compile a file
03:35called App.xaml.cs.
03:37There are also other areas.
03:40There is PreBuildEvent and PostBuildEvent. And if you want, you can even create
03:44your own custom build tasks and add them to this list.
03:47I am going to show you how to do a PreBuildEvent and a PostBuildEvent.
03:51To do that, I'm going to close this window, then right-click on
03:55CustomBuildProject and choose to reload it back into Visual Studio.
03:59Next, I am going to double-click on the Properties section, and I am going to go
04:03to this Build Events tab.
04:05So let's show you what happens when you do a compile.
04:07Any instructions that are inside this section are run first.
04:11Virtually anything that you can run at a DOS command prompt you can run inside this box.
04:16Then it'll go through the build instructions that are part of the csproj file I
04:20just showed you, and then when that's done, it will go out and run any other
04:23command you tell it in the Post-build command line.
04:26Now, I am going to start here by creating a brand-new folder.
04:28I am going to do a make directory .exe.
04:31I am going to call this directory buildDemo.
04:34I would like to make sure that this is a fresh directory,
04:37so I am going up one line above that and say I would like to remove the
04:39directory buildDemo.
04:43So I'll remove the directory, then recreate the directory.
04:45Let's see if this is working.
04:46I'll do a save, and then I'll do a build, and then we'll go and look in our hard
04:53drive, look in the bin folder.
04:57I was in a debug build, so I'll choose the Debug folder, and there is
05:01my buildDemo folder.
05:03Next, I am going to copy a file into that folder.
05:06So let me show you how we do that.
05:07I go to the PostBuildEvent and what I want to do is take this
05:10App.config.test file, I want to make a copy of it, rename it and put it in
05:16that new folder I just created.
05:18I can find that information about my application and put them in this Post-build
05:22event by clicking on this Edit Post- Build button and then clicking on Macros,
05:26and this shows me things like output directory.
05:29That's how I put the output directory into my script, by putting a dollar sign
05:34and then this special keyword here.
05:35So let me show you the text I'm going to run.
05:37I'll go to Assets. I'll open this AfterBuild text file. I'll copy this and then
05:45paste that right in here.
05:47So this says, "Go out to the DOS command."
05:50We call the command prompt now, right?
05:51"We are going to find the project directory and get this file, App.config.test,
05:57and then we are going to make a copy of it, and we are going to put it in the
05:59target directory--the output directory--and then in the new folder I just
06:02created, buildDemo, and then use this token to rename the file."
06:06Let's see if it works.
06:08Right-click here and choose Open Folder in Explorer, open the bin folder, open
06:15the Debug folder, drill down into there, and there, I was successful.
06:19I have a CustomBuildProcess.config file.
06:22There's more I could do with these events.
06:24I could build a batch file, for example.
06:26The build engine is customizable too.
06:28You can create your own build tasks in .NET code and then add them to the build script.
06:33Be sure and check out more details in MSDN help.
Collapse this transcript
Setting assembly information
00:00It's important to include certain information inside your finished assembly.
00:04At the minimum, you need to include a version number of your application and some
00:07information about your company.
00:09You can embed this information inside your code, and you can also instruct the
00:12build engine to modify the EXE properties.
00:15I'm inside Visual Studio, and I'm going to create a new project.
00:18I'll choose File > New > Project, and then I'm going to pick C# >
00:21Console Application.
00:22I will put this out in the correct movie folder, and then I'll click OK.
00:30To add assembly information, all I need to do is go to this property window,
00:34double-click on this node right here, and then in C# projects I click on the
00:38Assembly Information button.
00:39It's the same in Visual Basic; the button is just located in a
00:42slightly different location--
00:44it's down near the bottom of the page.
00:45Here I pick the name of my application, a description, our company name, our
00:56product name, which I think is going to be the same as my SuperFile title, but
01:00maybe I might call this one v2, and then down here I'll say Copyright Lynda.
01:07Then over here we'll change our assembly version.
01:09I can also change the file version in there well.
01:12There are some other items added down here.
01:14I'm going to leave those at the default, and then I am going to click on OK.
01:18Now I've to choose the name of my output EXE,
01:21I need to come up to this section and type in a new name.
01:24So here, I don't want to call this ConsoleApplication;
01:26I want to call this one SuperFileSaver, or something like that.
01:29Now I'm ready to build my application, and we will go and see if that
01:33information is now embedded in my file.
01:35I'll right-click here, choose to Open the Folder in Windows Explorer, find my
01:39executable, which is hiding here in my Debug folder. There it is.
01:42See, it's got the correct file name and when I hover over it, I see that this is
01:47Lynda.com, and it's version 2.0.
01:48If I want to see more information, I can right-click choose Properties >
01:54Details and here I can see all of that embedded information, including the File
02:00version is also in there.
02:01That's about all I have.
02:02So I need to close this.
02:03Now that you've embedded your information in the assembly,
02:06it's time to think about deploying the executable to the user's computer.
02:09That's the electrifying topic for the next movie.
Collapse this transcript
Deploying a basic Windows application
00:00The simplest way to deploy your .NET Windows application is to copy the files
00:04to a folder on the user's computer.
00:07Visual Studio plays no role in this deployment, other than compiling the finished
00:10executable. But let me show you this basic deployment anyway as it sets the
00:14stage for other movies in this chapter.
00:15For this movie, I'm going to be using an application called PixelSmithDesktop.
00:19This is a simple WPF application.
00:22Let me show you what it looks like.
00:23I'll run the application.
00:25It has a series of buttons on the left side of the screen.
00:28When I click the Add Ellipse button, it adds an ellipse to the designer surface.
00:32Then I can select a different color and add another ellipse or add a rectangle,
00:37and I can even add an image.
00:39So it's relatively easy to build this in WPF.
00:42The drag and drop behavior comes from a behavior that's part of the
00:46Expression Blend library.
00:48So in my applications folder, if you were to look in the compile folder--
00:53for instance, in the debug folder-- you'll see that I have a reference to this
00:56Expression Blend and the System.Windows.Interactivity.dll.
00:59Just make sure you have those DLLs in your folder when you compile the application.
01:04The first thing I need to do to make my application ready to release is to
01:07switch over to this release build by clicking in this dropdown and choosing
01:11Release and then building my application.
01:13Now here is how easy it is to deploy the application.
01:16I go out to my hard drive and make a copy of the Release folder, and then I put
01:20it wherever I want to install the application.
01:23Let's say I want to install it to my desktop.
01:25So I'll go over here, I'll right-click, and choose Paste.
01:28Now, the user should be able to go in to this application and double-click on
01:32PixelSmithDesktop, and there is a running application.
01:36They should be able to rename the folder and re-run the application.
01:43They should be able to move that folder anywhere on their hardware where they
01:46have permission to put a copy of a folder and still run the application.
01:50So that's really how simple it is to deploy a .NET application.
01:53The deployment story gets more complicated if you want to install shared files.
01:57Those shared files need to go in the .NET Global Assembly Cache.
02:01For that you need an installer application.
02:03If you have a web application, you have to upload the files to then web server
02:07and then configure the server.
02:09If you have a Windows application that needs auto-update, you should consider
02:13creating a ClickOnce application.
02:15Coincidentally, there are movies about each of these topics later in
02:18this chapter.
Collapse this transcript
Creating an installer with Visual Studio
00:00Installing a production application on a local computer is more than just
00:04copying files to the Program Files folder.
00:06You might need to check for dependencies and verify that a previous licensed
00:10version is available before continuing.
00:12You may need to remove certain files before installing the new version.
00:15It is a common practice to create items in the Start menu and copy help and
00:19documentation files to the computer.
00:21If you need to perform any of these tasks, you must create a setup program.
00:25The Windows Installer API exists to assist us in these scenarios.
00:28Visual Studio contains an installer project that creates Windows Installer files
00:33for your application.
00:34For this demonstration, I'm going to use a solution called InstallerDeploy that
00:38contains the application that I want to install, which is PixelSmithDesktop.
00:42This is that WPF application we've seen in other movies in this chapter.
00:45I'm going to start by adding an installer project.
00:48I'll right-click on my Solution and choose Add > New Project.
00:53Then I'll move down to the Other Project section and choose Setup and Deployment.
00:58There are two setup projects available here: InstallShield LE and Visual Studio Installer.
01:03This is a third-party application and it's an add-in to Visual Studio.
01:06I'm going to choose the Visual Studio Installer, and then I'm going to
01:10choose the Setup Wizard.
01:12I'll give this a better name.
01:13I'll call this one PixelSmithInstaller and then click the OK button.
01:20Since this is a wizard, it'll start walking me through these
01:22questionnaires. Welcome. Thank you.
01:25What kind of application setup do you want?
01:28Is this for a Windows or web application?
01:30This is for a Windows application.
01:32I don't need any of the special packages,
01:34so I'll click on Next.
01:35Here I'm picking the files that are coming out of my application.
01:40This indicates that I want all of the files necessary to run the application:
01:44the Primary output, EXE, and DLL.
01:47This would be the source code, like the C# files.
01:50Obviously, I don't need that from my end users.
01:52This is for my debugging symbols. I don't need that, but I do need content files
01:56and documentation files if I have those.
01:59So I'll click Next.
02:00Here I can add additional files. I'll click on the Add button, and then in this
02:05Other Files folder, you'll find a number of extra files.
02:08There is a Windows movie in here.
02:10There is a help file, there is a PDF, and a document file, and an RTF.
02:15I'm going to select all of these and then click Open and then Next.
02:20At this point, I'm done. I'll click on the Finish button.
02:23As you can see, a new project was added to my application.
02:26Now let me show you what happens when you go to build your application.
02:29If I go up here and I choose Build Solution, that normally would build all of
02:33the projects that are inside my solution. But as you can see from my Output
02:37window, it only built my main application; it did not build the installer.
02:42I think this is because you don't create installers until the end of
02:45your project cycle.
02:46Since installers can take a long time to build, the default mode is to not build
02:50this file when you choose to build the solution.
02:53Obviously, I do need to build it, so the way I can build the application is to
02:57right-click and choose Build.
02:59At this point, the installer is ready to run.
03:01I can right-click here and choose Install. And later when the application is
03:06installed, I can choose Uninstall from this location.
03:08I'll start the install process by clicking here.
03:11It's got the wrong name up here, PixelSmithsInstaller.
03:14It should say PixelSmithDesktop.
03:16So I need to fix that.
03:17So let me cancel out of here.
03:19Choose Yes and then choose Close.
03:22I need to change a couple of settings.
03:24To configure my application, there is two places I can go. One is I can click on
03:28the project and edit items in the Property window.
03:31For instance, I don't want this to say Microsoft as the manufacturer;
03:35I want it to say Lynda.
03:37Also, the ProductName is wrong.
03:39It should say PixelSmithDesktop version 2, and I'll also change the Author to Lynda like that.
03:46Another place I can change the configuration is by right-clicking and choosing View.
03:50The only one that I want to change for this demo is User Interface.
03:53So let me click here.
03:55These are the different screens that are going to show up during the
03:57installation process.
03:58What I would like to do is add a custom Readme to this install.
04:01So what I will do is right-click on Start and choose Add Dialog.
04:05Then I'll come over here and choose Read Me. OK.
04:08Next, I'm going to drag this Read Me up to the top of the list, so that it's
04:12second from the Welcome screen.
04:14So they'll see the Welcome screen.
04:15Then they'll see my Read Me.
04:17I also need to tell what text to show.
04:18So I'll come over here and click the ReadmeFile. Browse for that. I'll look in
04:24my Application Folder, find this ReadMe.rtf file and click OK.
04:28I'm done with the changes to the user interface.
04:32Next, I'm going to change what's installed on the user's computer.
04:35Here is the Application Folder.
04:37I would like to add some extra folders to this location.
04:40So I'm going to right-click and choose Add > Folder, Samples and Add > Folder, Documents.
04:49Now I can go back to my Application folder and drag the files to the location
04:55where I want them to be installed on the user's computer.
04:56So this documentation file and this movie file I think those belong in
05:01the Documents folder,
05:02so I'll drag those over here.
05:03And these two sample files, I'll drag those over to the Samples folder.
05:07Now I'll add a shortcut to the user's desktop.
05:09That's accomplished by coming over here and right-clicking, choosing Create New Shortcut.
05:13I want to create a shortcut to the Primary output, which will be my EXE.
05:19I should probably change the name, because it's a terrible name.
05:21I'd also like to add something to the user's Start menu.
05:24So I'll go over here and create a new folder and call this one PixelSmith.
05:31Then I'm going to click here and choose Create New Shortcut.
05:35I would like the Primary output.
05:37Again, I'll rename this one.
05:39I'd also like a shortcut to my help file.
05:41So I'll add one more shortcut and pick Documents and then Documentation.chm.
05:47Now, I'm ready to build the application,
05:49rename that, and I'm ready to build.
05:52I'll come over here and choose Build.
05:54Once I have a successful build, I'll go to my solution and choose Open Folder
05:59in Windows Explorer.
06:00It will open my installer application.
06:03Here is the files that it will give to my user.
06:06They can double-click on these, like the setup.exe, and now you see the dialog
06:10includes the text that I had, PixelSmith Desktop V2.
06:13It has my Readme file.
06:16It's suggesting the correct folder location based on the criteria I put in that
06:21Property window, the Lynda folder, and then the PixelSmithDesktop V2.
06:24I'll click on Next to install, and I have now successfully installed my application.
06:30Next, I'll go to the Start menu and see if my application is here.
06:34It was called PixelSmith.
06:36There it is, PixelSmithDesktop.
06:38It's running successfully.
06:40Next, I'll try my Start folder, and I'll look for the PixelSmith folder, and both
06:45my shortcuts are available there,
06:47so I'll click on the Documentation.chm file to see what my help file looks like. That's working.
06:52If you were to go look in the Program Files folder on your hard drive, you'd
06:55also see those subfolders that I generated, like for instance the Samples folder is there.
07:00I would like to show you how to uninstall the application.
07:03On Windows 7 you would go to Programs and Features, and then you're going to
07:08scroll down and find the PixelSmith area. There it is.
07:11Click on this and choose Uninstall.
07:14Click Yes to this dialog, and now the application is uninstalled.
07:18The Visual Studio Installer creates a basic setup that works for most developers.
07:22If you want more power, I suggest you look at the InstallShield add-in.
07:26There is one feature that's missing from this installer.
07:29I think it would be nice if the application would automatically update when you
07:32create a new version.
07:33For that, you need to look at creating a ClickOnce installer, and that's the
07:37topic of the next movie in this chapter.
Collapse this transcript
Creating a ClickOnce application
00:00ClickOnce is an alternative to the standard Windows installer.
00:04The name refers to the fact that the user is prompted once to install the app.
00:08The more compelling reason to use ClickOnce, however, is for its
00:11automatic update feature. Here is the idea:
00:14When you deploy your application you embed a URL in the application.
00:18Then you deploy a special manifest file to that URL.
00:21The manifest contains information about the application itself and a list of
00:25application files and prerequisites.
00:27Whenever your application is started, the ClickOnce engine checks that URL to
00:31see if the manifest has changed.
00:33If so, the application is updated based on the contents of the manifest file.
00:38I've used ClickOnce successfully on several projects, and I have to say that
00:42it's easy to use and works as advertised.
00:45I'm opening a solution called ClickOnceDeploy, which contains our
00:49PixelSmithDesktop WPF application.
00:52To do a ClickOnce deployment, you just need to open this Properties node and go
00:57to the Publish section.
00:59I would like to talk about security keys first.
01:02You have to sign your manifest. It's easy to sign by going to the Signing tab and
01:07using this Sign the ClickOnce manifests.
01:09I have already created a temporary test certificate key.
01:13If you want to create your own, just click on this Create Test Certificate key
01:16and it will replace the existing one.
01:18Next, I'll go to Publish, and we'll talk about these two locations.
01:22There is the Publishing Folder Location and the Installation Folder location.
01:26The Publishing Folder Location is where Visual Studio copies the files during
01:30a publish operation.
01:32The Installation Folder URL is where the user goes to install the application.
01:36These may be the same location, and they might be different.
01:40Let me tell you a reason why they might be different.
01:42Let's say we're deploying off of a web server.
01:45I don't have permissions to upload files to that server. I will have to give them
01:48to my IT administrator.
01:50So I'll publish to a local location.
01:52Then when I'm done, I'll zip those up and send them to my administrator and say,
01:56"These need to be installed on our web server."
01:58I do need to embed the correct HTTP location here though, because that's where
02:04your application is going to go look for the manifest.
02:06For today, we're going to use the same location to make our lives easier.
02:10I'm going to go out to our Desktop, and I'm going to create a new folder on the
02:14desktop called Deploy.
02:15Then I'm going to open that Deploy folder, and then I'll click up here and copy the location.
02:22Then I return back to Visual Studio, and I'm going to paste that location in
02:27this Publishing Location.
02:29Now, I'm going to have to change this to localhost, and then I'll take and copy
02:34this and paste it down here in the Installation Folder location.
02:38Next, I'm ready to publish.
02:40I will walk you through the wizard, so you can see how that works.
02:42I'll click on the Publish Wizard.
02:44This just asks where the publishing folder is.
02:46I'll go ahead and click the Next button.
02:48Here, I can specify where the user will install the application: from a web site,
02:53or from a local file, or from a CD-ROM.
02:55Naturally, we're choosing this local location.
02:57Next, I choose whether I want to have the application run offline or not.
03:01In other words, if the web site is not available, will this application is still run?
03:04I'll click Next and then Finish.
03:07It opens this folder, which is my Desktop\Deploy location, and let's look at what we have here.
03:12This is the file that the user will run to install the application.
03:15This is the manifest file.
03:17It contains XML data, pointing out what the current version of the application is.
03:22This folder contains all the physical files that need to be installed on
03:25the user's machine.
03:26So if I open up the Application Files folder, here is our first version of our
03:29application, and inside there are the files that need to be copied to the
03:32user's install folder.
03:33I'm going to go back here and double- click on setup.exe, step through the prompts.
03:38The reason I get this is because I'm not using a real security certificate; I'm
03:43using a test certificate.
03:44Click on Install, and now the application is running.
03:47We'll check out and make sure it's working.
03:50Looks good, and then I'll close the app.
03:54Now the next day your user comes into work, they go down here, and they choose
03:57to run PixelSmithDesktop.
03:59I don't know if you saw that, but very briefly there was a Checking for Updates
04:04dialog that popped up.
04:05It didn't find any new updates, so it just went ahead and ran the application.
04:09I'm going to make some changes to the application over inside Visual Studio.
04:12So I'm going to go to my UI, and I'm going to change some text on this ToolBox.
04:18I'm going to come over to the Text property and put an extra x in there and then
04:23compile my application.
04:24So now I have a new version.
04:26Let's publish that new version.
04:28We'll go back to the Publish location.
04:31Rather than click on the Publish Wizard, I can just click on the Publish Now.
04:35Let's go look in this folder.
04:38You see how there is two versions here now? And this manifest file is now
04:41pointing to the second version.
04:43So, the user now goes to their shortcut on their desktop, and they see this
04:48dialog, "A new version of PixelSmithDesktop is available.
04:52Do you want to download it now?" Of course we do.
04:54We'll click on OK, and now there is our new improved version of the application.
04:59There is more to explore in ClickOnce.
05:01For example, it exposes a programming model so that you can write code to check for updates.
05:06I'm a firm believer in the power of this tool, and I love how ClickOnce
05:09streamlines the update processes for my application.
Collapse this transcript
Setting up IIS for deploy
00:00Before watching the "Deploying a Silverlight or ASP.Net application" movie in this
00:04chapter, you need to have IIS running and configured correctly.
00:08This movie shows you the bare minimum necessary to deploy the application to
00:12your local web site.
00:13If you're going to have IIS running on your computer, it has to be enabled.
00:16To do that, you need to go to Programs and Features, and click on this Turn
00:22Windows features on or off.
00:24Next, you're going to want to come down here and click on this section where it
00:27says Internet Information Services, and then click OK.
00:30After about two minutes, IIS will be up and running on your computer.
00:34I've already got it running, so I'm going to click Cancel and then Close.
00:38Also for this movie, I'm going to create a separate folder on my desktop to
00:42contain my web site.
00:43Now that I have those two things ready, I'm going to open IIS.
00:46I'll go back to my Start menu and type in "IIS", and then I pick this item up
00:50here: Internet Information Services Manager.
00:53I'll maximize my window, and then we'll take a look at this preview over here.
00:58Currently, I have one site on my web server.
01:01I'm going to add a second site for my demonstration purposes.
01:05Right-click, choose Add Web Site.
01:07I'm going to call this site LyndaWeb, and I'm going to point it to that
01:12folder on my desktop.
01:13So I'll go to my desktop, find the LyndaWeb folder, and then click OK.
01:19Now because this default web site is already using port 80, I need to assign a
01:24different port number.
01:25I'm going to choose an arbitrary number. I'll just pick the number 90 and then click OK.
01:30Notice it says, "Start Web site immediately."
01:33So now the second web site is up and running.
01:36I'm going to click on Application Pools and verify that LyndaWeb is using the
01:41.NET 4.0 framework, which it is not.
01:43So I'm going to double-click on this item and then choose .NET 4.0.
01:47That's because I'm going to be using a Silverlight application in a later demo.
01:52My web site is now configured. I can close IIS.
01:55For a more detailed look at ASP.NET deployment, check out the ASP.NET Essential
02:00Training course here on lynda.com.
02:02Be sure and watch the IIS chapter in that title to learn more about setup and
02:06configuration of ASP.NET and IIS.
Collapse this transcript
Deploying a Silverlight or ASP.NET application
00:00After you create a Silverlight or ASP.NET web application, you typically deploy
00:04it to a web server where others can access your application.
00:08At a bare minimum, a web deployment copies files from one server to another.
00:12In some situations, you might need to deploy it to multiple servers in a web farm.
00:16I've seen many ways to deploy applications to web servers--some of
00:20them downright clunky.
00:21Fortunately, for us, Microsoft created web deploy extensions for their
00:25enterprise web server,
00:26IIS, and Visual Studio can use these extensions to deploy your application.
00:31For today's demo, I'm going to be using the solution
00:35UsingBingMapControl, which contains a Silverlight application and an
00:39ASP.NET application.
00:41The Silverlight pages are going to be rendered inside the ASP.NET application.
00:45Let me show you what the application will look like.
00:47I'm going to press F5 to run the application.
00:50This showcases the Bing Map control, which I can also programmatically access.
00:56For instance, I can click on this New York button to center the map on New
01:00York, or I can click here to zoom in on an area of the Grand Canyon.
01:05So this is our ASP.NET application.
01:07Now I'm ready to deploy.
01:09I need to push it out to our web servers.
01:12For our demonstration today, we're going to be using a local server in IIS, but
01:16in reality we probably would be publishing this to a production server
01:19somewhere on a host.
01:20I can go to Build > Publish.
01:24In this dialog, I have several ways to connect to the web server.
01:28I can use the local file system on my machine, because IIS is running locally.
01:32If it was running elsewhere, I could use FTP, or File Transfer Protocol, or
01:37FrontPage Server Extensions to deploy.
01:39I can also use Web Deploy, which is Microsoft's latest mechanism for deployment.
01:44I'll show you that dialog in just one minute.
01:46I'm going to delete all the existing files, and I'm going to save out to my
01:51desktop, which is where IIS is looking for the web files.
01:55Before I publish, I need to configure IIS for the correct authentication.
02:01I'm going to go run IIS, and here's the site we set up in the previous movie: the LyndaWeb.
02:09Now, I'm going to click on Basic Settings and then Connect As.
02:14If we connect as application user, and we run off our desktop, I'm going to get
02:18denied access to the web config file.
02:21So I'm going to switch over to a different user.
02:23I'm going to use my user account on this machine to connect.
02:27I'm going to test my settings. And if I get green authorizations, I know that
02:34I'm correctly configured.
02:36Naturally on your machine, you're going to have to use account that makes
02:39sense for your computer.
02:40I'll close all these dialogs and close IIS, and then I'm going to click Publish.
02:46Now I'm going to go look in my Desktop, in the LyndaWeb folder, and there are all
02:51the files necessary for my web application.
02:54Next, I'm going to test this in a web browser.
02:56I'll try Mozilla Firefox.
02:58I'll use localhost:
03:02and then my port number, which is 90, and we have successfully published our web application.
03:07Let me show you one more detail.
03:09I'm going to go back to Visual Studio and then choose Build > Publish.
03:15I can also deploy with this Web Deploy model.
03:18As I showed you earlier, I'm doing a local deploy.
03:21But you can also publish using this web deploy model, which is a much
03:24more powerful system.
03:25This a great option if you're deploying to production servers.
03:28With web deploy, I can perform other tasks, like changing web.config files
03:32for different environments, configure IIS settings, and even install security
03:37certificates.
Collapse this transcript
11. Working with Source Control
Understanding source control
00:01Version control systems have been protecting our code for decades.
00:04In this movie, I want to give a short overview of version control and how it
00:08relates to Visual Studio.
00:10Version control, also called source control, is a way to track versions in your software.
00:15More precisely, it permits you or your team members to track every single
00:19revision made to a project.
00:20The change track can be as small as changing one character inside a single text
00:25file, up to reorganizing entire directory structures for complex multi-project applications.
00:31All files and changes are stored in a repository.
00:34Most often this repository is stored on a server.
00:37In some of the newer control systems, like Mercurial, the changes are distributed
00:41across multiple developer desktops.
00:44Version control takes a snapshot of your project at a moment in time.
00:48Anyone with access to your repository can regress to a previous snapshot and
00:52investigate the files as they were at that time.
00:55Naturally, after working with an older version, you can retrieve the most recent set.
01:01Each change is stored separately in the repository.
01:04You never have to worry about overwriting the files.
01:06Of course, you'd better have a reliable backup plan for your repository.
01:11There are two common metaphors used in source control.
01:14The first is Lock-Modify-Unlock model.
01:17One person checks out a file, or files. At that moment, the source engine locks the files.
01:23All your other team members cannot edit the file while it is checked out.
01:27If you have ever worked with someone who forgot to check in a file and then went
01:30on sick leave, you know the pain that this type of system can cause.
01:34The second metaphor is the Copy-Modify-Merge model.
01:37There is no locking in this model, and anyone is free to get the latest version
01:41and change the file.
01:42The trouble, if there is any, comes when two or more people edit the file and
01:46then check in. The second person gets a warning when checking in their copy.
01:51It's their responsibility to merge these changes with the prior check-in.
01:55Visual Studio works with both of these models.
01:58It really depends on which source control engine you connect to.
02:01If you're using Microsoft Team Foundation Server, it supports both models.
02:05As you can see, there are many providers available to use.
Collapse this transcript
Setting up Team Foundation Server source control
00:00In this movie, I want to show you how to connect with Team Foundation Server.
00:03A word of caution before continuing:
00:05I am using my open source project on CodePlex site for this movie.
00:09You can see the screen here.
00:10I'm logged into CodePlex, and I have two projects that
00:13I am a coordinator on:
00:14my open source project called Shazzam, which is a pixel shaded editor, and this
00:18one I created for this week's class called TruffleSoup.
00:21Now I can connect to these sites, because I'm the coordinator on them, but you
00:24do not have permission to connect to the CodePlex projects.
00:27In order to follow along, you need to talk to your administrator and get
00:30information to connect to your copy of Team Foundation Server.
00:33Of course, if you don't have a Team Foundation Server of your own, you can
00:37always create a project on CodePlex.
00:39It's free, after all.
00:40I'm going to start by clicking this TruffleSoup project. I will click here.
00:45This is a temporary project. It's only here for 30 days, and I need to go to the Source Control tab, and
00:53then over here, I can find the information necessary to connect with different
00:57source control engines.
00:58As you can see, CodePlex allows you to connect with Subversion, Visual Studio
01:02Team Explorer, and Team Explorer Everywhere.
01:05I am going to choose this one here.
01:08Next I am going to copy this piece of information, because I am going to need it
01:12to connect to the server.
01:13Now, I am going to open up Visual Studio, and I am going to go to File > Source
01:25Control > Open from Source Control.
01:29Over here, you can see the Servers tab.
01:32If I open this up, this is where I can choose to servers that I want to connect to.
01:36I am going to remove this one and re-add it. Choose Add.
01:41Here, I am going to paste in the text I've got from the web site.
01:45I am supposed to connect using HTTPS, so I'll select this radio button here,
01:50and then I'll click OK.
01:53The next thing is it is going to ask me for my credentials.
01:57My credentials are listed down here--at least some of them are.
02:00My username is listed here, so I'll copy that, and then my password is the one I
02:05used to log on to CodePlex.
02:07I will come over here and type in the name and then my password.
02:11I have successfully connected to the server. Now, I can close this dialog.
02:25Now, it shows that it is talking to TFS with my credentials, and it shows that
02:30there are two team project collections: one is for Shazzam, my open source
02:35project, and the other one is for this fictitious site I created called TruffleSoup.
02:39And what I can do next is create workspaces for these.
02:42I will choose trufflesoup and click on Connect.
02:48It says, "Would you like to open from Source Control?"
02:50I already have an existing project up there.
02:52I can download it, but this movie was just about connecting to the source control.
02:56In the next movie, I'll show you how to set up your projects, create new
02:59projects, upload them to source control, and work with the files in
03:04version control.
Collapse this transcript
Using Team Foundation Server source control
00:00In this movie, I will show you how to connect to a Team Foundation Server
00:03repository, and work with the source control tools inside Visual Studio. A word of caution:
00:09I've already set up the connection to TFS in an earlier movie in this chapter,
00:13so if you need a refresher on how to create a connection, you can watch the Setup movie.
00:17I'm inside Visual Studio, and I am going to show you how to open a project
00:21from source control.
00:22I am going to go to File > Source Control, and then Open from Source Control.
00:28I already have our project set up on CodePlex.
00:32This is the sample project I created for this class.
00:34I am going to drill down into the folders on the server by double-clicking.
00:40At this point, I've reached the solution files on the server.
00:43Notice that's in a folder called TruffleSoupMain.
00:46I am going to select the solution file, and then I am going to go down here and
00:49map to a Local Path.
00:50So I am going to type in this folder that I created earlier this week,
00:54called localSourceFiles, and then I'm going to create another folder
00:57called TruffleSoup.
00:58Then I am going to click on OK.
01:03We just connected to the server.
01:05It's downloading the files to my computer.
01:10Now if I right-click on this solution and choose Open Folder in Windows
01:14Explorer, you'll see that inside my localSourceFiles folder is the TruffleSoup,
01:19and then I've downloaded this Readme.txt,
01:22this solution file.
01:24If I look in here, you'll see a number of files in here.
01:27Notice there are several files that end with this SCC extension.
01:31Those are used by the source control engine.
01:34There is another one up here:
01:35TruffleSoupMain.vssscc.
01:39Hard to say, but that's the name of the extension.
01:41I am going to return back to Visual Studio and show you the Source Control.
01:45If you can see, there is a little padlock next to each of the icons in here.
01:49That means it is under source control, and I don't have an editable copy yet of that file.
01:54So if I go over here to this main window, double-click on it--we need to unpin
02:01this for a while, this Pending Changes down at the bottom of the screen--
02:04here is the designer surface for this WPF application.
02:08If I go over to the Toolbox, and drag another Button out here, watch what
02:15happens to the XAML file over here.
02:16There is a red check mark over it, and when I hover over it, it
02:20says, checked-out to me.
02:22Now if other people check out files, I can get notification on icons here that
02:25when I hover over, it'll say it's checked-out to someone else.
02:28So that way, I know I can't check out that file.
02:30This is what's called a silent checkout.
02:33You can change whether it's a silent checkout or whether it prompts you by going
02:38to Tools > Options, and then choosing Source Control, and then Environment.
02:50Here's what happens: checked in items.
02:54If they're checked in, and I am editing it, it'll check it out automatically.
02:58If I'd rather be prompted, I can choose this Prompt for check out.
03:01I'll close this. Now, I am going to make my changes.
03:08Make this button the same size as the other one, and now I'm ready to check in.
03:13So what I do is I go over here, and I say, I have a pending check in here.
03:18I want to check this in.
03:19It asks me if I want to change my solution file. I say Yes.
03:25Then I'm prompted to check in my source files.
03:27Here is where I make my comment.
03:29There are also some affiliated items you can see over here:
03:37there is Check-in Notes, there is something called the Work Items, and there is Source Files.
03:42Now, work items are part of Team Foundation Server.
03:44They're like notes of what needs to be done.
03:47They can be bug tracking items.
03:49I don't have them set up on CodePlex, so I can't add a work item.
03:52I am ready to check this code in, so I'll click on Check In.
03:56Now, the other developers can check out this file and start working on it.
04:00If I ever want to see the history of what I've been doing, I can right-click
04:03here and choose View History.
04:06This shows that there has been several change sets on this file.
04:11I can see the timestamps on here.
04:13So I can see I was working on these last night.
04:17I can close this window.
04:18Another thing you can do is you can right-click and choose this Annotate item.
04:25What this does is it shows you your source code, and then along the edge it shows
04:29you where the changes have occurred.
04:30So something got changed up here on change set 1014.
04:34This is the latest change set, 1193.
04:37This is where I added that second button.
04:38So I can see that this is where I changed it.
04:41Now, in a real project when you're working with multiple developers, you'll be
04:43able to see each person's information over here and which sections they've been
04:47working on and what they have changed, which is really handy when you don't
04:50understand a piece of code and you need to know who wrote that section.
04:52You can go look this up and then go, talk to them and say, "What were you doing here?
04:55I don't understand what this code is doing at the moment."
04:58So I'll close the annotated version of that.
05:01I am going to make two changes.
05:02I am going to double-click on this to write some code, like that.
05:21I'm thinking I want to check this in--let me save this--
05:24but I'm not sure what is currently saved in the database.
05:28I want to know what the differences are between what I currently have on my
05:31machine and what's checked into source control.
05:33So what I can do is right-click on this, and choose Compare, and I'll click on OK.
05:43It brings up a Difference tool, and it shows me the two files. And I can
05:47obviously see that I've added four or five lines of code here on the local copy.
05:52See, it local up here at the top? Here is the server. Here is the
05:55timestamp of when this was last saved in the server. Here is the timestamp on the
05:59current file when I last saved it locally.
06:01Now naturally, I can also edit these files.
06:06I can copy pieces of data back and forth between these two sites.
06:09I'm happy with that change.
06:11I am ready to check it in,
06:12so I'll choose Check In again, and we'll check that in one more time.
06:24There is another window you should be aware of.
06:28It's available in the View menu, under Other Windows, right here Source Control Explorer.
06:36This is another way of looking at your source control engine.
06:39This shows my server and my TruffleSoup sub-item, and then I can click on this
06:46and see all of the files as they are on the server.
06:50I can do some of the same operations I was doing over here.
06:53I can right-click on this and choose Check Out for Edit.
06:55I do want to point out that this is a binary file.
06:58These are text files. These are binary files, this sample document.
07:03So Team Foundation Server creates those slightly differently.
07:06If I attempt to edit this, if I double- click on the SampleDocument.docx, it'll
07:12load it into Word 2010,
07:17but because it's a binary file, it doesn't automatically check it out.
07:21I have to go in and specifically check it out:
07:25Check Out for Edit.
07:28Now, I can make the changes in the Word doc. Save.
07:35Now, this is a common error.
07:41The trouble stems from the fact that the Sample Document is considered a binary
07:45file by TFS, and it doesn't automatically check those out when you open them.
07:50All you need to do is close this document, and check out from Source Control.
07:55Now, it's checked out. Now, we double-click on it to open it, and it will no longer be Read Only. Save.
08:11Then I can check it back in.
08:20I can't imagine working on a project without source control.
08:23I've used Team Foundation Server on many projects, and it just keeps getting better.
08:27Don't forget that Visual Studio also supports other source control servers.
08:31I've also worked on several projects that use the subversion server.
08:35There are several Visual Studio extensions that allow you to perform the same
08:38source control task against the subversion system.
08:41Regardless of which one you choose, Visual Studio can bind to the repository.
Collapse this transcript
12. Integrating with Microsoft Office Applications
Understanding the .NET Office integration
00:00Microsoft Office is the world's dominant corporate application suite.
00:04A good portion of the business world uses Microsoft Excel, Outlook, and Word applications.
00:09Over the years, Microsoft has incorporated many extensibility features in
00:13Office applications.
00:14With Office, you can create add-ins, write macros and even access to core
00:19features of Office via Visual Basic for Applications, sometimes called VBA.
00:24The Visual Studio team is also part of this integration toolset.
00:27In version 2003, they published a special copy of Visual Studio, called Visual
00:32Studio Tools for Office.
00:34In Visual Studio 2010, Office integration is built right in.
00:38Let me show you some of the types of applications you can build with Visual Studio.
00:41I am using my File > New menu, and I am clicking New > Project.
00:47I have to choose a language.
00:48I can choose either Visual Basic or C#.
00:49The underlying programming model for inside Office applications is Visual
00:54Basic for Applications.
00:56Up until this release of Visual Studio, programming Office applications in C#
01:00has been a frustrating experience.
01:02The object model in VBA doesn't mesh very well with the C# mechanics.
01:07Now that C$ 4.0 is out, it's much easier to write Office
01:10automation applications.
01:11For my demos, I'm going to use Visual Basic.
01:13So I am going to open up the Visual Basic node and choose Office and then filter
01:19it down to only the Office 2010 applications.
01:22As you can see, nearly every Office product is represented in here.
01:27Some of the Office products have multiple items. For instance, Excel has an
01:30add-in, a template, and a workbook.
01:34Let's talk about the difference between a workbook and an add-in.
01:37A workbook, when you create an Excel workbook, all of the code that you're going
01:41to write is embedded in that one document.
01:44If you open that document, you can use the code. If you open any other documents,
01:48that code is not available.
01:50When you create an add-in, you're creating an application that is independent of any document.
01:56Most of the Office applications I've seen have a separate add-in manager, and
02:00this allows the user to remove an add-in when it's no longer needed.
02:04As you can see, there is an add-in for Excel and one for Outlook, and so on.
02:09Let me just demonstrate what you can do with an Excel Workbook.
02:12I am going to click here and then click on OK.
02:14Visual Studio asks me if I'd like to create a new document or use an existing document.
02:20I am going to use a new document.
02:28As you can see, in the designer area of Visual Studio is a copy of Excel.
02:32Anything that you can do in Excel you can do inside Visual Studio.
02:35I can add text to a cell. I can take that cell and format it. I can apply Bold text.
02:44If I have numbers I can put the numbers in here and then add a auto-sum formula.
02:52If I choose to run the application, I can press F5 and debug the application.
02:58Here I am pressing F5. Excel is going to launch.
03:03And the important thing is I'm now debugging my code that's inside my Excel code behind.
03:08Let me show you that code behind.
03:10I am going to close Excel and then switch over to this Sheet1.vb. And then I am
03:17going to right-click on it and choose View Code.
03:19And as you can see, it's just Visual Basic code with some event procedures
03:24here that run when the sheet starts up and other one that runs when the sheet shuts down.
03:30I can also work with Word and the other Office products.
03:33Let me show you an example of that.
03:35I am going to choose New > Project, discard my existing one, scroll down,
03:42and pick Word Add-in.
03:46Remember, add-ins are independent of any document.
03:48Now, of course, I don't have a designer because it's not part of a document.
03:53This is typically added to the Add-in menu in your application.
03:57So if I were to run this and go look in the Add-in menu inside Word, I would
04:02see by custom add-in.
04:04I'll show you an example of that in a later movie.
04:07As you see, this movie is just a quick overview of what you can do with Office
04:10in Visual Studio. The rest of the movies in this chapter dig into each topic
04:14in more detail.
Collapse this transcript
Making a Word 2010 application
00:00Microsoft Word is a remarkable tool for building documents.
00:03Think about this for a minute.
00:05Word supports headers and footers, table of contents, and indexes.
00:09You can embed charts and images.
00:12It has a huge set of formatting tools, and the list just goes on and on.
00:16I've used Word automation successfully on many projects as a reporting engine,
00:20including creating hundreds of report templates for a large insurance company.
00:24Let me show you what it can do.
00:26In this demo, I'm going to create a Word 2010 document-based project.
00:31I'm going to go to File > New > Project.
00:34I'm going to choose Office 2010 and Word Document.
00:41Then I'm going to use a copy of an existing document, which is out on our
00:47Desktop, in our Exercise Files folder, and then over here in our Assets folder.
00:54I'm going to use this Shipping Bill of Record and then click OK.
01:02So as you can see, it's opened up the Word UI inside Visual Studio.
01:07I can see the content of my document. And over here, you can see that I have a
01:13copy of the docx file.
01:16I also have a code behind file called ThisDocument.vb.
01:20Now here is the goal.
01:22I want to be able to take these bold sections of the document--
01:26Date, and Customer, and Shipper name--and I want to be able to replace those at runtime.
01:32So the user would open up this document, and I would calculate today's date and
01:37put it at this location.
01:39Then I would want to pull the customer name out and store it here, and down
01:44here, I want to have the shipper name.
01:46So, what I can do is insert something called a bookmark.
01:49To do that, I'm going to go over to my Toolbox, and I'm going to go up to the
01:56Word Controls section right up here.
01:58I'm going to find this Bookmark, and I'm going to drag it over and drop it on Date.
02:04It says, "Make a selection or place the insertion point where you want to add
02:09a Bookmark control. Then click OK."
02:11So again, I can come over here and select that, if it wasn't selected already, and then click OK.
02:16Now, you notice that there are some gray square brackets around the word "Date".
02:21I'm going to give this a name that makes sense, dateBookmark, and I'll do the
02:31same for Customer--Bookmark.
02:44Lastly, I'll do one for the shipper. Double- click, drag, and then click OK, and I'm
02:53going to call this one shipperBookmark.
02:54Now next, I want to add some UI that'll go on the right edge of my Word document.
03:07These are called action panes.
03:10If I go to my project and right-click and say Add > New Item, you can see that
03:18there is a list of common elements here, and there is one up here at the top
03:22called Action Pane Control.
03:24You can also see that there's some Office things like the Ribbon, Ribbon (XML),
03:29Style Sheets, and other items.
03:31I'm going to click on Action Pane Control and then give it a new name.
03:35I'm going to call this one ShipperPane and then click Add.
03:42It has a designer surface where I can add any valid Windows Forms controls.
03:47So, I'm going to put a label over here.
03:49Now, I'm going to sort my properties in alphabetical order to make things easier
03:58to find, and then I'm going to type in "Choose Shipper", and then below that, I'm
04:04going to put a list box.
04:11Then I'm going to go click on the Items (Collection), click on this button that
04:13has the three dots on it, and I'm going to type in the names of my shippers
04:17"FedEx", "UPS", "USPS", and "Clairs Couriers", and then click OK.
04:30Now let's save this application.
04:35I'm going to save this out into my Desktop folder > Exercise Files > chapter 12 >
04:44movie number 02, and then click Save.
04:50Now, I'm going to write a little bit of code on this ShipperPane, and then now
04:57I'm going to test the application.
04:58So I'm going to choose the ShipperPane again, and I'm going to go to the
05:04SelectedIndexChanged property.
05:06I double-click on that item, and what I'm going to do is I'm going to find those bookmarks.
05:13To do that, I'm going to use the special keyword called "Globals." and then my
05:18bookmark names, ThisDocument--that refers to the current document--dot.
05:26There is my shipperBookmark.
05:27I'm going to replace the text that's in that, and I'm going to put in the text
05:32that's in my list box.
05:34So I'll choose ListBox1.SelectedItem. Let's review.
05:43The user is going to click on something in the pane.
05:45This event's going to fire.
05:46We're going to figure out which item they selected.
05:49We're going to get its text and apply it to the shipperBookmark.
05:52Let me save my application one more time, and let's test this out.
05:56I'm going to press F5 to run the application, and there is our document.
06:03I do not see the Action pane yet, however, and that's because I need to tell
06:07Word to load that Action pane.
06:09So, I'm going to shut down Word.
06:11I'm going to return back to my code over here, and I'm going to go to the
06:16document Startup event.
06:17That's over here in ThisDocument.vb file.
06:19Then I'm going to look at the code.
06:24Here is ThisDocument_Startup.
06:26Down here, I'm going to say Me dot ship date.
06:30I forgot to type in "Text" here.
06:40There is my customerBookmark.
06:46I'm going to set its Text equal to some fictitious customer name.
06:53Then finally, we're going to come down here and say,
06:55Me.ActionsPanes.Controls.Add, and I'm going to add an instance of my new Action
07:07pane control, which I called ShipperPane.
07:08So let's review what we did here.
07:15When the document starts up, I'm going to populate the two bookmarks with data,
07:19and then I'm going to load the ShipperPane in.
07:21Save my application. Press F5 to debug the app.
07:28The date has been correctly entered, the Acme Dental has been correctly entered,
07:32and if I go over here and click on one of these Shippers, you'll notice that
07:36it's changing it in the body of the document.
07:39Now of course I can turn off the bookmarks by going to my setting in Word and
07:42say, I don't want to see the bookmarkers in my text.
07:45We've just skimmed the possibilities of what you can do with Word automation.
07:49I'm sure you can think of many reporting scenarios where using Word in this
07:52fashion would be helpful.
Collapse this transcript
Making an Excel 2010 add-in
00:00Excel has found a permanent niche in most corporate environments.
00:03Its usage is not limited to accounting tasks either.
00:07In this movie, I'm going to show you how to extend it even further by
00:10creating your own add-in.
00:11Remember that an add-in is an application that is installed once and then loaded
00:15each time Excel runs.
00:17It's not tied to a specific document.
00:20For this demonstration, I'm going to create a custom ribbon inside Excel 2010.
00:24I'm inside a project called ProcessAnalyzer.
00:27I've already written the demonstration code for today.
00:29I'm going to start by showing you how to add your own custom ribbon to an Excel project.
00:35You right-click on the project, and choose Add > New Item.
00:42Here is the ribbon designer that I want to use.
00:45Now, I already have a ribbon, so I'm not going to go ahead and click on the Add button.
00:48Instead, I'm going to cancel this one and show you the existing
00:52ProcessMemoryRibbon I've written.
00:55As you can see, it has a designer surface.
00:58There's also a special section on the Toolbox that contains Office Ribbon Controls.
01:03So if I wanted to, I could take this CheckBox here and
01:05then drag it over and drop it on my designer surface.
01:09Of course, I can go to the code behind and manipulate this item, or read this
01:13information and push it into the Excel document.
01:16Now let me show you what I mean.
01:18I have this button here that I've added an icon and some pictures to.
01:23You can see it's a Button though.
01:24If I switch over to the code behind view by pressing F7, and scroll down here,
01:30you can see I've written a Button1_Click procedure.
01:33I start by creating a reference to an Excel worksheet.
01:36Then I go to the Globals area, and I find the current application.
01:41I get its Worksheet collection, and I tell it to add a brand-new worksheet.
01:45Once I've done that, I go into the sheet and I print some text into the A1 cell.
01:51I then print some other text into the B1 cell. And then I have a small link
01:57query here that goes out and talks to .NET and gets all the running processes on the computer.
02:04I then take those, apply a link query to that, and I say, I want to order them By
02:09their WorkingSet, how much memory are they using, in a descending order.
02:14Then I select out the processes.
02:18Then down here, I take the number of items--five, ten, fifteen, or twenty--and I
02:27convert this to Array and assign it to their process.
02:29And then down here, I'm going to generate some cells in my spreadsheet.
02:32This number is coming from my Ribbon value.
02:36If I go back to my Ribbon,
02:37you'll see that I have a Count dropdown here, and I've populated this with words
02:42like "Five, Ten, Fifteen."
02:43So that's why I'm getting that Count value.
02:45As you can see up here on line 20, I'm getting that from the Gallery, my
02:50Gallery, SelectedItem.Tag, and then I'm just returning an integer value.
02:55So down here, I am iterating over the processes.Length times one--the number of
03:02items inside this array--and I'm printing out a cell with the ProcessName,
03:08and its WorkingSet.
03:09Then I'm changing the NumberFormat for the cell.
03:13Once that's done, I select that range, and I create a brand-new chart.
03:19The name of the chart's going to be Memory Used.
03:21So, what's going to happen, when you click on my add-in, I'm going to look at
03:24the operating system, tell you the number of processes that are using the most
03:28WorkingSet memory, and then generate a chart out of that.
03:29Does that sound good? Let's see if it works.
03:33I'm going to press F5 to run the application, which will launch Excel, load my add-in.
03:39My add-in is living in this Add-Ins section.
03:46Office 2010 has a separate area for all custom Add-Ins. Here is my item.
03:51Here is my Count.
03:52There is Five, Ten, Fifteen, right?
03:56So I'll choose Ten.
03:59Now, I'm going to click in this button, and it generated this sheet with
04:06the names of the processes and the amount of memory used, and it generated
04:10this chart, which shows that Visual Studio is using the most memory of all the applications.
04:16Now, your user can uninstall this add- in by going to their Add-Ins menu. Or if
04:23you're a programmer, you can go to the Developer toolset to remove this.
04:26Let me show you how to do that.
04:28I'm going to go to File > Options, click on the Customize Ribbon, and then I'm
04:36going to check the Developer tab here and then OK.
04:41Now, I have a new tab right here, and I can go into the COM Add-Ins and find out my add-ins.
04:47Here is the Load Test Report add-in, and I can uninstall that.
04:52The other way to uninstall this is to go back to Visual Studio and choose this
04:58Build > Clean Solution.
05:01This will go out and remove it from the list of add-ins to run the next time Excel runs.
05:07So, I'll choose Clean Solution for my final demo.
05:10This concludes the section on Office automation.
05:13Many of the other Office products have similar add-in templates.
05:17I encourage you to explore your own ideas in your favorite Office tool.
Collapse this transcript
13. Extending Visual Studio
Understanding the extensibility model in Visual Studio
00:00Visual Studio contains an amazing assortment of tools to help in writing,
00:04testing, compiling, and deploying your applications.
00:07Microsoft had the foresight to make Visual Studio extendable and customizable.
00:11In fact, a lot of that tools you work with everyday in Visual Studio are really extensions.
00:15There are two main ways to extend Visual Studio.
00:18The simplest is to buy or download a third-party tool and configure
00:22Visual Studio to use it.
00:23This requires no code writing on your behalf.
00:26The second approach requires you to write some code and instruct Visual Studio
00:30to use this custom extension.
00:31If you want to write these extensions, you need to install the Visual Studio SDK.
00:36Hidden in the depths of Visual Studio are several extensibility frameworks.
00:41Most of the functionality of Visual Studio can be accessed via these APIs.
00:45One of these APIs is based on the ancient COM specification.
00:49You can build either add-ins or Visual Studio Packages that run on COM.
00:53In Visual Studio 2010, Microsoft introduces their new extensibility framework
00:57called Managed Extensibility Framework, or MEF.
01:02With this, you can build add-ins and component parts.
01:05Macros provide a quick and easy way to write plug-in code.
01:09They're written in Visual Basic.
01:10They are stored in a special Visual Studio macro files and can be imported into
01:15your Visual Studio using the Macro Explorer tool.
01:18COM add-ins have a higher potential for complexity.
01:21For example you can create walkthrough wizards or build new windows to add to the IDE.
01:26These are compiled into executable files and then deployed like any other application.
01:31Plus, you get to write them in your favorite .NET language.
01:34COM VSPackages are even more powerful than add-ins.
01:38These programs have been used by Microsoft partners for years.
01:41You have to be an accepted partner before you can acquire the SDK bits.
01:45With VSPackages, you get full access to the backend of Visual Studio.
01:49You can create your own visual designers, like the Windows Form Designer or the
01:53Silverlight Designer.
01:55If you've written your own custom languages, you can use Visual Studio Packages
01:58to create special code editors and integrate the compilers.
02:01MEF add-ins provide access to the fancy new code editor in 2010.
02:06With MEF, you are writing a WPF application, so you can overlay special
02:10graphics in the code editor.
02:12The rest of this chapter will show you how to use some of these
02:15extensibility tools.
Collapse this transcript
Adding external tools to the Tools menu
00:00Visual Studio contains a lot of tools for working with your code,
00:03but it's possible that it doesn't contain the tool that you want.
00:07After all, there are lots of companies out there creating developer tools.
00:10One of the easiest way to extend Visual Studio is by adding these third-party tools.
00:15I'm inside Visual Studio, and I have opened a solution called ExternalTools that
00:20contains two projects: one is a sample application we are going to add to the
00:24Tools menu, and another one is PixelSmithDesktop, which I am going to use for my
00:28disassembly demonstration.
00:30The External tools live up here in the Tools menu.
00:33They are in this section of the menu.
00:36For example, here is an external tool, a very simple one called Create GUID.
00:41This was added by Microsoft.
00:43When I click on this link, it will launch an independent application.
00:46And then all this does is allow me to generate a unique identifier and then
00:51copy it to the Clipboard.
00:53For a more exciting demo, I am going to add one of my favorite third-party tools,
00:57which is called Reflector.
00:58This is a free tool that lets you disassemble your .NET code.
01:01So I am going to go to Tools > External Tools, and then I am going to click the Add button.
01:09This is the title for the menu, so I am going to type in the word "Reflector".
01:15And then here, I choose the executable location.
01:17I click on this button.
01:20And the executable, I've made a copy of it on my desktop.
01:25Here it is, Reflector.exe.
01:27I've also created a copy of the executable from this solution file here
01:32OurExternalTool project,
01:33I've copied that over here because I am going to use it in a later demo.
01:37So I'll click on Reflector.exe and then click on Open.
01:43On this line are the arguments that I want to pass from Visual Studio to the
01:48Reflector application.
01:50In my case, I want to pass this argument called Target Path. I am done,
01:56so I am going to click on OK.
01:57Now what's going to happen is I can select one of these two projects, and when I
02:02run Reflector it's going to define the executable for this application and open it
02:07up and disassemble the code, like this.
02:13So there is PixelSmithDesktop.
02:15If I close this down and click on OurExternalTool and choose Reflector, I can
02:21see that it has added another executable.
02:25But this assembly tool is easy to use.
02:27I just click here, press the Spacebar, and it starts showing the disassembly
02:35information over here.
02:36Then I can drill down and find the class that I wanted to disassemble.
02:40And here's more information about it.
02:42I can look at the Visual Basic version of the code or disassemble it back into C#.
02:47Your choice.
02:48As I said, this is one my favorite third-party tools.
02:52For my next demonstration, I thought I would show you one of our tools.
02:55So I have created this application, which has a simple Windows form on it.
03:00And when you click on this button, I'm going to load the command arguments
03:05into this list box.
03:06I am going to double-click on this button to look at the code.
03:08Now what I'm doing is when the form is loaded in the constructor, I'm grabbing
03:12an array of arguments.
03:14Those are being passed in from the static void main.
03:18And then down here, I'm looking in the argument array, and I am looking for this
03:23delimiter, this semicolon.
03:25I am going to split the string based on that semicolon, and I am going to add an
03:28individual row to the list box based on that split string.
03:31So let me add this to the Tools menu.
03:34Click here. Go out to ExternalTools. Click the Add button again. Put my title in.
03:46I am going to find the executable.
03:49Remember, on your machine you have to find the correct location for this
03:51executable. For convenience, I put it on my desktop. There it is.
03:56And then I am going to put some arguments in here, like the Item Path, and then
04:01that delimiter--the semicolon--and then some other argument like Target Path,
04:07semicolon, and I'll choose one more, Solution Directory, semicolon.
04:13I will click on OK, and then I am going to run the application.
04:19And then I will click the Get Arguments.
04:21And you can see I have successfully passed three pieces of information from Visual
04:24Studio to my application. So let's review.
04:28You can easily add external applications to Visual Studio.
04:31If the third-party application supports command line parameters, you can pass
04:35information to them from Visual Studio.
04:37And that means you can customize the IDE even more to your liking.
Collapse this transcript
Creating macros
00:00A Visual Studio macro is code that can be run by the Visual Studio IDE.
00:04A macro is usually used to automate some task in Visual Studio--
00:08for instance, inserting Boilerplate comments, formatting or cleaning up
00:12portions of your code.
00:13Macros are written in a special version of Visual Basic. Plus, they are written
00:17and edited in a special version of Visual Studio.
00:20For today's demo, I'm in a solution called UsingMacros, and I've got the
00:24PixelSmithDesktop application open so that I can look at the source code.
00:28I'm going to double-click on MainWindow.xaml.cs.
00:31So to work with macros, you need to go to the Tools menu and then the Macros section.
00:39There are a number of menu items in here.
00:42The one we're interested in right now is the Macro Explorer.
00:44We're going to enable the Macro Explorer by clicking on this menu item.
00:49The Macro Explorer lives over here with the Solution Explorer and Class View,
00:54and it shows that we currently have two areas of macros.
00:58The top one is my RecordingModule--
00:59we'll look at that in a minute--and at the bottom we have this Samples module.
01:05Microsoft includes a number of samples for you to look at and learn from their examples.
01:09So, what I can do is click in my code and then come over here to this
01:14VSEditor Samples section and look at things like this BeginningOfFunction
01:19method, which I would guess--if I put my cursor here on line 30 and ran the
01:24BeginningOfFunction function, Run--it moves my cursor to the first line of the function.
01:31I'll put a comment in here in C#, and then I'll choose InsertDate.
01:38It put the current date in there.
01:43Now, if you want to learn how these macros work, you can right-click and choose
01:47Edit, which will then launch the special version of Visual Studio, and here's
01:53the code that Microsoft wrote to insert the date.
01:57A good tool, if you're brand-new to macros, is using the Macro Recorder.
02:02Recorded macros go in this area.
02:04Let me show you how that's done.
02:07I'll come over here to my code, and I'll turn on macro recording:
02:11Tools > Macros > Record TemporaryMacro.
02:16It's hard to see, but Microsoft has added a special macro recording toolbar up here.
02:23When I'm done, I'm going to click on the Stop Recording.
02:26Now, everything that I do in my code is being recorded and written as Visual Basic code.
02:31So I'll put a comment in, and then I'll draw a line across the screen.
02:45When I'm finished writing the macro, I can click on Stop Recording.
02:50Now, if you'll notice over here in MyMacros, there is a TemporaryMacro now.
02:55And I can right-click on that and go learn how to write comments and text inside my code.
03:00So, over here, I can see that I've got this DTE.ActiveDocument, and so on.
03:07DTE stands for Design-Time Environment, and ActiveDocument obviously is where
03:12my cursor was, and the Selection.Text is meaning I'm writing some text into
03:17that selected document.
03:20Then I'm writing a blank line and then some more text and a blank line and so on.
03:24So this is a great tool for learning how to write the Visual Basic code,
03:28especially if it's not your native language.
03:31You can also bring in your own macros.
03:33I have a series of macros I use all the time in Visual Studio.
03:37To bring in your own macros, you right -click and choose Load Macro Project.
03:42For today's demo, we're going to be using a VSMACROS file that's included in the
03:47Exercise Files folder and then the Assets folder.
03:49So, I'm going to click on this and then click on Add. And then up here, you'll
03:55see that I have two macros.
03:59We're going to look at the Search for Google macro.
04:02To show you how it works, I'm going to come down here and click on this
04:06BorderBrush property, and then I'm going to invoke my macro.
04:13As you can see, Visual Studio loaded a copy of Internet Explorer within the
04:17body of Visual Studio.
04:19It passed my string to the Google search engine.
04:23So you can see it's says search question mark q=BorderBrush.
04:26Let's look at the code.
04:29I'm going to right-click here. Choose Edit.
04:32I'm also going to show you that you can put a breakpoint in your code and run this later.
04:37So here, I'm getting the current selected text--that was my word BorderBrush.
04:42I'm trimming off any excess space and storing it in the string.
04:46I'm checking the length of the string to make sure that something is selected in the designer.
04:51Then I'm encoding that string, using the UrlEncode, so that it's safe to pass to
04:56a domain, and then I'm going to concatenate that on the end of this string here
05:01which is passing the string to the Google search engine.
05:04And then Navigate, that's telling Visual Studio to load the browser in the designer.
05:08Now, you notice I have a breakpoint in here.
05:11So I'm going to return to my code, I'll choose a different word, and then I'll
05:18right-click and choose Run.
05:20Notice that I switched immediately over to Visual Studio, even though I didn't
05:23have it running before.
05:24This is that special copy of Visual Studio. And at this point, I can step
05:28through my code by using F11, hover over the variables to find out what's
05:33contained in them, rewind my code by grabbing the yellow arrow.
05:37All the tools that work in Visual Studio's normal Debugger work in this special copy.
05:41I'm going to go ahead and remove my breakpoint and then press F5. And again, you
05:46see my query string pass to Google.
05:49So, a macro is just some Visual Basic code that can be executed from within the
05:53Visual Studio IDE, and I use it to automate common tasks.
05:56Let me show you one more thing.
05:58I'd like to attach this macro to a keystroke.
06:01I'm going to click on this Close button, and then I'm going to go to my Tools
06:05menu and choose Options.
06:09Next, I'm going to look in my Environment section and find the Keyboard section.
06:14The name of my macro is called SearchGoogle.
06:16So, I'm going to come over here and type in "goo".
06:19There is my macro, and now I'm going to come down here and press the desired
06:23keystroke that I would like to use to invoke this macro.
06:26I think Ctrl+Shift+G would be good.
06:30When I choose that, I see that that's already assigned to a pre-existing
06:34function inside Visual Studio.
06:35I have to decide if I want to erase this shortcut and replace it with my own, or
06:40pick a different keystroke.
06:42I never use this keystroke,
06:44so I'm going to go ahead and reassign it to my Google macro.
06:47Then I'll click OK, and then I'll come down here, and we'll try this one
06:52here, Canvas.SetTop.
06:54So, I'll click here, and then I'll press Ctrl+Shift+G, and it
07:00worked beautifully.
07:01So, let's think about this.
07:03A macro is just some VB code that can be executed from within the Visual Studio
07:07IDE, and I use it to automate common tasks.
07:10For more complex scenarios, we need to turn to other tools, but that's a
07:13topic for the next movie.
Collapse this transcript
Using the Extension Manager
00:00Visual Studio has supported an extensibility model since day one.
00:04Unfortunately, creating an add-in for Visual Studio has been a task for geniuses.
00:09You had to understand the Visual Studio SDK, the COM model, and a
00:13complex deployment mode.
00:15Most of that changes with Visual Studio 2010.
00:18By using the Managed Extensibility Framework, or MEF, as a replacement for COM,
00:23Microsoft has made it super simple for us to build and deploy add-ins.
00:27In this movie, I will show you the Extension Manager, which is the first step in
00:31understanding extensions.
00:33I am inside a solution called Extension Manager, which contains two projects: a
00:38Console application and our old friend PixelSmithDesktop.
00:42To work with Extension Manager, you go to the Tools menu and choose
00:46Extension Manager.
00:49On this tab, you can see any installed extensions in Visual Studio.
00:54I have two here because I've installed them in earlier movies: the DevExpress
00:58tools and the Snippet Designer.
01:00If I click on the Updates node, the tool will contact Microsoft web site to see
01:07if there are any updates for my installed extensions.
01:10As you can see, there are none.
01:12Here is where it becomes really powerful.
01:15I can click on the Online Gallery.
01:17The tool will go out to the Microsoft designated web site and give me a list of
01:22all the available extensions.
01:25Currently, they're sorted by the highest rank extensions.
01:27Now if I decide that I want this Productivity Power Tools application, I can
01:32click on the Download button.
01:34I will do that in just a second.
01:35Let me show you a couple of these tools.
01:38Here's one called the VS10x Code Map.
01:42As you can see, when you install this extension, it adds a special colorized
01:45navigation bar to the regular code window.
01:49That PowerCommands is another favorite of mine.
01:52This one gives you all sorts of useful commands, especially when you right-click
01:55items in this Solution Explorer.
01:57For example, here you can see I can open the containing folder or launch a
02:01command prompt directly from this app.xaml file.
02:05The one I'm going to install for today's demo is the Productivity Power Tools.
02:09All you need to do is click here and then click Download.
02:15Next, I need to click the Install button,
02:18and it takes me over to the web site, so I can read more about that extension.
02:23I will have to restart Visual Studio here.
02:27Now, I can read about this extension.
02:28Let's look at a couple of features.
02:31There is the Solution Navigator, which gives you an alternate view to the Solution
02:36Explorer, and then another one that's a favorite of mine is the new Tab UI.
02:43Also, some people like this automatic C# completion of your brace structure.
02:47So if you type in a beginning curly brace, it can type ending curly brace for you.
02:51I am going to close the browser, and then I am going to switch back to Visual Studio.
02:58Notice at the bottom of the screen it says you must restart Visual Studio,
03:00so I am going to choose Restart now.
03:05After a few seconds, Visual Studio restarts and reopens my solution.
03:10And now I can go to my Tools menu and go to Options.
03:19And there's a new node in the Tools Option section.
03:22Here I can turn on the different extensions. Like if I want to have the
03:27Automatic Brace Completion, I can click here.
03:29And if I wanted to disable that, I can turn it off.
03:32Extensions are written using WPF.
03:35So this is using a WPF UI.
03:37Here is the Document Well Tab.
03:40This is where I can change the way the tabs look inside Visual Studio.
03:43You can see I can do things like tab sorting by project in alphabetical
03:49order and then choose OK.
03:52Now, I am going to open up a few code files.
03:54I am going to open this Program.cs and make a change.
03:59Here is one of the additions.
04:01So there is now a red dot on that tab to signify that I have made a change to that file.
04:05I am going to go here to this MainWindow. xaml, and notice that the tabs are now colorized.
04:13This blue tab is for all the files that are inside the console demo.
04:17And I open multiple tabs here in the PixelSmithDesktop demo,
04:20you see they show this kind of brown theme and it's sorting them alphabetical by project.
04:27I don't have enough files to show you that in great detail, but if you have 20
04:30or 30 files opened it is really handy to see them in alphabetical order, when
04:33you're looking for that particular tab.
04:36I have to say I'm completely sold on this version of Visual Studio.
04:39This is the way extensibility should be, from the consumer's perspective.
04:43Extensions are easy to find, simple to install, and they automatically update if
04:48there's a newer version of the extension.
04:51They integrate easily with the Visual Studio IDE, too.
04:54The reason they integrate so well is because of the managed
04:56extensibility framework.
04:58And that's the topic for the next movie in this chapter.
Collapse this transcript
Creating an MEF add-in
00:00There were some major changes made to the architecture of Visual Studio in this release.
00:05The old Visual Studio interface, including the code editor, was replaced with a
00:09new UI written in WPF.
00:11Then the extensibility framework was enhanced with the new managed
00:14extensibility code bits.
00:17What this means for .NET programmers is that it is now easier to write a
00:20custom editor plug-in.
00:21You no longer have to learn the ugly COM object model to be a successful add-in author.
00:27I'm inside Visual Studio, and I've opened the CreateMefAddin Solution.
00:32This contains two extensibility projects.
00:36The first one is enabled. The second one I have unloaded temporarily. That's
00:40why it says unavailable.
00:42Before you attempt to create an extensibility item, you need to install the
00:46Visual Studio SDK, which I've done.
00:49Now I can go to File > New > Project and choose my language and then this
00:57Extensibility note. And here are some other new items that were added for the SDK.
01:03This is a syntax highlighter.
01:06This adds graphics to the editor margin.
01:09This applies adornments to the text, and the one we are going to look at today
01:14is called the Editor Viewport Adornment.
01:16This is going to draw a purple rectangle in a floating section of the UI.
01:21That's what the adornment layer is.
01:24I have already added a project of this type, so I am going to click Cancel, and
01:27then we are going to take a look at this code here.
01:31Most of the code that's inside this Floating Code Counter Factory is managed
01:35extensibility framework code.
01:36I suggest you spend some time looking at the code in there to learn how it works.
01:40I don't have time to give you a tutorial on managed extensibility framework.
01:45I am going to look in this class here through, FloatingCodeCounter.cs.
01:53We are going to draw something on the adornment layer, which as I said is
01:56a floating area of the window, and then here's the important parts for this application.
02:02I am using these bits of code to generate a WPF brush, and then this section
02:07is creating a rectangle, a 30x30 rectangle, and using the brushes to draw that rectangle.
02:12And then at some point, I'm going to get the adornment layer and add my floating
02:18code counter to the adornment layer.
02:20Then this wires up a few events to listen. If you resize the Visual
02:24Studio window, then it will move the adorner window to a new section.
02:28I'm ready to test out this application, so let me save the application and
02:35rebuild the application.
02:37Now lets think what needs to happen if I am going to test out my extension.
02:42I need to install the tool in Visual Studio. Extension manager is one of the
02:50ways that I can do that.
02:51There is also a special type of installer, called a VSIX installer.
02:55You'll notice that in this project there is a file that ends with vsix.manifest.
03:01When I compile my code in this bin folder, it will create a file with
03:08the extension vsix.
03:10So when I'm ready to test the application, I need to launch another version of
03:14Visual Studio, especially if I want to debug my add-in.
03:20I need to launch this second version and then attach the debugger to it, and
03:23then when you work in that second version of Visual Studio, you can walk through
03:27the code in this first one.
03:28To do that, you would click on Debug > Start Debugging.
03:32Using this method, however, is very slow because it's attaching the debugger to Visual Studio.
03:36So I am going to choose Start Without Debugging, which will launch the second
03:40version of Visual Studio but wont attach the debugger.
03:42I will show you what it looks like.
03:49Notice that at the top of my window it says Experimental Instance.
03:52That's to tell me that I'm in the second copy of Visual Studio.
03:55Now before I go any further, let me make one change. Tools, Options, and then
04:03I'm going to go to Projects and Solutions, and uncheck this Save New
04:06Projects when created.
04:08I am going to be creating a lots of sample projects.
04:10I don't want to cut up my folder with applications I am not going to use.
04:13So I'll click on OK, and then I'll create a brand-new project, create a console
04:20application and here's my Code Editor window.
04:26There is the first instance of Visual Studio in the background, and then you can
04:30see when I resize this window, this rectangle moves to a new position.
04:33That's the floating window.
04:34When I close this second instance of Visual Studio and choose Discard, I get this
04:41dialog because of that Tools Option settings I just changed. And now I am going
04:45to show you the code in this second project.
04:49So I am going to reload the second project and unload the first project.
04:57Then I am going to save my changes, and we are going to rebuild my application.
05:04This application is slightly more interesting because it has a better UI.
05:07I have written a WPF user control, and I've added some text blocks, and in this
05:13case, I've added a text block called Total Lines Text block.
05:16I'm going to read the line count inside the code window and update this number.
05:20The way I am doing that is I am going to exposing some properties on this window and
05:24then in the FloatingCodeCounterV3.cs file, down here in this area, I am using
05:32this to talk to the Visual Studio editor and get the line counts again.
05:37Look at this code to see how it works. I am ready to try it out, so I am going to
05:42go and choose Debug > Start Without Debugging.
05:50The Experimental Instance of Visual Studio runs, I click on New Project, choose
05:55a console application, and there is my new floating window.
06:01You can see the old floating window is still there.
06:03This is interesting.
06:04It was installed inside this experimental instance of Visual Studio. So I
06:08can uninstall this purple rectangle by going to Tools, Extension Manager,
06:14and it shows that I have the FloatingCodeCounter and the
06:17FloatingCodeCounterV3 installed.
06:20So if I click here and choose Uninstall and then restart Visual Studio, that
06:28purple rectangle disappears.
06:29I am going ahead and leave with it like it is.
06:32Now, watch what happens when I come over here and type in some lines of
06:35code. The total lines updates to 15 and if I put a few blank lines in and show
06:41have 4 blank lines of code, and if I put comment in here, that updates the
06:49comments section. Wow.
06:52The ease of creating these extensions is incredible.
06:55I know I've created a few extensions in the old days, and this is much
06:58easier. Plus, the ability to use the extension manager to add and remove
07:02extensions is very powerful.
07:04In the next movie, I'll show you how to install extensions and add them to the
07:07public extension store.
Collapse this transcript
Deploying and installing an add-in with VSIX
00:00If you've been following along in this chapter, you probably have ideas for
00:03building your own add-ins and custom tools for Visual Studio.
00:07How do you distribute those to other developers?
00:10The best way is to use a special installer, known as V-S-I-X, or VSIX.
00:15You can use a VSIX installer to install many Visual Studio editions, including
00:18projects and item templates, ToolBox controls, .NET assemblies, and even
00:23custom start pages.
00:25A VSIX is nothing more than a compressed file that contains your compiled code
00:30and some special files: a content type XML file and a VSIX manifest.
00:36Let me show you how to create a VSIX installer.
00:40I have a Solution called CustomVSIXInstaller opened, which has a project in it
00:45called LyndaCustomBizTemplate.
00:47So this thing that I'm going to install today is a project template.
00:51You might remember from earlier movies that you can export your own templates by
00:56going to File > Export.
00:58You're going to choose to export a Project template. And then here, we're going
01:07to give this template a little bit of a more interesting description, like that.
01:22I don't want to automatically import this template into Visual Studio,
01:25so I'm going to uncheck that check box. And I will want it to show the output
01:29files folder when I'm done.
01:31Now, you can see I have a file from my previous runthrough.
01:37So I'll click on Yes to delete it, and here is that custom ZIP file.
01:42This contains the ZIP information for the custom template.
01:46Now I'm going to create the custom VSIX installer.
01:48I'll go over to my solution, right- click, and choose Add > New Project.
01:57I'm going to go to the C# section, then to Extensibility, and then to
02:03the VSIX Project. And I'm going to call mine lyndaBizTemplateInstaller and then click OK.
02:17At this point, it's a simple matter of filling out some details--whatever I
02:21want to call my Product.
02:24And then I'm going to come down here, and this is the content that I'm going to install.
02:28So, I'm going to click on the Add Content.
02:30Here, I get to pick the kind of installer I'm making.
02:34Project Template's what I'm working with, but you can see there's also VS
02:37Packages, there's Custom Extension Types, there is the MEF Components from an
02:42earlier demo, and here is the Toolbox Control.
02:45Today, I'm going to Project Template, and then I'll come down here and choose
02:50that ZIP file that I just saved out.
02:53So, I'm going to go over here and copy this file path, return back to Visual
02:56Studio and paste it in here. I'll paste it in here.
03:04It'll be more useful if I paste it in here. And there is my ZIP file and then I click on Open.
03:09One more thing: I need to type in a subfolder here.
03:11I don't need to, but I'm going to have this customize the Open Project window by
03:17doing this, and I'll click on OK.
03:20Next, I need to save my project and build or rebuild my solution.
03:26At this point, I can go out to my installer, right-click, and choose Open Folder
03:31in Windows Explorer, and then drill down into the bin > Debug folder, and here is
03:38the custom installer.
03:39Now, I will give this installer to other developers.
03:43They will take it, and they'll double-click on it.
03:47It shows information about what they're installing,
03:49the Lynda Business template.
03:51Now, I didn't put in a licensing term.
03:54Now I'm going to click the Install button and then Close.
04:00It's told me I needed to restart Visual Studio, but I could relaunch a second
04:04copy, which is what I'm going to do.
04:10And then I'm going to go to File > New Project, and there is the lynda folder,
04:16and there's our lyndaCustomBizTemplate.
04:17Now, I'm going to choose this one and click OK, and now I have my custom files
04:24all loaded in from that CustomBizTemplate.
04:26There's one more thing you can do with this template, is you can go out to this
04:32site, the Visual Studio Gallery, and click on Upload, tell them what kind of
04:40item you're uploading, give them the path to your template, and then it'll be
04:44shared with all the other Visual Studio 2010 users when they go to the Tools > Extension Manager.
04:53It'll show up as an item they can install from this location.
04:57Now you know how to create a custom add- in and a custom installer to share your
05:01new tool with the .NET community.
05:03I can tell you from past experience that this way of doing extensions is far
05:06superior to the old model.
Collapse this transcript
14. Configuring Your Application
Working with configuration files
00:00.NET applications use configuration files to change the way they behave at runtime.
00:05These configuration files are located in the same folder as the
00:07application executable.
00:09Let me show you a few configuration files inside Visual Studio.
00:13I've opened a Solution called ConfigurationFiles.
00:15It contains a ConsoleApplication, a FinancialLibrary DLL, and a WebApplication.
00:21I'm going to start by looking at the WebApplication.
00:24They commonly have a configuration file called the Web.config.
00:28It's added automatically in the project template.
00:31And this contains settings in an XML format that might be used by this web
00:35application. Now, in web applications, you typically have at least one Web.config
00:40in the root directory of the web site, but you can also put other Web.configs in
00:45children directories.
00:46So here, you can see it's just XML.
00:49There's a section called configuration, and below that is a subsection called
00:53connectionStrings. And inside this connectionString is something with the name
00:58ApplicationServices, and then below that it has some details of how to connect
01:02to this particular SQLEXPRESS database.
01:05This implies that in my C# or VB code, I could open this
01:10connectionString through some code.
01:12Other applications that you might create, like console applications, don't have config files.
01:19So let me look at this ConsoleApplication and see what's going on here.
01:22This ConsoleApplication has a Reference to the FinanceLib.
01:26That means when I deploy the application, I have to deploy the Console's EXE,
01:31and I also need to deploy the FinancialLib DLL.
01:35Luckily for me, in Visual Studio, when I do a compile--I'm going to come here and choose a
01:41Build Solution, I do a Show All Files and then look in the bin directory--
01:47you'll see that it created the EXE, and then also after it's uncompiling the DLL,
01:51it made a copy here.
01:53What I want to do is write a configuration file that allows me to change the
01:56location of this DLL, to put it in another location on my hard drive.
01:59I need a config file to tell this Common Language Runtime where to go probe for that file.
02:06So, the first thing I'm going to do is add a configuration file to this ConsoleApp.
02:09I'll right-click and choose Add > New Item.
02:13Then I'll come over here to the Search text box and type in "config".
02:20Here is the one I'm looking for: Application Config File.
02:25Leave the name at the default, App.config, and click Add.
02:29Now, the CLR, when it's looking for a configuration file for a Windows
02:33application, expects the file to have exactly the same name as your
02:36finished executable.
02:37Now, my finished executable's name is ConsoleApp.exe.
02:41That means my config file should be called ConsoleApp.exe.config.
02:48The good news is if I compile the application right now, you'll see that there's
02:55now two copies of the config:
02:57this is one which I edit inside Visual Studio and then every time I compile it
03:01makes a copy of this and gives it the correct name.
03:04Therefore, if I ever change the compiled version of the name later, Visual
03:07Studio will change the name of the configuration file.
03:10I need to do one more thing.
03:11I need to write some code in this configuration file that will probe in new locations.
03:16I've already got the code written.
03:19It's up here in this Assets folder.
03:20So here's what I'm going to use.
03:24I'm going to copy this piece here and then switch back to the App.config and paste that in.
03:34So, this is telling the Common Language Runtime: at runtime when you go to find
03:39an assembly, I want you to search these two extra locations:
03:44otherfiles and the bin folder. So now I'm ready.
03:48I'll save and I'll compile.
03:50Now, I'm going to open this Folder in Windows Explorer, and what I'm going to do
03:58now is I'm going to take this Debug folder and copy it and move it somewhere
04:01else on my hardware.
04:02And so I think I'm going to move it to move it to my Desktop and paste that in.
04:05And then I'll go in here, and I'll see if this application runs.
04:16I'll double-click on this and the application runs and gets my payment value.
04:23Okay now, let's put a folder in here, and call this one "DLLs" like that.
04:32Now, I'm going to take this FinancialLib file.
04:34I'm going to move it in there.
04:36Now when I try to run this application, it throws an unhandled exception because
04:41the Common Language Runtime is looking for that DLL and cannot find it.
04:44So, what I can do is come into this configuration file--this is just a text
04:50file--and I can create another probe.
04:54I can say instead of looking in the bin folder, let's look in that DLLs folder.
04:59I'll save that file, and now I'm going to double-click on this ConsoleApp.exe
05:06again, and it runs successfully.
05:09So that's how I can change the runtime probe for that file. So let's review.
05:15A configuration file is loaded by the .NET runtime when the application starts up.
05:20The application reads sections of the config file to determine what settings and
05:23data to use at runtime.
05:24You can write your own code to read the sections of the config file too.
05:27Visual Studio includes a clever tool to simplify creation of application and
05:32user-specific settings.
05:33I'll show you how this settings tool works in the next movie.
Collapse this transcript
Using the Settings Editor
00:00Your application may need global settings.
00:02These are typically stored in the configuration file and loaded at startup time.
00:07These settings affect the entire application, regardless of who is using the
00:10application on the local computer.
00:13You may also want to store user-specific settings.
00:15Each user on the local computer would get their own copy of the
00:19configuration value.
00:20.NET has a