var – Considered harmful
There seems to be a new phenomenon in the c#.net 3.5 world, where people are using the “var” keyword, because, well I’m guessing they are lazy. Take this example, what is MORE READABLE:
var sourceRect = new ComparisonRectangleContainer();
OR
ComparisonRectangleContainer sourceRect = new ComparisonRectangleContainer();
Now, I know that will be straight forward, you can see what the new type is, it will be a ComparisonRectangleContainer, but what happens when you use var when getting a response from a method, like the following:
var parallelCombiner = FirstPass();
Now, what does FirstPass return? Is it an IList<String>? No, I have to mouseover to find out:
Ok, so it turns out it returns an ImageParallelCombiner, the first bit of code does not make it clear.
You should always write code for HUMANS, not the compiler
The following is PERFECTLY valid c#.net code, but is it good code?
var @this = new @class();
@base.@stackalloc(delegate(@if @event)
{
if (!@return.@bool(@event)) return;
@is.@const(“S[" +@event + "] matched T[" + @return.@override(@event) + "]“ );
@this.@implicit(@event);
});
Note – you can prefix variable names with @ if you want to use a keyword – i.e. @class is a valid variable name, so is _ (underscore) – perfect if you want to make things as hard as possible for people to read it, like the “var” keyword.
LAB: Image Detection, Part 2
Warning – this post will be heavy on images, so if you are on a low bandwidth connection, well sucks to be you then.
With a few minor improvements to the code, here we have the Source image:
![]()
Here is the target image’s blobs:
![]()
As you can see, it has found both blobs, and here is the raw output for the blob data:
![]()
They are very similar, only a few pixels out! Now to compare the images somehow…
Code Snippets – Text on images
Two code snippets for you:
private void drawTextWithBackground (string Text, Font font, Graphics grpaphics, Brush backgroundBrush, Brush foregroundBrush, int x, int y)
{
SizeF size = grpaphics.MeasureString(Text, font);
grpaphics.FillRectangle(backgroundBrush, x,y,size.Width, size.Height);
grpaphics.DrawString(Text,font,foregroundBrush,x+1,y+1);
}
private void drawTextAtBottom (String Text, Font font, Graphics graphics, Image sourceImage, Brush backgroundBrush, Brush foregroundBrush)
{
SizeF size = graphics.MeasureString(Text, font);
int y = (int) (sourceImage.Height – size.Height);
drawTextWithBackground(Text,font,graphics,backgroundBrush,foregroundBrush,1,y);
}
Does exactly what it says on the tin!
LAB: Image Detection, Part 1
Every now and then, I get a crazy idea, to try something rather hard in .net – maybe hard isnt the right word for it, but something that hasn’t really been attempted before, or if it has, nothing public about it. So the idea I have is for some form of image detection, to say that Image X is x % similar to Image Y. I prefer to use the terms Source and Target, but it doesn’t really matter.
The source and the target images are here. That’s a photo I took a few weeks ago, if you do decide to steal it, please put a message on there that points back to me
Anyway, the target image is 14×15 pixels smaller – that’s a whopping 210 pixels different! It shouldn’t be too hard to match up the two images, or should it?
Histograms
My first attempt was with Histograms. I grabbed some open source (unsafe!) code that generates an array of int[] and lists the histogram values, so I shoved both images through that, and got it to output the Source’s histogram value, the Target’s histogram value, the difference between them both, and the percentage of how similar it is, like so: (The first number is the key# of the int in the array, just because)
![]()
Right at the bottom, I had an average of all the percentages, to see how “similar” the image is. I was expecting the percentage to be fairly high, since there is only a few hundred pixels difference, and I didn’t change any of the levels or colours when I cut the images out of each other, but it told me there was a 87.7% similarity! That was very shockingly low. If you see something wrong with my maths from the code below, please let me know:
public HistogramCompareResults(int key, int source, int target)
{
this.key = key;
this.source = source;
this.target = target;
difference = source – target;
if (difference < 0)
{
difference = difference*-1;
}
if (source != 0 && target !=0)
{
if (source > target)
{
percentage = (double)target / (double)source;
}
else
{
percentage = (double)source / (double)target;
}
percentage = percentage*100;
}
}
Image Processing
My next port of call was basically image processing, like what I did with my ANPR project that I created – basically filtering stuff out and building a “thumbprint” of the image, that hopefully will withstand being resized and stuff like that. Using image filters, flattening images and looking for large “blobs” of images, sofar I have come up with this:
![]()
That is the current “thumbprint” for the red channel, on the source image.
An introduction to Pex
Pex generates Unit Tests from Parameterized Unit Tests through Automated White box Testing based on Input Generation
Ill be the first to admit, I have never been a big fan of unit testing. To me, it seems like a big waste of time. I’m not sure why, but I just don’t like it – I suppose it has something to-do with spending time writing tests instead of actually doing proper, paid work. Yes, yes, I know that tests “prove” that your code works, as long as you have coded your tests right. But what if you haven’t?
Anyway, I thought id give Pex a test since it generates unit tests for me. I love things that generate things for me, because I like to do things as quickly as possible, and the computer is millions of times faster than me at generating stuff.
For starters – head over to the Pex website, and download it – I downloaded the academic version, since im running VS.NET 2008 pro. Installing it after downloading it also might help matters.
Once you have installed it, you should be able to just open up a project and when right clicking inside a .cs file, you should see a few menu items:

Once you click on that little baby, Pex should start whizzing into action. Be warned, Pex will only test Public classes and methods – it dosent like testing anything else that isnt public, and I believe it will give you a warning.
In VS.NET’s status bar, you will get a bunch of messages, along the lines of “Pex: listening to monitored process (cold start)” and then “Pex: Finished”. It should also pop up a window, something vaguely like the following:

I know this looks confusing at first, but it really isn’t. Its quite simple really. You have various options, but what’s interesting (I think) is the grid view in the middle. The first icon says if the test passed or failed – green = pass, red = ? (Take a guess, gwan, take a guess!) The number denotes the number of the test (its incremental), and the name is the value it has tried If the test fails and throws an exception, that will be listed under the Summary and Error message bits.
If you click on the test, you can see the following to the side :

The details is the actual code for the unit test that it has executed. I know, it says “this.” instead of the class name, and that’s because Pex creates a copy of the code to run tests on, in a partial class. Under the stack trace subtab, it well, gives you the stack trace (Please tell me you knew what that would do. Please.)
This is where Pex becomes rather … brilliant – it allows you to save all of these generated tests into its own project, simply by selecting the tests you want to save, and hitting the fancy “Save Test…” button – it will go off, and generate its own project (It will ask you for the name of the project, where it should live etc, I will provide a screenshot later) – and it will save the tests to that project! Tests that you can run later, and you don’t have to use vs.net’s test suite – you can decide to plug in different testing libraries such as MBUnit (my current fav) by downloading extensions that are available on CodePlex, or I believe you can create your own.
More on Pex later!
OnItemCommand for Nettiers 2.3
Well the good news is I have submitted my patch to include the OnItemCommand for the TableRepeater in Nettiers 2.3 – You can either download the file from this link, or directly from here (it lives in WebLibrary\UI)
Missing page pointer: NOAVATAR
If you get the following error with YAF:

While using a custom language pack, you probably have tried visiting the forum, and you probably have read this following, brilliant, useful post:

And guess what happens when you click on that link?
HURRAH! Oh hangon, no thats not good. Anyway, the solution is to put this :
<page name=”CP_EDITAVATAR”>
<Resource tag=”TITLE”>Modify Avatar</Resource>
<Resource tag=”AVATAR”>Avatar</Resource>
<Resource tag=”AVATARCURRENT”>Current Avatar</Resource>
<Resource tag=”AVATARNEW”>Choose a New Avatar</Resource>
<Resource tag=”AVATARUPLOAD”>Upload Avatar from Your Computer:</Resource>
<Resource tag=”AVATARDELETE”>Clear Avatar</Resource>
<Resource tag=”AVATARREMOTE”>Enter URL of Avatar on Remote Server to Use:</Resource>
<Resource tag=”OURAVATAR”>Select your Avatar from our Collection:</Resource>
<Resource tag=”OURAVATAR_SELECT”>Click Here to Select an Avatar</Resource>
<Resource tag=”AVATARTEXT”>Avatar Explaination Text</Resource>
<Resource tag=”NOAVATAR”>[ No Avatar ]</Resource>
<Resource tag=”WARN_TOOBIG”>Image size can’t be larger than {0}x{1} pixels.</Resource>
<Resource tag=”WARN_SIZE”>The size of your image was {0}x{1} pixels.</Resource>
<Resource tag=”WARN_RESIZED”>The image was resized to fit.</Resource>
<Resource tag=”WARN_BIGFILE”>The size of your image can’t be more than {0} bytes.</Resource>
<Resource tag=”WARN_FILESIZE”>The size of your image was {0} bytes.</Resource>
</page>
Into your chosen language xml file, and it should work.
My current wordpress setup
I have the following plugins installed for WordPress:
- Akismet
- Faster Image Insert
- Google Analyticator
- iMax Width
- Scissors
- Simple Tagging
- Simply Exclude – I use this so I can exclude the “links” category from the mainpage, and the rss feed
- Sociable Zyblog Edition
- SyntaxHighlighter Plus
- TinyMCE Advanced
- WassUp – ultimate live web stats
- WP Easy Uploader – this alows me to upload files and themes, without downloading them locally, connecting to the server via secure ftp and uploading the files manually – can give it a URL, and it will download and extract.
If you have any more decent wordpress plugins, let me know!
Just a reminder – DropDownList in ASP.NET
DropDownList.Items.FindByValue(string) is case sensitive. Just a reminder.
Null Object Pattern – Pattern or Antipattern
The essence of polymorphism is that instead of asking an object what type it is and then invoking some behavior based on the answer, you just invoke the behavior. The object, depending on its type, does the right thing.
from http://sourcemaking.com/refactoring/introduce-null-object
The basis for this pattern is that instead of checking to see if null objects, you create a prototype object, with a “IsNull” property, and instead of checking to see if its null, you would check this property.
To be really honest, im not sure why you would do things this way. This seems like a really convoluted way of basically doing nothing at all. The code samples show that instead of doing object == null, you do object.IsNull. What is the point of that?
This method of doing this is counter intuitive. If the database cannot find the user, why should it return an empty object and set a property to say that it SHOULD be null? If it SHOULD be null, why not set it to null?
In my opinion, this is an antipattern, not a pattern.