Inno setup 为什么我会得到;可变期望值“;编译器错误?
我试图在Inno安装程序中获取安装路径的驱动器号,不带冒号,如果驱动器号是C,它将返回空字符串 调用函数:Inno setup 为什么我会得到;可变期望值“;编译器错误?,inno-setup,pascalscript,Inno Setup,Pascalscript,我试图在Inno安装程序中获取安装路径的驱动器号,不带冒号,如果驱动器号是C,它将返回空字符串 调用函数: {code:GetDriveLetter|{drive:{src}} 功能: function GetDriveLetter(DriveLetter: string): string; var len: Integer; begin len := CompareText(UpperCase(DriveLetter), 'C'); if len = 0 then begin
{code:GetDriveLetter|{drive:{src}}
功能:
function GetDriveLetter(DriveLetter: string): string;
var
len: Integer;
begin
len := CompareText(UpperCase(DriveLetter), 'C');
if len = 0 then
begin
Result := '';
end
else
begin
Result := Delete(UpperCase(DriveLetter), 2, 1);
end;
end;
我得到了编译器错误:
预期变量
在这一行:
Result := Delete(UpperCase(DriveLetter), 2, 1);
那条线有什么问题?如何修复此功能?也许您需要类似的功能
[Code]
function GetDriveLetter(DriveLetter: string): string;
begin
if CompareStr(DriveLetter, 'C:\') = 0 then
begin
Result := '';
end
else begin
Result := Copy(DriveLetter, 1, 1);
end
end;
但您的示例不是针对install path,而是针对installer source path…您得到的变量预期编译器错误,因为这是一个过程,您希望将声明的字符串
类型变量(然后对其进行内部修改)传递到该过程中。您传递的不是变量,而是大写
函数调用的中间结果。因此,若要修复此错误,您可以声明一个变量,或使用预声明的结果
一,例如:
var
S: string;
begin
S := UpperCase('a');
Delete(S, 2, 1);
end;
除了我要指出几件事Delete
是一个过程
,因此不会返回任何值,因此即使将声明的变量传递给它,也会导致不存在的结果赋值失败。CompareText
函数已经是一个不区分大小写的比较,因此不需要在输入端加上大小写。除此之外,我不会比较整个输入(例如,drive:
常量返回的C:
),而只比较第一个字符(但这取决于您希望使函数变得多么安全)。对于第一个字符的比较,我会这样写:
[Code]
function GetDriveLetter(Drive: string): string;
begin
// we will upper case the first letter of the Drive parameter (if that parameter is
// empty, we'll get an empty result)
Result := UpperCase(Copy(Drive, 1, 1));
// now check if the previous operation succeeded and we have in our Result variable
// exactly 1 char and if we have, check if that char is the one for which we want to
// return an empty string; if that is so, return an empty string
if (Length(Result) = 1) and (Result[1] = 'C') then
Result := '';
end;