Category Archives: Programming

Regex and LINQPad

Recently, I was working with a customer who needed dynamic worksheet names in their Excel workbooks.  With ExcelWriter this is no problem!  Since the input was coming from the end users, I need to perform some validation on the input to make sure it conforms to Excel’s restrictions on what a valid worksheet name can be.

Valid Worksheet Names in Excel

  1. They cannot exceed 31 characters.
  2. They cannot contain the following characters: \ / ? * [ ]
  3. The name cannot be the same as another Worksheet in the Workbook.
  4. The worksheet name cannot be blank

In this case I am interested in #2 and dealing with invalid characters.  I know that Regex is the best option for this–when I’m working with Regex, I often find testing to be a pain. I‘ll open Visual Studio, create a console application and write some code to test my patterns.  Just when I was starting to get fed up with all these random testing console applications in my projects directory, I remembered an application called LINQPad that my co-work Sean told me about. LINQPad is a standalone C#/VB/F# scratch pad that can instantly execute code. The output is displayed in a rich format.

So I came up with the following Regex Pattern: Continue reading Regex and LINQPad

Joining DataTables in LINQ

[Image via Patou Fine Art]

The Problem

Recently, I encountered a situation where I had to join two Data Tables from databases on two different servers. Had they been on the same server, I would do this in SQL with a simple JOIN statement, but since the databases were on different servers, my options were limited. I could:

  1. Use a linked server (http://msdn.microsoft.com/en-us/library/ms188279.aspx)
    But: This wasn’t really an option because I wouldn’t be able to modify production servers
  2. Use System.Data and create a DataRelation
    But: DataRelations are clunky and don’t perform LEFT OUTER JOIN’s very well
  3. Create a new DataTable and copy rows over manually (with, for example, a foreach loop)
    But: This would be painfully slow
    And: I’d have to duplicate a lot of null-row handling logic that LINQ and SQL do themselves
  4. Join the tables in the codebehind using C#’s own SQL-like syntax, LINQ.
    But: I had no idea how to use LINQ.

The Solution

After some research, I decided on option #4. To get a basic overview of LINQ, I started by going through 101 LINQ Samples and trying to learn the LINQ syntax. This was my first attempt at getting a method to join two DataTables: Continue reading Joining DataTables in LINQ

Don’t Pay for Install Tools when WiX Works

Recently we found it beneficial to rewrite one of our Windows installers. The previous one was built with the well-known InstallShield 2008, which is a pretty powerful GUI tool built over a very basic language called InstallScript. You can do pretty much anything you need with the tool, but a lot of it can be rather arcane and not at all straightforward. This is fine and dandy if a team has a dedicated installer engineer, but when the installer is rarely touched it doesn’t tend to work out as well. InstallScript itself eventually runs into using goto statements, something abhorrent to many of us. On top of that, the tool is a few grand per license. We needed to make some major changes to our installer, and knowing the state of the tool and code for the existing one we thought it better to seek out alternatives.

Why WiX? WiX actually comes out of Microsoft, and was amongst the first of their projects to be delivered open source under the Common Public License. Since it’s open source, it’s also free. Before, we had to log into the one machine that can actually make changes to the installer, as we only had the one license. Now, every developer can just have WiX installed on their box outright. Additionally, WiX comes with Votive, a Visual Studio plugin. As we’re already using Visual Studio to develop OfficeWriter, the project in question, this is simply a nice feature we get for free. One doesn’t need Visual Studio or the plugin to develop a WiX installer, however. Continue reading Don’t Pay for Install Tools when WiX Works

Spamming Made Easy in ASP.NET

Receiving an auto-generated email is often unpleasant, but when deployed correctly, they can be useful for both customers and companies. C# and SQL make it easy to grab information from a database and send customized emails with little effort.

Step one: grab the data.

Before sending emails, information from the database, like email addresses, needs to be collected by a query. A SQL query is just a string that asks a database for a specific table, but users should be able to dynamically change it depending on their needs.  As such, certain lines need to be flexible.  In the past, I had used string formatting to insert text into the query, which looks like the following:

string text = "I ilke {0} more than {1}, honestly";
text = string.Format(text,"dogs",cats");

Robert Morris, Encryption and the Pony Express

[Image via NexGadet.com]

Robert Morris Sr. died June 26, 2011. I had the honor of meeting Mr. Morris in 1996 at a Dartmouth Workshop, where he gave an interesting and dynamic talk on transportable agents. He then took questions from the attendees. In my ignorance, I asked him:

“Aren’t you worried that the vast majority of common encryption schemes (SSL, RSA, etc) are based on prime factors of large numbers? If someone found an algorithm for this wouldn’t all common encryption be useless?”

Little did I realize that speculation has been rife for a long time that the NSA had in fact solved this long ago, either by math or vast amounts of hardware or a combination of both. As the former Chief Scientist of the NSA, Morris had obviously been asked the question before by people much smarter and with much more at stake than me. He could have easily brushed it off. Instead, with the infinite patience of parent, he carefully chose his answer. Continue reading Robert Morris, Encryption and the Pony Express

Simple powershell automated FTP upload script

[This post was originally posted on my personal blog, http://simplystallings.com]

At work, we manually patch our web servers. Part of the process requires ftping the updates to each server. This process is tedious considering the number of web servers and number of steps required. I decided this past patch Tuesday that I would invest a little time into automating the process using powershell.

I located a simple ftp upload script located @ http://poshcode.org/1134. With a few modifications I had my script:

$file = "somefilename"
$filePath = "C:\" + $file
for ($i=0;$i-lt$servers.length;$i++)
{
"ftp url: $ftp"
#FTP URL syntax
$ftp = $servers[$i]+$file
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Uploading $File..."
$webclient.UploadFile($uri, $filePath)
}

I added an array that contains the ftp URL syntax for each web server then I iterate over the array using a for loop. Continue reading Simple powershell automated FTP upload script

A loss of precision, but not a loss of magnitude

Those of us working with relatively high-level languages spend most of our time able to blissfully ignore the mucky little details that go into making a program actually run. C# allows us to not think about allocating and freeing memory, for example, or how our strings are terminated. It’s all too easy to extend this willful blindness to other details, like how numeric types actually work. Can this variable ever have a fractional part? Yes? Double! No? Int! Can it be really big? Long! Done, sorted.

Recently I was writing some functions to do unit conversions. Specifically, I was converting between points (1/72 of an inch) and EMUs (1/914400 of an inch). Part of the point of EMUs is that you can use them for high-precision values without needing floating point arithmetic, so I was storing them in a long. The application in question deals with fractional points, so those went into a double. Now, what’s the largest positive point that we can convert into an EMU without any overflow? If we do a little math we see that there are 12,700 EMUs per point, so the obvious answer should be long.MaxValue / 12700.0, which comes out to a bit over 7.2E14. Excellent. However, if I turn that around and convert that many points back into EMUs, I get -9.2E18. Oh dear. Continue reading A loss of precision, but not a loss of magnitude

Scrum Debates: Research as a Task

The concept of scrum can seem very simple in theory. As stories come in, the team analyzes them, and then story points them based on the complexity of the story. It couldn’t be simpler. But in practice, it is rarely this simple. The problem is that sometimes you won’t know just how complex a story is until after you’ve played around with it a bit. At my company, this is a problem we face almost every time we meet to story point backlog items.

Usually, a bit of uncertainty is fine. That’s the entire reason teams usually story point with a non-linear scale (one the popular scales being: 1,2,3,5,8,13,20,40,100). If a story could be anywhere from a 30 to a 40, then just give it a 40 (assuming we’re using the popular scale). Even if the story ends up closer the 30, there’s probably some other story that was more complex than its story point implied and it all generally averages out. But every now and then, there’s just too much uncertainty. A story might be anywhere from a 5 to a 50 depending on factors you won’t know until you’ve looked into it a bit and done some research. Continue reading Scrum Debates: Research as a Task

Introduction to Rhino Mocks

[image via Brian Schroer]
Because mocking objects is awesome, here is a quick introduction to Rhino Mocks so that we can use it in any C# code.

What is a mock object?

The mock object is a very light weight object where you can simply define what to return when a given method is called with a given parameter. You may at some point want to write a unit test on an object that depends on another class. To avoid this dependecy, you mock out that class and replace it with a mock object. So, for example, if I’m testing an aggregate class:

// combines a list of numbers using a given operator.
// e.g. if your operator is addition, and you pass 2,3,4 to the aggregate method,
// it will return 2+3+4=9
class Aggregator
{
    Aggregator(IOperation op) {...}
    double Aggregate(out string text, params double[] numbers) {...}
}
// an operator such as addition or subtraction or a random number generator.
interface IOperator
{
    string DisplayText {get; set;}
    double Operate(double a, double b);
}

Mocks give you the freedom from depending on an operator to perform correctly in order to have your unit tests for the aggregator class to pass. Continue reading Introduction to Rhino Mocks