To illustrate refactoring in (Yahoo free web hosting) action, update your Main()

To illustrate refactoring in action, update your Main() method with the following code: static void Main(string[] args) { // Set up Console UI (CUI) Console.Title = “My Rocking App”; Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine(”*************************************”); Console.WriteLine(”***** Welcome to My Rocking App *****”); Console.WriteLine(”*************************************”); Console.BackgroundColor = ConsoleColor.Black; // Wait for key press to close. Console.ReadLine(); } While there is nothing wrong with the preceding code as it now stands, imagine that you want to display this prompt at various places throughout your program. Rather than retyping the same exact console user interface logic, it would be ideal to have a helper function that could be called to do so. Given this, you will apply the Extract Method refactoring to your existing code. First, select each code statement (except the final call to Console.ReadLine()) within the editor. Now, right-click and select the Extract Method option from the Refactor context menu. Name your new method ConfigureCUI() in the resulting dialog box. When you have finished, you will find that your Main() method calls the newly generated ConfigureCUI() method, which now contains the previously selected code: class Program { static void Main(string[] args) { ConfigureCUI(); // Wait for key press to close. Console.ReadLine(); } private static void ConfigureCUI() { // Set up Console UI (CUI) Console.Title = “My Rocking App”; Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine(”*************************************”); Console.WriteLine(”***** Welcome to My Rocking App *****”); Console.WriteLine(”*************************************”); Console.BackgroundColor = ConsoleColor.Black; } } Note If you are interested in more information on the refactoring process and a detailed walk-through of each refactoring supported by Visual Studio 2005, look up my article Refactoring C# Code Using Visual Studio 2005 online at http://msdn.microsoft.com. CHAPTER 2 56 BUILDING C# APPLICATIONS
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply