Mark Miller

March 2010 - Posts

  • Spend a Day with Miller in Vegas! (Free CodeRush Training)

    Hey kids,

    Ever wish you could spend the day with your good old buddy Mark Miller in a Las Vegas training room?

     MillerHangingOut

    Just sitting back, shooting the breeze and talking about whatever CodeRush thing is on your mind? Well the good folks at DevExpress are working hard to make your dream come true. :-)

    That’s right, kids. DevExpress is putting together a full day’s worth of formal CodeRush training, tentatively scheduled for Thursday the 15th of April, the day after the DevConnections conference and the official launch of Visual Studio 2010 in Las Vegas. And like the title of this blog post reads, this training session will be 100% free.

    Not only will it be free, but DevExpress will also be giving away prizes, we’ll hear from a special guest speaker from the Visual Studio team at Microsoft, and there’ll be some yummy food to eat. All courtesy of DevExpress. It’s crazy I tell you. Absolutely crazy. :-)

    As for content, we’ll start off the morning talking about CodeRush Xpress, then we’ll move into the more powerful features of the full version of CodeRush, wrapping up with extensibility, showing how to radically customize your IDE. The course content will look something like this:

    • CodeRush Xpress
      • Navigation
      • Selection
      • Clipboard Features
      • Declaration from Usage
      • Refactoring
      • Performance/Memory Comparison
    • CodeRush
      • Selection Embedding
      • Other Productivity Features
        • Intellassist
        • Auto Declare
        • Smart Semi-Colon
        • Smart Parens
      • Templates
      • Code Issues
      • Test Runner
      • More Code Providers
      • More Clipboard Features
      • More Refactorings
      • Other Language Features
        • ASP.NET
        • XAML
        • JavaScript
        • etc.
      • Extensibility
        • Third Party Plug-ins
        • Introduction – Creating a Simple Plug-in
        • Creating Refactorings
        • Creating Code Providers
        • Creating Code Issues

    So what’s the catch, you ask? It’s simple. We need to get a sense of how many people will show up, and we need to know very soon so we can book a room big enough to hold everyone. If this event sounds like your kind of fun and you can make it to Vegas in April, post a comment below to let us know of your intentions to be there. I expect that soon we’ll create a registration page so you can lock in your spot, and when that’s page is available we’ll let you know through blogs, tweeting, shouting and otherwise general mayhem.

  • Slicing and Dicing Strings with CodeRush for Visual Studio

    CodeRush brings powerful string manipulation features to Visual Studio. Here are a few of my favorites:

    Splitting Strings

    When you need to break a string literal into two or three pieces. For example, let’s start with the following text:

     

    public string GetGreetingText(int daysLeft)
    {
      return "Welcome back, customer. \nSo glad to see you. Days remaining on our special offer: " + daysLeft.ToString();
    }

    I can place the caret (the flashing I-beam that marks the editor’s insertion point) right before “Days remaining” text and press the CodeRush/Refactor! key (Ctrl+` by default) and then choose Split String.

    SplitStringInto2

    This will split the string into two pieces.

    Next, let’s select the last two expressions on this line:

    SelectLastExpression

    Press the CodeRush/Refactor! key, and choose Extract Method to create a new method called GetDaysRemaining, like this:

    GetDaysRemaining

    Use String.Format

    Next, move the caret down inside the string inside the GetDaysRemaining method. Press the CodeRush/Refactor! key and select Use String.Format.

    UseStringFormat

    This neatly combines the concatenation into a single string that is easy to read and so much easier to translate (translating string fragments is much harder to do when grammatical rearrangements are needed as part of the localization).

    Speaking of translation…

    Extract String to Resource

    Move the caret inside the “Days remaining…” string. Press the CodeRush/Refactor! key and select Extract String to Resource, then select “Create new resource file”.

    ExtractStringToResource

    CodeRush will create a new resource file to hold this string, and highlight the resource identifier for an easy rename (tip: I like to use Camel Case Nav and Camel Case Select – Alt plus Left or Right arrow keys with Shift to select -- to quickly trim a long identifier down to a reasonable size). Let’s call this resource “DaysRemaining”.

    DaysRemainingResource

    Placing strings in resource files is a good first step to preparing your code for translation.

    Splitting Strings, Revisited

    The CodeRush Split String refactoring has a bonus feature built in: the ability to split a string into three pieces. Just select the portion of the string that you want to pull out and apply the refactoring. For example, in the code above, there is a “\n” appearing inside the string. This “\n” may be confusing to translators, it makes the string harder to read, and it may not be the most appropriate way to terminate lines on some platforms, so let’s take it out and replace it with the Environment.NewLine expression.

    First, select the “\n”:

    SelectSlashN

    Press the CodeRush/Refactor! key and select Split String

    ApplySplitStringIntoThree

    Applying the refactoring will leave you with this:

    SplitStringInto2Applied

    Notice the “\n” string fragment is selected for easy editing. Now we can use CodeRush’s “enl” template to replace the “\n” with a proper Environment.NewLine expression.

    AfterEnvironmentNewLine

    Introduce Format Item

    See that “Welcome back, customer.” string in the code above? It would be great if we could actually pass in the customer’s name in place of the text “customer”. That would make the greeting certainly more personal. It’s easy to do this with CodeRush. Just select the part of the string that you want to replace, and press the CodeRush/Refactor! key, like this:

    IntroduceFormatItemPreviewHint

    Apply the Introduce Format Item refactoring to get this:

    AppliedIntroduceFormatItem

    Notice that once again the extracted string is selected for easy editing. Now let’s promote this string to a parameter. Press the CodeRush/Refactor! key and select Promote to Parameter.

    PromoteToParameter

    And give the new parameter a meaningful name, like customerName:

    PromoteToParameterAfterRename

    Converting Text to Strings

    Sometimes you need to work with a text coming from another environment, and that text span several lines or contain quote characters (that would need to be escaped). So in this example I used the “ms” template to create a method that returns a string, and I expanded the “r” template to get the return keyword and then I pasted in some XAML code that creates a TextBlock:

    private string GetTextBlock()
    {
      return <TextBlock
      Canvas.Left="25" Canvas.Top="5"
     
    Foreground="Teal" FontFamily="Verdana" FontSize="18" FontWeight="Bold"
     
    Text="Sample Output" />;
    }

    Of course this code will not compile. Fortunately we have CodeRush installed so getting it into its desired shape is easy. Just select the text we want to convert, press the CodeRush/Refactor! key and select Embed Selection | To string, like this:

    EmbedString

    CodeRush will convert the text into a string, like this:

    ConvertToString

    Nice. Notice how the quotes are properly escaped and the separate lines are now concatenated together.

    Now, suppose you wanted to parameterize some of the values in this string. For example, turn the “Sample Output” into a “text” parameter. How would you do that? Well, you should already know how. Just select the text, split the string, and promote it to a parameter.

    Wrapping Up

    If you’re not already using CodeRush, you should be. This post has covered only a portion of the tools and features that work with strings, and working with strings is only a very small tip of the very large iceberg of power that is CodeRush. CodeRush is the fastest, most powerful, and most memory-efficient developer productivity add-on available for Visual Studio.

LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners