In this video, I want to show you an example of a real powerful feature in C# called Refactoring Code. Now again this is a functionality of the Visual Studio environment, but it's a great tool you need to know about. Let's take a look at something here where, let me, let me do something here, just don't want to confuse you, but, anywhere in here that you've got some code and let me you show how I got where I'm at. I've just got a form and I dropped a button on it and I double-clicked and I actually had already written some code here. But in my Button 1 Click Event a couple of things I want to show you about Refactoring. Okay. I can Refactor or change aspects of my code automatically. What I'm going to do is, jump down here, outside the Namespace, notice where I'm at here and I'm going to create a class. I'm going to call it Public Class Mark and then I will just put a little Public Void Add This, put a little method in here. Okay. And then we would later add some code here. Alright. Well notice when I come up here, I can create an instance of Mark. If you don't know what I'm doing, don't worry, I just want to show you something kind of quick here and set that equal to, not a new instance, but a new instance of Mark and then notice when I hit M and a period. I can see my Add This Method down here and so I will tell it to call that and that's pretty straightforward. Alright. Not a lot's going on here. There's absolutely no functionality here, I'm real good at writing useless code. So what I'm going to do now, is show you how I can Refactor. Notice if I get on the Class Name Mark and right-click, I can choose Refactor and notice I can rename this. And let's say, instead of Mark, I want to call it, VTC Mark and notice I can preview my changes, I can search in Comments, search in Strings. I'll just say OK. There's a preview of the changes that are going to happen, this is what it's going to look like here and I just apply it. Now notice everywhere that it was Mark, it has been changed to VTC Mark. A really cool way to make changes to your code. It will get all of them for you, most of the time a lot of neat stuff can happen there, well let's look at something else. Within my class here, in Mark, I start to write some things inside my Add This Method. Okay. And what I do is, I create a String called A and I set it equal to Hello World. Right. And then I create a Message Box and I call the Show Method and I'm going to print my string out. Then I realize, hey you know what? That really should be a method. Okay. That shouldn't be inside here. I'm going to be calling this Hello World a lot and doing a Message Box, don't ask me why. Okay. And so what I can do, is highlight the code, right-click, go to Refactor and say Extract Method. Left-click on that, what do you want to call this? Well I'm going to call this the Useless Message Box Method. Okay. And I hit OK and notice it created a Private Useless Message Box Method for me and in place of my code where it was, it put the Useless Message Box call here. Alright. So that's Refactoring, a couple of things you can do very easily here. You will find yourself all the time, within one method, writing 8, 10, 12 lines of code, thinking, man I might have to call this some other places. This really should be a method on it's own. Fine, just highlight it all, right-click, Refactor, make it a method or Extract a Method. So anyway that's Refactoring so keep that out there in your little developer toolbox in your brain and pull it out when you need it, a lot of cool functionality there.
| 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 |