Board index » delphi » Q: Unique ID in a class

Q: Unique ID in a class

I would like to generate a unique ID for each object I create
in a class. What is the best way to do that?

Thanks -- John

 

Re:Q: Unique ID in a class


In article <317904D6....@pilot.msu.edu>,
   "John E. Bell" <johnb...@pilot.msu.edu> wrote:

Quote
>I would like to generate a unique ID for each object I create
>in a class. What is the best way to do that?

>Thanks -- John

Try this. (Syntax tested for D2 -- not runntime tested. Should work for D1
as well.) It uses a class function to return the next ID in the class, and
a class static variable (in this case implemented by using a writeable
typed constant, thus the $J+ compiler switch) to store the next ID.

(I prefer using typed constants as static variables (as I do here), rather
than global variables, because it provides better encapsulation.)

unit IDClasses;

{$J+}
interface

type

IDClass = class(TObject)
  private
    FID: Longint;
  protected
    class function GetNextID: Longint;
  public
    constructor Create;
    property ID: Longint  read FID;
  end;

implementation

constructor IDClass.Create;
begin
  inherited Create;
  FID := GetNextID;
end;

class function IDClass.GetNextID: Longint;
const
  NextID: Longint = 0;
begin
  Result := NextID;
  Inc(NextID);
end;

end.

--
Brad Aisa <ba...@hookup.net>
Senior Software Designer
Business Machine Interfaces, Inc.
Toronto, Ontario, Canada

Re:Q: Unique ID in a class


In article <317904D6....@pilot.msu.edu>, "John E. Bell" <johnb...@pilot.msu.edu> writes:

Quote
>I would like to generate a unique ID for each object I create
>in a class. What is the best way to do that?

>Thanks -- John

Do you mean that you want each instance of your class to have a
identifying code?  Or that you want different objects created as data
members of some class to have unique ids?

If you want different Instance Id's, why not just use the instance
pointer itself, which is already unique to any instance you've created?

Do you actually need to know what the ID is, or are you just using it
for comparison purposes (i.e. ObjectA <> ObjectB)?

A bit more info please...

Best,

-* Stephen *-
Stephen Posey
University of New Orleans
Email  : S...@uno.edu
WWW    : http://www.uno.edu/~slp

Re:Q: Unique ID in a class


Quote
s...@uno.edu wrote:

> In article <317904D6....@pilot.msu.edu>, "John E. Bell" <johnb...@pilot.msu.edu> writes:
> >I would like to generate a unique ID for each object I create
> >in a class. What is the best way to do that?

> >Thanks -- John

> Do you mean that you want each instance of your class to have a
> identifying code?  Or that you want different objects created as data
> members of some class to have unique ids?

> If you want different Instance Id's, why not just use the instance
> pointer itself, which is already unique to any instance you've created?

> Do you actually need to know what the ID is, or are you just using it
> for comparison purposes (i.e. ObjectA <> ObjectB)?

I might be able to live with just knowing objects apart, but what
I would really like is a small integer that uniquely identifies
each object, such as 1, 2, 3, etc.

Thanks -- John

Re:Q: Unique ID in a class


In article <317DF982.3...@pilot.msu.edu>, "John E. Bell" <johnb...@pilot.msu.edu> writes:

Quote
>s...@uno.edu wrote:

>> In article <317904D6....@pilot.msu.edu>, "John E. Bell" <johnb...@pilot.msu.edu> writes:
>> >I would like to generate a unique ID for each object I create
>> >in a class. What is the best way to do that?

>> >Thanks -- John

>> Do you mean that you want each instance of your class to have a
>> identifying code?  Or that you want different objects created as data
>> members of some class to have unique ids?

>> If you want different Instance Id's, why not just use the instance
>> pointer itself, which is already unique to any instance you've created?

>> Do you actually need to know what the ID is, or are you just using it
>> for comparison purposes (i.e. ObjectA <> ObjectB)?

>I might be able to live with just knowing objects apart, but what
>I would really like is a small integer that uniquely identifies
>each object, such as 1, 2, 3, etc.

>Thanks -- John

I'm still unclear about where the "objects" are coming from: are these
instances of new classes you're creating, are these dynamically
allocated pre-existing classes, are they being created as data members
of another classs, are they components on a form, what?  And (in general
terms) what is your need for unique identification? it may be that
Delphi or the VCL have a solution to that need already in place.

Since your question asks about "generating" the IDs, and this can get a
bit tricky depending on the context wherein these controls are being
created; I can't really help without a bit more info as to how and when
the classes are instantiated.

More info please,

-* Stephen *-
Stephen Posey
University of New Orleans
Email  : S...@uno.edu
WWW    : http://www.uno.edu/~slp

Re:Q: Unique ID in a class


Quote
s...@uno.edu wrote:

> In article <317DF982.3...@pilot.msu.edu>, "John E. Bell" <johnb...@pilot.msu.edu> writes:
> >s...@uno.edu wrote:

> >> In article <317904D6....@pilot.msu.edu>, "John E. Bell" <johnb...@pilot.msu.edu> writes:
> >> >I would like to generate a unique ID for each object I create
> >> >in a class. What is the best way to do that?

> >> >Thanks -- John

> >> Do you mean that you want each instance of your class to have a
> >> identifying code?  Or that you want different objects created as data
> >> members of some class to have unique ids?

> >> If you want different Instance Id's, why not just use the instance
> >> pointer itself, which is already unique to any instance you've created?

> >> Do you actually need to know what the ID is, or are you just using it
> >> for comparison purposes (i.e. ObjectA <> ObjectB)?

> >I might be able to live with just knowing objects apart, but what
> >I would really like is a small integer that uniquely identifies
> >each object, such as 1, 2, 3, etc.

> >Thanks -- John

> I'm still unclear about where the "objects" are coming from: are these
> instances of new classes you're creating, are these dynamically
> allocated pre-existing classes, are they being created as data members
> of another classs, are they components on a form, what?  And (in general
> terms) what is your need for unique identification? it may be that
> Delphi or the VCL have a solution to that need already in place.

They are objects that I am creating and will be stored in a list
which might get reorganized. The objects are, in effect, given to
another process which will want to refer back to the original.
Hence, giving each one a unique, preferrably user-friendly identifier
(i.e., not a 9 digit number) would enable me to find it easily in
a list.

Thanks again - John

Other Threads