1. Home
  2. Computing & Technology
  3. Delphi Programming
TGuidEx - custom class for manipulating Guid values
A Guid type represents a 128-bit integer value. The TGuidEx class exposes class (static) methods that help operate GUID values and TGuidField database field types.
 More of this Feature
• TGuidEx - source download
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Database development with Delphi
• Class methods

GUID - Globally Unique IDentifier
In Delphi, GUID values are represented with the TGuid record defined in the System unit. A Guid value is a 128-bit integer (16 bytes) that can be used in database applications when a unique identifier is required.
When developing database applications, the TGuidField is used to represents a guid field in a dataset.

To create a sample GUID, hit CTRL + SHIFT + G in the code editor, this will create a unique GUID value, like this one: ['{B54ED86E-211F-4803-AF46-0586DA66C583}']. Each time you press CTRL + SHIFT + G, you'll get a new Guid value.
A Guid value can be converted from a string constant in the following form 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', in which each x is a hexadecimal digit in the range 0-9 or A-F. For example, '123ABC45-65DE-F0F0-4242-A1B2C3D4E5F699' is a valid uniqueidentifier value.

TGuidEx
To ease the process of manipulating GUID values (in database applications), I've created a simple wrapper class arround the TGuid record. The TGuidEx class provides only class methods (can be called without actually creating and freeing the class).

Here's an example of usage:
Let's say you have a database TABLE with a uniqueidenitier type FIELD "ID".

1. To insert a new row into the TABLE, you can use the next SQL statement:
'insert into TABLE (ID, ...) values (" + TGuidEx.ToQuotedString(TGuidEx.NewGuid) +',...)';

2. To check if a TGuidField is "empty", use
var
  guid : TGuid;
begin
 guid := TGuidField(DataSet.FieldByName('ID')).AsGuid;
 if TGuidEx.IsEmptyGuid(guid) then
   ShowMessage('Value is "Empty"');

Here's the full source code (download):

unit GuidEx;

interface

uses SysUtils;

type
  TGuidEx = class
    //Creates and returns a new globally unique identifier
    class function NewGuid : TGuid;
    //sometimes we need to have an "empty" value, like NULL
    class function EmptyGuid : TGuid;
    //Checks whether a Guid is EmptyGuid
    class function IsEmptyGuid(Guid : TGuid) : boolean;
    //Convert to string
    class function ToString(Guid : TGuid) : string;
    //convert to quoted string
    class function ToQuotedString(Guid : TGuid) : string;
    //return a GUID from string
    class function FromString(Value : string) : TGuid;
    //Indicates whether two TGUID values are the same
    class function EqualGuids(Guid1, Guid2 : TGuid) : boolean;
  end;

implementation

{ TGuidEx }

class function TGuidEx.EmptyGuid: TGuid;
begin
  result := FromString('{00000000-0000-0000-0000-000000000000}');
end;

class function TGuidEx.EqualGuids(Guid1, Guid2: TGuid): boolean;
begin
  result := IsEqualGUID(Guid1, Guid2);
end;

class function TGuidEx.FromString(Value: string): TGuid;
begin
  result := StringToGuid(Value);
end;

class function TGuidEx.IsEmptyGuid(Guid : TGuid): boolean;
begin
  result := EqualGuids(Guid,EmptyGuid);
end;

class function TGuidEx.NewGuid: TGuid;
var
  Guid : TGuid;
begin
  CreateGUID(Guid);
  Result := Guid;
end;

class function TGuidEx.ToQuotedString(Guid: TGuid): string;
begin
  result := QuotedStr(ToString(Guid));
end;

class function TGuidEx.ToString(Guid: TGuid): string;
begin
  result := GuidToString(Guid);
end;

end.//GuidEx

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.