2015-05-13 338 views
2

我试图在没有冒号的Inno安装程序中获得安装路径的驱动器号,如果驱动器号是C,它将返回一个空字符串。为什么我会收到“Variable Expected”编译器错误?

调用函数:

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

我得到的编译器错误:

Variable Expected

在这条线:

Result := Delete(UpperCase(DriveLetter), 2, 1); 

那条线有什么问题?我如何解决这个功能?

回答

1

你有因为Delete可变预计编译器错误是要在其中有望通过一个声明的程序string类型变量(然后在内部修改)。你没有传递一个变量,而是一个UpperCase函数调用的中间结果。因此,要解决这个错误,你可以声明一个变量,或使用预声明Result一个,例如:

var 
    S: string; 
begin 
    S := UpperCase('a'); 
    Delete(S, 2, 1); 
end; 

除了我想指出的几件事情。 Deleteprocedure,因此不会返回任何值,因此即使您传递了一个声明的变量,也会失败一个不存在的结果分配。功能已经是不区分大小写的比较,所以不需要上层输入。除了我不会比较整个输入(例如,C:drive:常量返回),但只有第一个字符(但它取决于您想要使用的函数的安全性)。对于只有第一个字符的比较,我会写这样的东西:

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

但你的榜样是不是安装路径,但安装程序源路径...

相关问题