Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GUID constants

468 views
Skip to first unread message

Justus J.

unread,
Jan 12, 2008, 6:28:47 AM1/12/08
to
Hi all,

I'm using GUIDs to uniquely identify records (in files, not in a
database) across instances of my programme. The data is stored in
several files containing different kinds of records, which are "linked"
to each other using IDs. Users are allowed to pass each other their
data-files and data-files might be merged and the programme needs still
to be able to tell which from which, hence the GUIDs (practically no
chance of creating duplicate IDs by accident). All records have a field
ID of type TID which in turn is a TGUID. New records are assigned a new
GUID, whereas references to deleted records result in GUID_NULL. In the
future, I might skip to another way of identifying data, so I'm trying
to isolate the ID stuff.

To simplify this transition, I would like to make a constant of the
"null ID" (GUID_NULL), as it might become, eg, a Longint, String or
whatever someday.

So far the background story. My question:

// --------------------------
uses
ActiveX;

type
TID = TGUID;

const
cNullID = GUID_NULL;
// --------------------------

The compiler aborts on a "constant expression expected". Looking at
ActiveX.pas, GUID_NULL *is* a constant. What's going wrong?

As a bypass, I have declared the cNullID as a var, and set it in the
Initialization part of the unit.

GUIDs are strange things anyway, as I can't check for values like
if (aGUID = bGUID) then ...

I seem to be forced to use
if ((GUIDTostring(aGUID) = GUIDToString(bGUID)) then ...

although I can live with that.

Justus

Chris.Cheney

unread,
Jan 12, 2008, 9:11:29 AM1/12/08
to
"Justus J." <In...@SKIPTHIS.row-ware.com> wrote in news:4788A46F.7080203
@SKIPTHIS.row-ware.com:

GUID_NULL may be a constant but it is not a <constant expression> because
TGUID is a packed record type.

> GUIDs are strange things anyway, as I can't check for values like
> if (aGUID = bGUID) then ...

TGUID is a packed record ... (I seem to be repeating myself).

> I seem to be forced to use
> if ((GUIDTostring(aGUID) = GUIDToString(bGUID)) then ...

Use the IsEqualGUID function (see help).

HTH

Rudy Velthuis

unread,
Jan 12, 2008, 9:55:37 AM1/12/08
to
Justus J. wrote:

> type
> TID = TGUID;
>
> const
> cNullID = GUID_NULL;
> // --------------------------
>
> The compiler aborts on a "constant expression expected".

There are so called "true" constants, which have no type and must be
basic types, like numerals, characters or strings and there are typed
constants, like the ones you are using. Only true constants can be part
of a constant expression.

GUID_NULL is a typed constant, so you can't use it in a constant
expression. You can declare cNullID as variable and then initialize it
in the initialization section of the unit:

initialization
cNullID := GUID_NULL;

end.

For the rest of the program, the effect is the same.
--
Rudy Velthuis http://rvelthuis.de

"Linux is like living in a teepee. No Windows, no Gates, Apache
in house." -- Usenet signature

Rob Kennedy

unread,
Jan 12, 2008, 12:12:59 PM1/12/08
to
Justus J. wrote:
> uses
> ActiveX;
>
> type
> TID = TGUID;
>
> const
> cNullID = GUID_NULL;
> // --------------------------
>
> The compiler aborts on a "constant expression expected". Looking at
> ActiveX.pas, GUID_NULL *is* a constant. What's going wrong?

Typed constants can't be used where constant expressions are required.
QC has a bug filed against that, but it's marked as closed and inactive,
which means that although it hasn't been fixed, CodeGear has no plans to
actively work on it, either. If it's particularly painful to you, go
vote for it and lobby that the case be re-opened.

http://qc.codegear.com/wc/qcmain.aspx?d=8258

Typed constants can only be used in regular executable statements, not
in declarations as above. Either assign cNullID somewhere else, as Rudy
demonstrated, or make cNullID's declaration duplicate GUID_Null's
declaration.

> As a bypass, I have declared the cNullID as a var, and set it in the
> Initialization part of the unit.
>
> GUIDs are strange things anyway, as I can't check for values like
> if (aGUID = bGUID) then ...

A GUID is a record, and in Delphi, records aren't comparable. That's QC
1238, in case you're interested. It's marked as fixed, but the fix is
that Delphi supports operator overloading for records now. I don't know
whether there's a default comparison operator for all records, though,
or whether TGUID has a comparison operator defined.

> I seem to be forced to use
> if ((GUIDTostring(aGUID) = GUIDToString(bGUID)) then ...

More direct is to use IsEqualGUID.

--
Rob

0 new messages