Tuesday, November 7, 2006

Foreach loop in MSBuild

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/foreach_loop_in_msbuild.htm]

Yesterday we mentioned how MSBuild can handle conditional logic. It can also handle looping using the concept of "batching" and ItemGroups.

<Project    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">        <ItemGroup>        <ExampColl Include="Item">            <Number>123Number>        ExampColl>        <ExampColl Include="Item">            <Number>456Number>        ExampColl>        <ExampColl Include="Item">            <Number>789Number>        ExampColl>    ItemGroup>    <Target Name="ShowMessage">        <Message Text = "Number: %(ExampColl.Number)"/>    Target>Project>

The first step is to make an ItemGroup - which is simply a group of items that you can potentially loop through. Each ItemGroup needs the required "Include" attribute. For a basic example, lets just set this to "Item". The power comes in that you can add your own custom attributes to the ItemGroup, such as "Number".

You can then reference (and loop through) the ItemGroup using "%(.)", such as "%(ExampleCol1.Number)". When run, the above snippet produces the following output. Notice the Message task being called three times - once for each item in the ItemGroup.

Target ShowMessage:
    Number: 123
    Number: 456
    Number: 789


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.01

There's a lot more that you can do with Task Batching, but this is a helpful start. You can replace the Message task with any task you want, and use the '%' character in any attribute of that task. It's a lot cleaner than the DOS for loop.

Monday, November 6, 2006

Conditional Logic (If-Then-Else) in MSBuild

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/conditional_logic_ifthenelse_in_msbuild.htm]

MSBuild provides the ability to do an if-then-else control structure, using the Condition attribute, which is available to every task. Simply put, the task only runs if the given condition is true.

An If-Then-Else means "if the condition is true, run the first statement, else run the second statement". This is logically equivalent to: If 'X' is true, run S1; If 'X' is false, then run S2. We can use the task's Condition attribute, with the ! ('Not') Operator to achieve this. For example, in the snippet below, if the variable "RunTask" is true, then the first line is run, else the second line is run.

    <Message Condition="$(RunTask)" Text="Do X" />
    <Message Condition="!$(RunTask)" Text="Do not do X" />

You can see a list of allowed conditional expressions in the MSBuild reference. It includes operators ( <, >, <=, >=, ==, !=, !, And, Or), parenthesis, and even an "Exists" function to see if a file exists.

Friday, November 3, 2006

The Benefits of Reinventing the Wheel

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/the_benefits_of_reinventing_the_wheel.htm]

When someone else already makes a good, working tool, you may not want to reinvent the wheel and build a similar thing yourself. Of course we want to reuse code and not waste time repeating someone else's work. However, as an extracurricular activity, there can be benefits to creating your own version of an interesting tool:

  • Great learning opportunity - You'll understand something better when you build it yourself. You can then also compare your solution with the industry standard, and check for general trends to possibly improve your programming (i.e. "I did it this way, but they did it that way... ah... that's why they did it that way.")
  • A sense of personal accomplishment - you may a fun tool. That someone else already made a similar tool doesn't diminish your own adventure.
  • An appreciation for the tool when you see how hard it was to write.
  • The possibility that you can write it better.
  • You can customize it to your unique needs.
  • If the tool isn't free, you can now have your own copy that you don't need to worry about licensing for.
  • It's fun to write cool tools - even if someone else already has. Especially when you're a younger developer, you need to start somewhere - you can't always write flashy new things that no one has seen before.

Thursday, November 2, 2006

Taking notes with Microsoft OneNote

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/taking_notes_with_microsoft_onenote.htm]

In our profession, there is a constant need to take quick notes. Ideally, there'd by a way that is (1) quick to load, (2) flexible, (3) searchable, (4) different information formats (pictures, outlines, tables, paragraphs), and (5) low maintenance. I've played around with several techniques:

  • Using Notepad files on my desktop
    • PRO: very quick to load and make edits
    • CON: very little formatting (outlines & tables), no image support, not scalable
  • MS Word
    • PRO: Lots of formatting ability
    • CON: To bulky, html is bad
  • FrontPage
    • PRO: Lightweight, friendly HTML that can easily be used on the web, easy hyperlink to other pages
    • CON: Each resource (like an image) is its own external object - not embedded as a single file.
  • Visio
    • PRO: Decent for formal images
    • CON: Designed mainly for diagrams and modeling, not for note taking
  • Wikis
    • PRO: Optimized for easy entry and search-ability
    • CON: Entered with plain-text formatting limits your options
  • Custom Tools - I've looked at SourceForge's Freemind, and tried to build some things myself

