ALTE DOCUMENTE
|
|
Getting Started with Visual C# 2005 Express Edition
Hello and welcome to this sixteen lesson video series that will help you get started with Visual C# 2005 Express Edition. These lessons and the accompanying documents with supplemental readings are designed to take someone who's a complete novice, who has never written any code before or maybe has just written a little code a while back, and help them be productive in writing real-world applications within a few hours. Now if this describes you, you're in the right spot. If you're already an experienced developer or an experienced .NET developer, quite honestly, there are probably better ways for you to spend your time. If you're looking for videos and advanced concepts for our new features in Visual C# 2005 or Visual Studio 2005 or any of the latest products from Microsoft, please navigate to www.learnvisualstudio.net for instructional video tutorials aimed at all skill levels. So if you're just getting started, the aim of this series is to help you become productive quickly.
With that as our goal, please keep in mind that there are many concepts that I either gloss over or just ignore completely. So you may want to use these resources as a good introduction. However the videos are not intended to replace reference books or online help that comes with Visual C# 2005 Express Edition. So I recommend that at the end of this series of lessons to also dedicate yourself to other books, online resources or even my website www.learnvisualstudio.net for more instructional videos to help you come to a more complete knowledge of Visual C#, the C# language and application development. Developing software, especially with a language like C# and a software application like Visual C# 2005 Express Edition, is a lot of fun. Sure it will be a little intimidating at first, but if you just take a little bit at a time, just like everything else in life, you'll start seeing results. In my mind there is nothing more gratifying than imagining some software application that I want to build and see it begin to come together, working through the problems and the hurdles, and then finally seeing the end result and letting others use it.
I hope that after this series of lessons you get i 21121o1414v nfected by the programming bug and begin to realize how exciting this can really be. Okay, it's time to get started.
The lessons are arranged in the following way -
Lessons 1 through 3 focus on the workflow of building applications and getting comfortable with how to navigate around the Visual C# 2005 interface.
Lessons 4 through 7 will focus on the C# programming language itself, as well as an introduction to the .NET framework.
Lessons 8 through 11 will focus on working with data, displaying data from data sources like a database or an xml file, and handling the errors that come up in your application while it is running.
And then lessons 12 through 16 will demonstrate the creation of an entire application from beginning to end involving design, development, testing, and then deploying your application.
The application that we will build in the last five lessons will be an RSS Reader. Now if you're not familiar with the term RSS, that stands for "Really Simple Syndication," and it is one of the latest trends on the internet. It allows web sites, especially those web sites that update their content frequently like blogs for example, to publish a short description of new articles as they are posted on the web site. So users can subscribe to an RSS feed, which is basically a URL that points to a file on the web that contains the title of the article, a description of the article, and hyperlinks so people can read the actual article. Now to subscribe to a feed, the user is going to need a program that knows how to interpret an RSS feed, the special format that is used. Then it needs to periodically check each news feed that the user subscribed to, so that they can determine if any of his favorite web sites have any new content available.
So
many popular web sites have these little
And then for a given channel, we will update it in order to find all the news items for that channel, and then we will be able to click through, as you can see over here on the left-hand side I'm looking at the titles for the news items and then on the right-hand side I can view the full title, the description, the date it was posted, and then I have a little browser where I can look and see a little bit more about that web page that its pointing to and click this little "expand" button here which will pop open a web browser that allows me to view the page full-screen if I would like to. But we have a lot of information to cover before we get to this point. I just wanted to dangle a carrot out in front of you so that you stay engaged in this series of lessons. Just about everything we will learn in the next ten lessons will build up to creating that application. So make sure that you understand each concept before moving on. So let's get started by talking about the basic steps of creating applications, and we will begin by creating the most simple application we possibly can. All that we want to do is to create a Windows application, it's also known as a Windows forms application, and on it we will add a single button to the form that is created by default for us. And when the user clicks on this button we're going to display a pop-up message that says "Hello World".
Now, "Hello World" is kind of the universal first program that we always build regardless of the programming language or programming tool, because it helps you see the entire workflow of creating a program without having to introduce too much complexity so early into the learning process. So let's begin by going to file - new projects, and then in the new project dialog we're going to select "Windows application" as the template that we will begin with. Then we're also going to change the name from "WindowsApplication1" to "HelloWorld" and then we will select the "OK" button. It takes a moment or two, but what C# is doing at this point is creating a template for a Windows application. So by default, after a moment or two, what we will see is a form that will pop up with nothing on it.
Now at this point we have a complete Windows application. Let me do this just to prove that point. I'm going to use my mouse cursor to go to this little button. This is the "start" button - this allows us to run our application, in what's called "debug mode." It allows us to see what the application will do if we were to stop right now and compile it and give it to a friend. Let's go ahead and hit "start," and the program is compiling right now, and as you see what you have is a complete Windows application. Admittedly it doesn't do anything at this point, but you have your form and on the right-hand side you have your typical Windows control box to minimize and to make it full screen or to bring it back to its original size and also to close the application. And then you have the control box here on the left-hand side that allows you the typical Windows commands to minimize and maximize and close and so on. So to stop this application we will just click the "X" button.
Now the next thing we want to do is to put a button on our form, and to do that we're going to hover our mouse cursor over the toolbox. When we do notice that it jettisons out from the left-hand side and it allows us to scroll through all the controls that are available to us. Now a control is typically a visual element that allows the user to either interact with your application or read information from your application. Each of the controls is a little bit different; they have different capacities and different reasons why we want to use them. A button for example, as we drag it and drop it off the tool box, will allow the user to indicate to the application that some action should be taken. So in this case whenever the user clicks the button, they would expect something to happen, and in our case, a message box that displays "Hello World." So again let's go to our "start" button, run our application, and I want you to notice that we have a fully working application again. We have a button and we can click the button as many times as we want to, or I can even hold the button down. Notice that it's a little grayed out when I do that.
But nothing really happens at this point because we haven't written any code that reacts to that button click. So that's really our next step. So this can be a little bit tricky the first time you see it, so pay close attention. What I'm going to do is double-click this button and when I do, another file will be opened within our main area. Notice that we have a series of tabs at the top, this is a good time to start talking about that. We can see the design view, which gives us a visual representation of what the form is going to look like. We can also see what's called a code view; this allows us to write code that in this case reacts to things that happen on our form. Now when we double-click that button it created what's called an "event handler." We're going to talk about event handlers in lesson three.
So I don't want to get too far ahead of ourselves here. But notice that we have a blinking cursor on the correct line between these two little curly braces. So you want to make sure that you typed the following line of code: messagebox.show ("Hello World") and then a semicolon (;) at the end. Now again, we will take time to discuss all the things that you see here. Let's not get bogged down on that just now, let's just take it at its face value and type this information in, and then what I'm going to do is select the "start" button one more time. And when I do. this time I'm going to click the button and I will get a little box that pops up a message box that says "Hello World." And I can click this as many times as I want to and each time I do, notice that the code that I wrote will be executed again. So let's go and close our application.
So we successfully created our first Windows application, a HelloWorld application. The next thing that we want to do, especially if we want to keep this application for future reference or to work again on it in the future, what we need to do is save our project. There are two ways to do this. The first way is to go to "File Save All," or we can just use the toolbar shortcut for save all. While we do, notice that there is a little dialog box that pops up that asks for the name of the project, which we already named HelloWorld whenever we created the application.
Then it will ask us the location that we want to save our project to. Now by default it's going to attempt to put it in the My Documents directory, under a subdirectory called Visual Studio 2005\Projects. And it will create a new folder called HelloWorld. So let's go ahead and use the default. There is one good reason why you want to do this as opposed to putting it on a different location on the hard drive, and the reason is that a lot of backup software that's available today for the Windows XP looks by default in your My Documents directory and tries to save it. So if that's the case then you will automatically get the benefit of backing up all your source code if you would follow this old convention. So our application is now saved. We're going to come back to that part in just a moment. But let's talk about how our project is organized.
First of all there is a lot of code that can be added to a software application, and all the code that gets compiled into a single program is called a project. So a project is an organizational unit that keeps track of all of the files that will all be compiled together into one application, usually a .exe program. There is also another organizational unit called "solution." You can see the hierarchy between the projects and the solution over here on the right-hand side in the solution explorer.
This shows us first of all the name of our project "HelloWorld," then all the files and other important information that makes up our HelloWorld project over here below it, such as our form one for example. And then there is another file like program.cs that we didn't have to modify, that were created automatically for us whenever we use the Windows application template, whenever we work by creating a new application. Now we have a particular project. We only have one form, it was very simple, but we can have dozens of forms potentially within our application. And so they will all be listed here within the solution explorer underneath the main organizational unit called HelloWorld, that's our project. Also, solution can contain multiple projects. Now the reason why we want to do that may not be very clear right now. But once you get into more advanced software development, the concept of a solution might come in handy. So let's just table that discussion for right now and focus right on the main organizational unit being our projects.
And again to put in another way, the only purpose of our project is to keep track of all the files and all the code that belongs to a single application. Now let's take a brief moment and open up our Windows Explorer and I'm going to "start," I happen to have a pin here, but you may have to navigate to "All Programs - Accessories - Windows Explorer" in order to view what's on my hard drive. Now what was placed on my hard drive? So by default, mine comes up to My Documents, am going to find my Visual Studio 2005 folder, and then I'm going to find my Project subfolder, and notice that I have a HelloWorld folder underneath that. Now we have a solution file here. We can pull things over so that you can see things a little more easily; keep in mind that is the top organizational unit. So a solution can contain projects.
So what will happen is there will be one project folder for each project in the solution. Since we only have one project, there is only one subfolder called HelloWorld, after the name of the same thing. So this is the folder for the project itself. Notice first of all that we have a Visual C# project file. So this will keep track of all the files that belong to our project. Then we also have a few files with similar names - Form1, Form1.cs, and Form1.DesignerCS. The purpose of these will become more obvious in the next lesson, so we will just table that discussion too for right now. But then we can also see our little Program.cs and so on. Now there is a special folder where our compiled applications live once we run the application.
Whenever we run the application there is an interim build called the debug build that allows us to run the application, and then also see the results of running the application by putting break points and things of that nature which we will talk about in just a moment. But if we want to run our application now, we have two places where we can run it from. We can go back to our Visual C# interface and then select the "start" button, or we can just go down here and drill into our bin folder, which is a special folder that was created automatically for us. And then go to the debug folder, you will notice that there are a couple of files there but the one in particular that has the type of application is our HelloWorld application. So we can double-click it and we can run it just like we were if we were to run it a few moments ago in Visual C#.
So we have a complete application, and if somebody has the .NET framework we can actually take this application now and put it on their hard drive and it would run. But there is actually a better way to do this, and that is not the preferred way, so let's just not do that at the moment. I just want to let you know that a real application has been built, it is a debug version of the application but it is still able to run just as if we were to run it or install it on somebody else's computer. So this consists of a pretty good overview of what happens on our hard drive whenever we save our application, also the organizational units of projects. We have projects which contain files of source code, and then we also have a solution which contains multiple projects. So we have seen how that is structured on our hard drive. So I'm going to close Windows Explorer for right now and move on.
So the solution explorer has a very important purpose within Visual C# because it allows us to manage files that are within our project. Now let's talk for a moment about some of the things that we can do with the solution explorer. First off, if we were to accidentally or purposefully close any of the tabs within our main area by selecting the little "close" button here on the right-hand side of the tab, we can get back to that form or the code view in one or two ways. We can either select the icon that represents the form and then select the "code view" button, or select it and click the "view designer" button, or we can just double-click the icon and the default view- the Designer View- will open up.
Additionally, we can add or remove files from a project here as well. We can right-click and select "Add New Item," which allows us to select any of the possible files that we can insert, or we might just want to insert a new Windows form, in which case we will select this here. We will talk about this more a little bit later whenever we're developing more intense applications.
You can ignore this right now by just clicking somewhere in the white area to get rid of that context menu. Also at some point we will probably want to shut down our computer, so we will need to know how to close our application and then reopen it again. So once you make sure that your project is saved by clicking the "save all" button, and then we can select "Close Solution." This will unload the project from Visual C#, or we could just shut down Visual C#, it will prompt us if we have any unsaved changes. To begin let's go to Start - Visual C# 2005 Express, and open up our project.
We can either select from an existing project , our HelloWorld project is listed here, or we can go to File - Open Project and then we will need to navigate to the appropriate directory My Computer, let's go to our Desktop here or rather our Documents. So, Bob's Documents - Visual Studio 2005 - Projects - HelloWorld, and then I can select the solution file or the project file, whichever one is available to me first. Then I will select "open," and now I have my project again. You see it pops open any other forms that I had opened the last time that I saved the application, and we get to start working on our application again. And that is how to shut down and reopen our applications.
Finally we're going to be writing a lot of code, so what I want to do very early on is introduce the concept of Debugging. And when I use the term Debugging what I really mean is that we want to pause the execution of the application so that we can watch each line of code execute and look through the code and see if there is something that catches our eye. Now this is very useful whenever our programs get much larger where we need to observe what is going on behind the scenes, because sometimes our applications may not give us enough feedback or something is going wrong and it is hard to pinpoint the exact problem. So what we want to do in that case is set a break point which will stop the execution of our application while we're in Debug mode or running our application with this little "start" button here, and it will allow us to step through each line of code at our own pace and watch how the application executes.
Now again this is not going to be very impressive with just one line of code within our application. But if we were to have more lines of code like we will in the upcoming lessons, this is a very, very important feature of a development environment. It will allow us this rich functionality to watch the code execute. So to create a break point what I want to do is get back to my code view, so let's select our form1 and then select View Code, and what I want to do is to put a break point on the line of code that we wrote, the MessageBox.Show HelloWorld. So to create a break point there are actually a number of ways, the easiest way is to just go to the left-hand side of the line of code and then there is this little grey area - we want to click it and when we do notice that a little red button appears, and then our text is highlighted in red. This tells us that the application will stop executing at this line of code and will wait for us to take over. So now let's run our application by clicking the "start" button.
Our application is executing just fine. Whenever I click Button1, notice that red line of code now turns to yellow. This tells me that it is the next line of code that will be executed. Now to walk through this line of code what I can do is simply use one of these buttons that have been added to our development environment because we're in Debug mode. So I'm going to click either the "Step Into" button or F11, but I could just as easily select the Step Over or Step Out. We will talk about those much later in this series of lessons.
For our purposes right now, let's just click this "Step Into" button and when I do, notice that it executed that line of code to display a message box. I'm going to click "OK" and go back to our code view, and notice that the next line of code in this end of our little method here - this little curly brace. So we can just either select the Step Into one more time, or I'm just going to select "continue executing the application," and now we're running the application again in full speed. So just keep that little tip in mind, we're going to use that in the coming lessons to watch how our code executes especially in lessons number four and five.
So, phew, that was a lot of information for the first lesson. I hope you enjoyed it and if there is something that did not make sense, please watch it again or just continue on and some of the questions you might have in your mind might be answered in the upcoming lessons. So this would also be a good time to read through the first two or three lessons in the accompanying document that I provided. If you enjoyed this video please visit www.learnvisualstudio.net to download and watch over 500 videos just like this one aimed at all skill levels on many different topics related to C#, Visual Basic.NET, ASP.NET and more. Thank you.
|