Annotating Methods in Delphi?

The feature is named XML Documentation Comments and is documented here. It appears to have been modelled closely on the equivalent .net feature so you should be right at home with it. The documentation contains this example: /// <summary> Removes the specified item from the collection /// </summary> /// <param name=”Item”>The item to remove /// … Read more

How can I sanitize a string for use as a filename?

You can use PathGetCharType function, PathCleanupSpec function or the following trick: function IsValidFilePath(const FileName: String): Boolean; var S: String; I: Integer; begin Result := False; S := FileName; repeat I := LastDelimiter(‘\/’, S); MoveFile(nil, PChar(S)); if (GetLastError = ERROR_ALREADY_EXISTS) or ( (GetFileAttributes(PChar(Copy(S, I + 1, MaxInt))) = INVALID_FILE_ATTRIBUTES) and (GetLastError=ERROR_INVALID_NAME) ) then Exit; if I>0 … Read more

TStringList vs. TList

TStringList is a descendant of TStrings. TStringList knows how to sort itself alphabetically. TStringList has an Objects property. TStringList doesn’t make your code incompatible with all previous versions of Delphi. TStringList can be used as a published property. (A bug prevents generic classes from being published, for now.)

Declare a TDateTime as a Const in Delphi

Ok, my reaction is a bit late, but here’s a solution for the newer Delphi’s. It uses implicit class overloaders so that records of this type can be used as if they are TDateTime variables. TDateRec = record year,month,day,hour,minute,second,millisecond:word; class operator implicit(aDateRec:TDateRec):TDateTime; class operator implicit(aDateTime:TDateTime):TDateRec; // not needed class operator implicit(aDateRec:TDateRec):String; // not needed class … Read more

How to parse a JSON string in Delphi?

You don’t need to use external libraries to perform a JSONPath search. Example with Delphi 10 Seattle: uses System.JSON; procedure ParseJSonValue; var JSonValue:TJSonValue; st:string; Branch: string; begin st := ‘{“data”:{“results”:[{“Branch”:”ACCT590003″}]}}’; JsonValue := TJSonObject.ParseJSONValue(st); Branch := JsonValue.GetValue<string>(‘data.results[0].Branch’); JsonValue.Free; end;

Best way to find if a string is in a list (without generics)

You could use AnsiIndexText(const AnsiString AText, const array of string AValues):integer or MatchStr(const AText: string; const AValues: array of string): Boolean; (Both from StrUtils unit) Something like: Result := (AnsiIndexText(‘Hi’,[‘Hello’,’Hi’,’Foo’,’Bar’]) > -1); or Result := MatchStr(‘Hi’, [‘foo’, ‘Bar’]); AnsiIndexText returns the 0-offset index of the first string it finds in AValues that matches AText case-insensitively. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)