If you want to see a bunch of nerds get into a debate, just get 2 or 3 in a room and ask them about Comments and when should you Comment? How much should you Comment? Now everybody agrees that there should be Commenting in code but nobody agrees it seems on exactly how much Commenting and when to Comment. But what I want to show you in this video is how we do Comments in C#. This is pretty straightforward, it's not a huge departure from, from anybody else out there in the development world for the most part but what I'm going to do is, drag a button onto a form. Notice I did a little different way that time and, and let me delete this and go back and show you. I clicked on the button, came over here and notice I've got this little Plus sign and the little A B. Now I can drag and build the button, what size I want and when I drop it. So if you ever want a button that large, that's where you can do it or you can just drop it on there and then resize it with the handles. Right. But anyway, we'll leave our button like this and just double-click it. Now what I want to do is, when I click on this button, I'm going to create an Integer. I'll do an Integer A and I'll set it equal to 5 and then I will do a Message Box.show that says you know, Variable created and I find, don't know why I would do something this useless. But hey, I've written a lot of useless code in my time. Okay. Now notice it's giving me a little squiggly here that says that Variable A is assigned but never used. And that's okay, we'll live with that for now but, so I've written some useless code here that looks useless. But why would I want this Message Box here? I can put some Comments in here and the easy way to do this is, 2 forward slashes and notice it's turned some things green. This Message Box is totally useless and notice this is going to set everything on this line. Now something else that you can do and I'll take it away here and you can put it at the end of a line out here. Okay. And notice, this line will still execute but I have a Comment at the end. So the 2 forward slashes will put a Comment just on that line, create an Assigned Variable. Now if I want to Comment a bunch of things and this is kind of a trick a lot of people use qnd that is I want to Comment both of these lines out. Well I could come right here and put the 2 slashes on the front and that just Commented those 2 lines out. Then I have to go manually, take these things back off again alright, to undo them. I can just highlight both of these lines, let me get this one off the end down here, so I don't confuse you anymore than I probably already am. Notice if I highlight the 2 lines and I come up here on the little Toolbar and if you don't see this Toolbar, you can go up here to View Toolbars and you can see there's Standard and Text Editor. I'm working with Text Editor here but highlight these 2 lines, come up here and just click Comment. It will put 2 slashes in front of every one of those lines that I have highlighted. Then when I'm done with it, I can come highlight these lines again, click this one and Uncomment. Now this little trick with these two buttons, it's amazing how many people either forget about these or don't realize they're up there. It's a neat little trick, I noticed that if I want to Comment out a bunch of stuff, I can highlight everything and it just Commented out everything or I can undo it. Now what's interesting is, I can just keep doing this and then I have to keep undoing it to get it back. Okay. But that's one way. Then if I want to Comment a, a whole section out and not use the Toolbars, I can use the forward slash and an asterisk and notice everything after this is green. So it is Commented from here, all the way down to here. The way I stop this Commenting is, I can come right here and I can put the asterisk and the forward slash. And notice now everything between these, notice I started it with a forward slash asterisk, I close it with an asterisk forward slash and this one is now Uncommented. So I can use the asterisk forward slash, I'll delete it here. I'll grab this one, I drag it up here and notice everything is green. I'll come down here and put it and notice it's Commented everything from here to here and now this is open. So you got a lot of options here with Comments. Use Comments, first of all, to document anything in your code that's going to be hard to figure out when you come back, a week, 2 weeks, 2 months, 6 months later and try to make changes to this code, and update it, enhance it whatever but also for other developers. Now there's always this debate about how many Comments? How much should I write? To what level of detail? And trust me on this, you'll never satisfy other developers. Write yourself enough in Comments so that you can understand what's going on, so that they can understand what's going on. And just keep in mind, you're always going to hear with your Comments, this person doesn't believe in Comments or the ones they wrote were too cryptic, I couldn't understand them. Or they wrote their autobiography in Comments and I don't understand that. So you'll always hear one or the other but I would fail on the side of writing too many Comments maybe making them a little too detailed. Because it's amazing how many times you come back to some code a few months later and you're thinking, was I on some real like, really strong sinus medicine that morning, because none of this code seems to be making any sense. Why would I write it that way? As it always usually is in life, there are reasons for doing what you're doing; they just don't make sense later. Okay. So anyway that's a quick look at Comments in C#.
| Course: | Microsoft C# 2010 |
| Author: | Mark Long |
| SKU: | 34306 |
| ISBN: | 978-1-61866-037-4 |
| Release Date: | 2012-03-19 |
| Duration: | 8.5 hrs / 105 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |