I've found something funny which might (maybe) be a compiler problem so just wondering firstly if I've got something wrong and secondly if anyone else has a problem with the following code:
There's no problem with this code, if replace unknown cnCrLf with LineEnding. But...
1. If the parameter is only for returning a value, it is better to mark it as out.
2. It is better to use the const modifier if the parameter does not change, especially for managed types.
3. The try finally block is not used correctly. Correct: AllocResource; try UseResource finally FreeResource end;
4. I suspect that on all platforms if SaveDialog.Execute returns true, the file name will not be empty.
With this in mind, we can rewrite as follows:
function GetSaveFilename(out MyFilename: string; const Filter: string = '';
const Title: string = ''): Boolean;
var
SaveDialog: TSaveDialog;
begin
SaveDialog := TSaveDialog.Create(nil);
try
try
SaveDialog.Title := Title;
SaveDialog.Filter := Filter;
Result := SaveDialog.Execute;
if Result then
MyFilename := SaveDialog.FileName;
except
on E: Exception do
raise Exception.Create('Cannot get save filename' + LineEnding + E.Message);
end;
finally
SaveDialog.Free;
end
end;
I also think that exception handling is not particularly useful here.