Today I needed to create a sequential GUid in .Net to test if my Compareto function worked as expected.

The problem is that Guid.NewGuid produces a random Guid duh. which isn’t sequential. Of course in my unittests I want to know what value I’m giving to the guid.

.Net doesn’t have a SequentialGuid function as far as I know So went for a little Google. And I found this site.

Generate Sequential GUIDs for SQL Server 2005 in C#

Which has an interesting concept. But I needed no such difficult things for my unittests.

The following would prove my point just as well.

vb.net
1
2
3
4
ObjectThatHasGuid1 = New ObjectThatHasGuid()
ObjectThatHasGuid1.GetType.GetField("_id",Reflection.BindingFlags.NonPublic or Reflection.BindingFlags.Instance).SetValue(ObjectThatHasGuid1,new Guid("00000000-0000-0000-0000-000000000001"))
ObjectThatHasGuid2 = New ObjectThatHasGuid()
ObjectThatHasGuid2.GetType.GetField("_id",Reflection.BindingFlags.NonPublic or Reflection.BindingFlags.Instance).SetValue(ObjectThatHasGuid2,new Guid("00000000-0000-0000-0000-000000000002"))
ObjectThatHasGuid1 = New ObjectThatHasGuid()
ObjectThatHasGuid1.GetType.GetField("_id",Reflection.BindingFlags.NonPublic or Reflection.BindingFlags.Instance).SetValue(ObjectThatHasGuid1,new Guid("00000000-0000-0000-0000-000000000001"))
ObjectThatHasGuid2 = New ObjectThatHasGuid()
ObjectThatHasGuid2.GetType.GetField("_id",Reflection.BindingFlags.NonPublic or Reflection.BindingFlags.Instance).SetValue(ObjectThatHasGuid2,new Guid("00000000-0000-0000-0000-000000000002"))

Life can be so easy sometimes if you know how. I totaly forgot that GUID was an object and that you can instantiate it.