Also, all of these are funneled to a specific purpose designed to make formal documents, not informal note taking. Enter Microsoft OneNote - a desktop app designed explicty for note taking. Some benefits:

  • Quick start up (it can run in the tool tray)
  • Draw with a pen to scribble any diagram
  • Automatic outlines
  • Can just copy in images
  • Auto-saves everything
  • Can just start writing anywhere on the page.

It looks like it has a lot of potential, and provides a decively different niche than other office products (Word, FrontPage, Visio, or Excel) that may be used for the same task. I'll curious to check it out more.

Wednesday, November 1, 2006

SQL Tip: Concat entire column into a single CSV string

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/sql_tip_concat_entire_column_into_a_single_csv_string.htm]

Sometimes in SQL, you'll want to concat an entire column's values into a single string.

For example, say you have a parent-child relationship (like state-city, or entity-code), and in a parent's list page, you want to display a column with the CSV string of all the children.

State City
IL Chicago
IL Springfield
IL Rockford
WI Madison

You can concatenate a list of values into a CSV string like "Chicago, Springfield, Rockford" by continually selecting into the same variable:

        declare @sCsv varchar(1000)
        set @sCsv = ''

        select @sCsv = @sCsv + City + ', '
        from MyTable
        where State = 'IL'; --any filter clause here

        --remove final ","
        if (Len(@sCsv) > 1)
                select @sCsv = substring(@sCsv,1,len(@sCsv)-1)
        return @sCsv

Tuesday, October 31, 2006

Regular Expressions: "Does not start with...."

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/regular_expressions_does_not_start_with.htm]

 

A coworker recently asked me how to have a regular expression check if something does not start with a certain value. For example, how to tell if an input value does not start with 'abc'.

Note that it's very easy to see if something does start with a certain value, just use the anchor-to-first-start char '^'. For example, this regex will check for all strings that start with 'abc' (followed by any \w character(s)):

    ^abc\w+

To see that something does not start with a given value, use the Grouping Construct 'Zero-width negative lookahead assertion":

    ^(?!abc)\w+

This would handle the following cases:

Pass - none of these start with 'abc': Fail - all of these start with 'abc':
defg
ab
xyz
11999
abc
abcdef

Note that there are four similar grouping constructs based on the combos of Positive/Negative - Lookahead/Lookbehind

  • Positive Lookahead
  • Negative Lookahead
  • Positive Lookbehind
  • Negative Lookbehind

You can download a free regex editor from MVP Roy Osherove

Sunday, October 29, 2006

A Dozen Extracurricular Activities to be a Better Developer

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/a_dozen_extracurricular_activities_to_be_a_better_developer.htm]

 

I talk to a lot of developers, see a lot of resumes, and give a fair deal of interviews. One of the biggest gaps I see is a lack of any extracurricular activities - i.e. work-related tasks not required by your current job. Empirically, I notice a direct relationship between a dev's technical confidence and the quality of their extra curriculars. Here are some ideas of good extra curriculars that won't just make your resume stick out, they'll make you a fundamentally better developer.

  1. Write a technical article (such as for www.codeProject.com, or any website in the Codezone community).
  2. Start your own blog
  3. Give an internal presentation to your company
  4. Mentor newer developers
  5. Help out with your companies technical interviews
  6. Get a certification
  7. Get a patent (obviously not feasible with most companies, but the larger ones may support it)
  8. Start an open-source project
  9. Attend a user group (for example, if you're in Chicago, consider attending the Chicago .Net User's Group)
  10. Participate in online forums, answering others questions (which goes great with having your own blog and writing articles)
  11. Make your own personal website
  12. Help your non-technical friends set up their own web sites.

No matter where you are, there's likely at least some of these you could do in your current job.