函数 might be undefined 报警?Result如何写? ( 积分: 0 )

  • 主题发起人 主题发起人 fyxr
  • 开始时间 开始时间
F

fyxr

Unregistered / Unconfirmed
GUEST, unregistred user!
请大侠看下我的函数为什么出“ 函数 might be undefined ”报警,
去掉注释任一处都将报警,因为有提前返回,没办法写成完整的if嵌套,
请问如何修改,谢谢!

function gufGetNextSerialNumber(aSerialNumber:string;aSeparatorBeforeNotCompute:Boolean=True;aFixLength:Boolean=True;aLetterNotCompute:Boolean=True):string;
var
SerialNumber:string;
SeparatorBeforeNotCompute:Boolean;
FixLength:Boolean;
LetterNotCompute:Boolean;
SerialNumberLength:Integer;

I:Integer;
BreakI:Integer;

HeadNumberIndex:Integer;
LastNumberIndex:Integer;
NumberBeforeStr:string;
NumberBehindStr:string;

SeparatorIndex:Integer;
ComputeStr:string;
NotComputeIndex:Integer;
NotComputeStr:string;
FormatComputeStr:string;
begin
SerialNumber:=aSerialNumber;
SeparatorBeforeNotCompute:=aSeparatorBeforeNotCompute;
FixLength:=aFixLength;
LetterNotCompute:=aLetterNotCompute;

HeadNumberIndex:=-1;
LastNumberIndex:=-1;
SeparatorIndex:=-1;
NotComputeIndex:=-1;
BreakI:=-1;

SerialNumberLength:=Length(SerialNumber);


if SerialNumberLength<1 then begin Result:='';Exit;end;

for I:=SerialNumberLength downto 1 do
begin
if (HeadNumberIndex=-1) and (SerialNumber in ['0'..'9']) then HeadNumberIndex:=I
else if (HeadNumberIndex<>-1) and (not (SerialNumber in ['0'..'9'])) then begin LastNumberIndex:=I+1;Break;end;
end;

for I:=SerialNumberLength downto 1 do if not (SerialNumber in ['0'..'9','a'..'z','A'..'Z']) then begin SeparatorIndex:=I;Break;end;


if ((SeparatorBeforeNotCompute) and (FixLength) and (LetterNotCompute)) then
begin
//(*
if (HeadNumberIndex=-1) or (LastNumberIndex=-1) then begin Result:='';Exit;end;
if SeparatorIndex>LastNumberIndex then begin Result:='';Exit;end;

NumberBeforeStr:=Copy(SerialNumber,1,LastNumberIndex-1);
NumberBehindStr:=Copy(SerialNumber),1,SerialNumberLength-HeadNumberIndex);
ComputeStr:=Copy(SerialNumber,SerialNumberLength-HeadNumberIndex+1,HeadNumberIndex-LastNumberIndex+1));

if ComputeStr='' then begin Result:='';Exit;end;

if Length(IntToStr(StrToInt(ComputeStr)+1))>Length(ComputeStr) then begin Result:='';Exit;end
else
begin
FormatComputeStr:=IntToStr(StrToInt(ComputeStr)+1);
FormatComputeStr:=StringOfChar('0',Length(ComputeStr)-Length(FormatComputeStr))+FormatComputeStr;
Result:=NumberBeforeStr+FormatComputeStr+NumberBehindStr;
Exit;
end;
//*)
end;

