Results 1 to 3 of 3

Thread: best ways of auto assing a id value to an object for a specific class?

  1. #1

    best ways of auto assing a id value to an object for a specific class?

    What is the best way of auto assing a id with an value for a object of a specific class?
    Use a class var to count the number of instances? Would that limit the number of delphi supported, is fpc capeable of such a thing? Or are the better ways?

    Thanks for your answers in advance.
    http://3das.noeska.com - create adventure games without programming

  2. #2

    Re: best ways of auto assing a id value to an object for a specific class?

    Below is the only way I've successfully managed to do this in Lazarus and Delphi. You basically have to go back to the FindComponent method and rely on it giving you an accurate answer. This isn't copy/paste code its from the top of my head so minor tweaks may be necessary (I don't have a compiler handy on this system).

    [pascal]function getValidName(baseName : AnsiString): AnsiString;
    var
    i : integer;
    begin
    i := 1;
    while assigned(FindComponent(baseName+inttostr(i))) do
    inc(i);
    result := baseName+inttostr(i);
    end;

    function getValidNameFromClass(aClass : TPersistentClass) : AnsiString;
    begin
    result := getValidName(aClass.ClassName);
    end;[/pascal]

  3. #3

    Re: best ways of auto assing a id value to an object for a specific class?

    If you want an unique id, simply use the object address:
    [code=pascal]
    function GetUniqueID(AObj: TObject): AnsiString;
    begin
    result := AObj.ClassName + IntToStr(Integer(AObj));
    end;[/code]

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •