使用Inno Setup将Boolean转换为String

ygo*_*goe 8 inno-setup pascalscript

在Inno Setup Pascal脚本中将布尔值转换为String的最简单方法是什么?应该完全隐含的这个微不足道的任务似乎需要一个完整的if/ else构造.

function IsDowngradeUninstall: Boolean;
begin
    Result := IsCommandLineParamSet('downgrade');
    MsgBox('IsDowngradeUninstall = ' + Result, mbInformation, MB_OK);
end;
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为"类型不匹配".IntToStr不接受Boolean.BoolToStr不存在.

Mar*_*ryl 19

如果只需要一次,最简单的内联解决方案是转换BooleanInteger并使用该IntToStr函数.你得到1True0False.

MsgBox('IsDowngradeUninstall = ' + IntToStr(Integer(Result)), mbInformation, MB_OK);
Run Code Online (Sandbox Code Playgroud)

虽然,我通常使用该Format函数得到相同的结果:

MsgBox(Format('IsDowngradeUninstall = %d', [Result]), mbInformation, MB_OK);
Run Code Online (Sandbox Code Playgroud)

(与Delphi相反)Inno Setup/Pascal Script Format隐式转换BooleanIntegerfor %d.


如果您需要更精彩的转换,或者您需要经常进行转换,请执行您自己的功能,正如@RobeN已在其答案中显示的那样.

function BoolToStr(Value: Boolean): String; 
begin
  if Value then
    Result := 'Yes'
  else
    Result := 'No';
end;
Run Code Online (Sandbox Code Playgroud)


Rob*_*beN 5

[Code]
function BoolToStr(Value : Boolean) : String; 
begin
  if Value then
    result := 'true'
  else
    result := 'false';
end;
Run Code Online (Sandbox Code Playgroud)

或者

[Code]
function IsDowngradeUninstall: Boolean;
begin
    Result := IsCommandLineParamSet('downgrade');
    if Result then 
      MsgBox('IsDowngradeUninstall = True', mbInformation, MB_OK)
    else
      MsgBox('IsDowngradeUninstall = False', mbInformation, MB_OK);
end; 
Run Code Online (Sandbox Code Playgroud)