[Lazarus] Invalid component names

dmitry boyarintsev skalogryz.lists at gmail.com
Mon Apr 27 10:59:52 CEST 2009


to be sure about name is unique name at the run time there're at least two ways.

1st) global counter

var
  GlobalCounter : integer = 0;

function GetUniqueName(ClassName: String = ''): string;
begin
  if PrefixName = '' then Result := 'dynamicComponent'+IntToStr(GlobalCounter)
  else Result := Copy(ClassName, 2, length(ClassName)-1) +
IntToStr(GlobalCounter);
  // need to care about thread safety here.
  inc(GlobalCounter);
end;

2nd) use GUIDs.

function GetUniqueName(ClassName: String = ''): string;
var
  g : TGUID;
begin
  SysUtils.CreateGUID(g);
  Result := ClassName + GUIDToString(g);
end;

...

  AComponent := TSomeType.Create( owner );
  AComponent.Name := GetUniqueName(AComponent.ClassName);

thanks,
dmitry



More information about the Lazarus mailing list