Convert Boolean to String with Inno Setup

匿名 (未验证) 提交于 2019-12-03 03:00:02

问题:

What is the easiest way to convert a Boolean value into a String in an Inno Setup Pascal script? This trivial task that should be completely implicit seems to require a full-blown if/else construction.

function IsDowngradeUninstall: Boolean; begin     Result := IsCommandLineParamSet('downgrade');     MsgBox('IsDowngradeUninstall = ' + Result, mbInformation, MB_OK); end; 

This doesn't work because "Type mismatch". IntToStr doesn't accept a Boolean neither. BoolToStr does not exist.

回答1:

If you need it once only, the easiest inline solution is to cast the Boolean to Integer and use the IntToStr function. You get 1 for True and 0 for False.

MsgBox('IsDowngradeUninstall = ' + IntToStr(Integer(Result)), mbInformation, MB_OK); 

Though, I usually use the Format function for the same result:

MsgBox(Format('IsDowngradeUninstall = %d', [Result]), mbInformation, MB_OK); 

(Contrary to Delphi) The Inno Setup/Pascal Script Format implicitly converts the Boolean to Integer for %d.


If you need a more fancy conversion, or if you need the conversion often, implement your own function, as @RobeN already shows in his answer.

function BoolToStr(Value: Boolean): String;  begin   if Value then     Result := 'Yes'   else     Result := 'No'; end; 


回答2:

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

or

[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;  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!