if ((SeparatorBeforeNotCompute) and (FixLength) and (not LetterNotCompute)) then
begin
//(*
for I:=SerialNumberLength downto 1 do
begin
if not (SerialNumber in ['0'..'9','a'..'z','A'..'Z']) then begin Result:='';Exit;end
else
begin
if SerialNumber in ['0'..'9'] then
begin
if (SerialNumber<'9') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='0';
end;
if SerialNumber in ['a'..'z'] then
begin
if (SerialNumber<'z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='a';
end;
if SerialNumber in ['A'..'Z'] then
begin
if (SerialNumber<'Z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='A';
end;
end;
end;
//*)
end;

if (SeparatorBeforeNotCompute) and (not FixLength) and (LetterNotCompute) then
begin
//(*
if (HeadNumberIndex=-1) or (LastNumberIndex=-1) then begin Result:='';Exit;end;
if SeparatorIndex>LastNumberIndex then begin Result:='';Exit;end;
NumberBeforeStr:=Copy(SerialNumber,1,LastNumberIndex-1);
NumberBehindStr:=Copy(SerialNumber,1,SerialNumberLength-HeadNumberIndex);
ComputeStr:=Copy(SerialNumber,SerialNumberLength-HeadNumberIndex+1,HeadNumberIndex-LastNumberIndex+1));

if (ComputeStr='') then begin Result:='';Exit;end;
FormatComputeStr:=IntToStr(StrToInt(ComputeStr)+1);
FormatComputeStr:=StringOfChar('0',Length(ComputeStr)-Length(FormatComputeStr))+FormatComputeStr;
Result:=NumberBeforeStr+FormatComputeStr+NumberBehindStr;
Exit;
//*)
end;

if (SeparatorBeforeNotCompute) and (not FixLength) and (not LetterNotCompute) then
begin
//(*
for I:=SerialNumberLength downto 1 do if not (SerialNumber in ['0'..'9','a'..'z','A'..'Z']) then begin NotComputeIndex:=SerialNumberLength-I;Break;end;
ComputeStr:=Copy(SerialNumber,1,NotComputeIndex);
NotComputeStr:=Copy(SerialNumber,1,SerialNumberLength-NotComputeIndex);

for I:=Length(ComputeStr) downto 1 do
begin
if ComputeStr in ['0'..'9'] then
begin
if (ComputeStr<'9') then begin ComputeStr:=Chr(Ord(ComputeStr)+1);Result:=NotComputeStr+ComputeStr;Exit;end
else ComputeStr:='0';
end;
if ComputeStr in ['a'..'z'] then
begin
if (ComputeStr<'z') then begin ComputeStr:=Chr(Ord(ComputeStr)+1);Result:=NotComputeStr+ComputeStr;Exit;end
else ComputeStr:='a';
end;
if ComputeStr in ['A'..'Z'] then
begin
if (ComputeStr<'Z') then begin ComputeStr:=Chr(Ord(ComputeStr)+1);Result:=NotComputeStr+ComputeStr;Exit;end
else ComputeStr:='A';
end;
BreakI:=I;
end;
if (BreakI=0) and (ComputeStr[1] in ['0'..'9']) then begin Result:=NotComputeStr+'1'+ComputeStr;Exit;end;
if (BreakI=0) and (ComputeStr[1] in ['a'..'z']) then begin Result:=NotComputeStr+'a'+ComputeStr;Exit;end;
if (BreakI=0) and (ComputeStr[1] in ['A'..'Z']) then begin Result:=NotComputeStr+'A'+ComputeStr;Exit;end;
//*)
end;

if (not SeparatorBeforeNotCompute) and (FixLength) and (LetterNotCompute) then
begin
//(*
for I:=SerialNumberLength downto 1 do
begin
if SerialNumber in ['0'..'9'] then
begin
if (SerialNumber<'9') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='0';
end;
BreakI:=I;
end;
if (BreakI=0) then begin Result:='';Exit;end;
//*)
end;

if (not SeparatorBeforeNotCompute) and (FixLength) and (not LetterNotCompute) then
begin
//(*
for I:=SerialNumberLength downto 1 do
begin
if SerialNumber in ['0'..'9'] then
begin
if (SerialNumber<'9') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='0';
end;
if SerialNumber in ['a'..'z'] then
begin
if (SerialNumber<'z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='a';
end;
if SerialNumber in ['A'..'Z'] then
begin
if (SerialNumber<'Z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='A';
end;
BreakI:=I;
end;
if (BreakI=0) then begin Result:='';Exit;end;
//*)
end;

if (not SeparatorBeforeNotCompute) and (not FixLength) and (LetterNotCompute) then
begin
//(*
if (HeadNumberIndex=-1) or (LastNumberIndex=-1) then begin Result:='';Exit;end;
NumberBeforeStr:=Copy(SerialNumber,1,LastNumberIndex-1);
NumberBehindStr:=Copy(SerialNumber,1,SerialNumberLength-HeadNumberIndex);
ComputeStr:=Copy(SerialNumber,SerialNumberLength-HeadNumberIndex+1,HeadNumberIndex-LastNumberIndex+1));

//if (ComputeStr='') then begin Result:='';Exit;end;

FormatComputeStr:=IntToStr(StrToInt(ComputeStr)+1);
//FormatComputeStr:=StringOfChar('0',Length(ComputeStr)-Length(FormatComputeStr))+FormatComputeStr;//这一行比较奇怪,没有Result也出警告
Result:=NumberBeforeStr+FormatComputeStr+NumberBehindStr;
Exit;
//*)
end;

if (not SeparatorBeforeNotCompute) and (not FixLength) and (not LetterNotCompute) then
begin
//(*
//for I:=Length(SerialNumber) downto 1 do
begin
//if SerialNumber in ['0'..'9'] then
begin
//if (SerialNumber<'9') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=NotComputeStr+SerialNumber;Exit;end
//else SerialNumber:='0';
end;
//if SerialNumber in ['a'..'z'] then
begin
//if (SerialNumber<'z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=NotComputeStr+SerialNumber;Exit;end
//else SerialNumber:='a';
end;
//if SerialNumber in ['A'..'Z'] then
begin
//if (SerialNumber<'Z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=NotComputeStr+SerialNumber;Exit;end
//else SerialNumber:='A';
end;
end;
//for I:=1 to SerialNumberLength do
begin
//if (SerialNumber in ['0'..'9']) then begin Result:=Copy(SerialNumber,1,I-1)+'1'+Copy(SerialNumber,I,SerialNumberLength-I+1);Exit;end;
//if (SerialNumber in ['a'..'z']) then begin Result:=Copy(SerialNumber,1,I-1)+'a'+Copy(SerialNumber,I,SerialNumberLength-I+1);Exit;end;
//if (SerialNumber in ['A'..'Z']) then begin Result:=Copy(SerialNumber,1,I-1)+'A'+Copy(SerialNumber,I,SerialNumberLength-I+1);Exit;end;
end;
//*)
end;
end;
 
if SerialNumberLength<1 then begin Result:='';Exit;end;
把这句改下
Result := ''
//默认返回空字符串,有了默认返回值下面怎么写都不会有警告了
if SerialNumberLength < 1 then Exit;
 
老大我试过,不行的!
 
最后一个分支,注释部分去了,还是报警,

if (not SeparatorBeforeNotCompute) and (not FixLength) and (not LetterNotCompute) then
begin
//(*
for I:=SerialNumberLength downto 1 do
begin
if SerialNumber in ['0'..'9'] then
begin
if (SerialNumber<'9') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
else SerialNumber:='0';
end;
if SerialNumber in ['a'..'z'] then
begin
//if (SerialNumber<'z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
//else SerialNumber:='a';
end;
if SerialNumber in ['A'..'Z'] then
begin
//if (SerialNumber<'Z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=SerialNumber;Exit;end
//else SerialNumber:='A';
end;
end;
for I:=1 to SerialNumberLength do
begin
//if (SerialNumber in ['0'..'9']) then begin Result:=Copy(SerialNumber,1,I-1)+'1'+Copy(SerialNumber,I,SerialNumberLength-I+1);Exit;end;
//if (SerialNumber in ['a'..'z']) then begin Result:=Copy(SerialNumber,1,I-1)+'a'+Copy(SerialNumber,I,SerialNumberLength-I+1);Exit;end;
//if (SerialNumber in ['A'..'Z']) then begin Result:=Copy(SerialNumber,1,I-1)+'A'+Copy(SerialNumber,I,SerialNumberLength-I+1);Exit;end;
end;
//*)
end;
 
如果只有最后一个分支,去掉其他的分支,就没事了,
不知问题出在哪?
 
//SerialNumberLength := Length(SerialNumber);
for I:=SerialNumberLength downto 1 do
begin
if SerialNumber in ['0'..'9'] then
begin
if SerialNumber<'9' then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result := NotComputeStr + SerialNumber;Exit;end
else SerialNumber:='0';
end else
if SerialNumber in ['a'..'z'] then
begin
if (SerialNumber<'z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=NotComputeStr+SerialNumber;Exit;end
else SerialNumber:='a';
end else
if SerialNumber in ['A'..'Z'] then
begin
if (SerialNumber<'Z') then begin SerialNumber:=Chr(Ord(SerialNumber)+1);Result:=NotComputeStr+SerialNumber;Exit;end
else SerialNumber:='A';
end;
end;
原因就不知道了,不过改成上面这样就行了
另,这个函数似乎乱了点
 
大侠见笑了,是有点乱,我去试一下,再次感谢!
 
谢谢大侠,通过了,也不知道为什么
 
后退
顶部