Tuesday, March 03, 2009

Generating GUIDs

This may seem trivial but here we go:

I recently created an installer with WIX and obviously had to create a number of GUIDs along the way. While there is a "Create GUID" option in the Tools menu it has a number of drawbacks:

  • Clicking around a UI with a mouse is not the most efficient thing in the world
  • There is not an immediately suitable format available, registry format is closest, but braces need to be removed and the rest uppercased

The obvious? solution to this problem is to create a macro like the following:

Public Sub CreateWixGuid()
Dim g As Guid = Guid.NewGuid()
Dim textSelection As EnvDTE.TextSelection

textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
textSelection.Text = g.ToString("D").ToUpper()
End Sub


and assign a keyboard shortcut to it (I chose Ctrl+G,Ctrl+G), and voilà a GUID in the correct format is generated and inserted.



 



 

Tuesday, November 11, 2008

How to not do validation

Yesterday when I was trying to do some online shopping I the e-commerce system in use perfomed input validation in a slightly suboptimal way. Here is what happened:

  • To be able check out the stuff in my shopping basket I had to create an account.
  • To create an account I had to enter username, password and address.
  • Go to checkout
  • Confirm to use the just entered address as the shipping address
  • On the following page where the pay button should be there's this small note "The city is not long enough"  WTF!?!
  • Try again with the city name in the other language as it is longer
  • Same story "The city is not long enough" ...
  • Only when changing the primary address the order goes through

A validation rule stating that the city cannot be just three letters are just plain stupid, secondly if there is a need to use stupid validation rules, then do the validation when the data is entered and not when it is used ...

This comic comes to mind.

Saturday, November 08, 2008

Debugging WPF databinding issues

Lately due to switching teams and project I've been doing much more WPF than before. I cannot say I have fully mastered the learning curve yet but I'm getting better and it is an interesting journey.

Occasionally the data bindings does not work as expected and then it is nice to get more information about them in order to figure out what went wrong.

The following is useful when you want to know why a particular binding is misbehaving (or rather you want to know where you screwed up...)

xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"



ItemsSource="{Binding <snip>, diagnostics:PresentationTraceSources.TraceLevel=High}"


The above information and MUCH more is presented by Beatriz Stollnitz



(This post is also a 'reminder to self')

Monday, November 03, 2008

C# quiz 9/?

Consider the following:

Guid globallyUnique1 = new Guid();
Guid globallyUnique2 = new Guid();
if (globallyUnique1 == globallyUnique2)
{
MessageBox.Show("This cannot be");
}
else
{
MessageBox.Show("Everything is well");
}

What is shown? Why?

Wednesday, October 15, 2008

Sandcastle to dead tree format woes

For reasons partially unknown to me, I was tasked with converting Sandcastle generated API documentation to dead tree format.

The best way seems to be feeding some magic XSLT to Sandcastle that would that would directly create PDF via XSL-FO or DocBook or whatever. However, I haven't been able to find such a thing and I'm not ready to put down that kind of work to create it myself at this point.

Another option I pursued was to print the CHM to XPS/PDF but this has its own cons. Firstly directly printing from CHM viewer strips CSS rendering ugliness. This is correctable by finding the correct html file from %TEMP% and a little search/replace magic and providing the stylesheets and images.

Having gotten this far I found out that the stylesheet needs to use something else but relative font size to keep the text legible.

Now as if this wasn't enough the browser or at least the PDF/XPS "printer" chokes on the sheer amount of input, still not finished after N hours, and this just for the documentation of a smallish test assembly. :(

This does not look feasible at this point.

Bright ideas appreciated!

Thursday, April 03, 2008

C# quiz 8/?

Now it's time for some one regarding serialization:

Given this serializable class:

    [Serializable]
public class Victim {
public static int InstanceCount;
public int Data { get; set; }

public Victim() {
InstanceCount++;
Data = 2;
}
}

and the following test code:

    class Program {
static void Main(string[] args) {
Victim v = new Victim();
v.Data = 3;
BinaryFormatter serial = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
serial.Serialize(ms, v);
ms.Seek(0, SeekOrigin.Begin);
v = (Victim)serial.Deserialize(ms);
Console.WriteLine("Data {0}", v.Data);
Console.WriteLine("Instances {0}", Victim.InstanceCount);
}
}

What is the output, and why?

C# quiz 7/?

If we compile the following code to a dll, for example using the following command line:

csc.exe /t:library Library.cs

namespace Library {
public class Library {
public const int FOO = 1;
public static readonly int BAR = 2;
}
}

And refer those in our test program:

    class Program {
static void Main(string[] args) {
Console.WriteLine(Library.Library.FOO);
Console.WriteLine(Library.Library.BAR);
}
}

Now, if we change FOO to 10 and BAR to 12 and recompile Library.dll without recompiling the test program, what is the output when we run the test program?