Home
Username:
Password:
Introduction to Microsoft C# 2008 Tutorials

Visual Studio 2008 / Code Snippets

Subtitles of the Movie

Now let's talk about code snippets. This is a really cool thing that's in, it's part of Visual Studio and it's part, a functionality in C# but I want you to be aware of it because if you're like, like me and a lot of other developers, unless you develop 24-7, well, probably not 24-7. More like 23-7, right? Unless you develop almost continually for a living, little syntactical things drive you nuts; If Statements, Do While Loops and all those sorts of things. Well, there a function in here called Code Snippets and notice that if I want to put an If Statement right here in my code, there's a really cool thing I can do. All I have to do is point, left click at the position where I want to put the code and then just simply right click and in the pop-up menu that comes up, just choose Insert Snippet. Now, notice. This has a number of things in here and we'll just kind of go through some of these. If I double click on .NET X30, you can see there's, I can do some things there and I'll just hit backspace and come out of that. Actually I'll take all that out. Office Development, there's some. Let's look at Visual C# and then we'll come back to the others. So I double clicked on Visual C# and then I want to do an If, just an If Statement, right? And so I double click on If and notice it put it in there. It put the basics for the If Statement in there for me. Now, let me just delete or actually I can just undo and let's right click and look at some snippets out there. Let's say that I want to create a Struct. Double click and there it is. Guess all I have to do now is just change the name of my Struct because that's going to be my variable name for it or the name of the Struct and then I can add the code to it that will make the Struct function. So that's a snippet. Now, those are out there and let me show you one more. I will insert a snippet and if I go into Other, Others Workflow. Actually I don't want to get into that. Let me go into Office Development. For XL, for Controls, I want to Hide or Show a Worksheet. Notice there's the statement that will do that and I have to put the worksheet in and I'll have to set something out here and notice if I just mouse over, it will tell me what I have to put in there and it'll highlight it in green for me. You OK? So I'll simply undo that again. So that's just adding snippets. Now, let me show you something else. Remember our message box down here in our code. I want an If Statement around this thing so what I'm going to do is highlight the code that I want to be inside the If Statement, then right click that highlighted code, choose Surround With and come down and choose If and double click and notice now this is going to run if this condition is true and I'll put, have to put in here, notice if I mouse over it's telling me the expression to evaluate. So if, you know, X equals three, then the message box will run. OK? And then I can just undelete if I don't like that. So it's very easy to do some pretty sophisticated stuff here and it will help you because a lot of time it's hard to remember those things. Now, there's one bit of functionality I just want to tell you about and you can go dig a little deeper because again, I don't want to go too far as a Visual Studio class here because this is C# after all. But if I go to Tools and just left click on Tools and come down, you will see Code Snippets Manager. If I go into the Code Snippets Manager, you will notice that in the Visual C Sharp, notice in the language Visual C#, I can click Add and I can tell it where to add a snippets directory. The only issue here is to build your own and to put these in, you're going to have to write a new code snippet that uses an XML file that defines that. You can just go out to the documentation on Visual Studio and see how to do that. So just go out and chase that down on the Internet or in the documentation but you can very easily put your own code snippets in here. So if there's code you like, some people like to format their comments a certain way, you can easily do that with snippets. So very easy to do thing. Make sure that you make snippets your friend, OK? They are your programming friend; how about that? So that's Code Snippets and I want you to be aware of those out there. They can really help you, especially for you guys who have a lot more responsibilities than just coding all day and you ladies as well. So you can just go out there and kind of grab some code and we'll get you started really quickly. So that's Code Snippets.

Tutorial Information

Course: Introduction to Microsoft C# 2008
Author: Mark Long
SKU: 34046
ISBN: 1-935320-78-5
Release Date: 2009-10-09
Duration: 7 hrs / 76 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available