【问题标题】:InnoSetup: How to pass a two dimensional string array to a functionInnoSetup:如何将二维字符串数组传递给函数
【发布时间】:2013-08-29 09:17:08
【问题描述】:

Innosetup 正在杀死我。我收到一个 RUNTIME 'Type Mismatch' 错误,对我来说,这是非常出乎意料的。我正在使用 Inno-setup 5.5.3 (u) (其中“u”表示 unicode 版本)

我正在尝试将二维数组传递给方法。

这是我的完整示例。

[Setup]
AppName=EmptyProgram
AppVerName=EmptyProgram 1
UsePreviousAppDir=false
DefaultDirName={pf}\EmptyProgram
Uninstallable=false
OutputBaseFilename=HelloWorld
PrivilegesRequired=none

[Messages]
SetupAppTitle=My Title

[Code]
var
    langMap : array[0..3] of array[0..1] of String;


function getMapVal(map : array of array[0..1] of String; key: String ) : String;
begin
    Result:='not testing the body of the method';
end;

function InitializeSetup(): Boolean;
begin
    MsgBox('Hello world.', mbInformation, MB_OK);

    getMapVal(langMap, 'hello');    // this line here fails with type mismatch! Why?

    Result := FALSE;
end;

这个例子可以运行,但要调用方法:

getMapVal(langMap, 'hello');

它可以编译,因此对声明很满意。但是在调用时,不匹配错误。我究竟做错了什么?

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    首先,您制作的不是哈希映射,而是纯键值列表。目前没有办法在 InnoSetup 中制作真正的泛型哈希映射。无论如何,您当前的代码需要一个完整的重构。我宁愿这样写:

    [Setup]
    AppName=My Program
    AppVersion=1.5
    DefaultDirName={pf}\My Program
    
    [Code]
    type
      TKey = string;
      TValue = string;
      TKeyValue = record
        Key: TKey;
        Value: TValue;
      end;
      TKeyValueList = array of TKeyValue;
    
    function TryGetValue(const KeyValueList: TKeyValueList; const Key: TKey; 
      var Value: TValue): Boolean;
    var
      I: Integer;
    begin
      Result := False;
      for I := 0 to GetArrayLength(KeyValueList) - 1 do
        if KeyValueList[I].Key = Key then
        begin
          Result := True;
          Value := KeyValueList[I].Value;
          Exit;
        end;
    end;
    
    procedure InitializeWizard;
    var 
      I: Integer;
      Value: TValue;
      KeyValueList: TKeyValueList;
    begin
      SetArrayLength(KeyValueList, 3);
      for I := 0 to 2 do
      begin
        KeyValueList[I].Key := 'Key' + IntToStr(I);
        KeyValueList[I].Value := 'Value' + IntToStr(I);
      end;
    
      if TryGetValue(KeyValueList, 'Key2', Value) then
        MsgBox('Value: ' + Value, mbInformation, MB_OK);
    end;
    

    【讨论】:

    • 感谢这个有趣的例子。我现在注意到一件事,因为我希望能够简化元素的“插入”(不仅仅是获取),似乎没有“参考传递”?是这样吗?即,不可能将“插入”代码移动到过程中,在该方法中进行插入(并期望在过程完成后值仍然存在)?
    • 这是一个如何实现字符串键值列表的示例。也就是说,你的代码,不过是重构的。你问的是另一个问题的话题,因为我不知道你的确切意思。
    • 不,对不起,没关系。当我自己的代码遇到问题时,在(重新)测试你的代码(这次是正确的)时,我发现我犯了一个错误。我没有我在上一条评论中提到的那种麻烦。再次感谢您的帮助。
    猜你喜欢
    • 2015-05-27
    • 2018-03-12
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2023-03-06
    相关资源
    最近更新 更多