程序如下
  TUser=record
    UserName:string[100];
    AThread:TIdPeerThread;
  end;OnlineUser:array of TUser;
添加用户到在线列表中
function TMsg.AddUser(AUser:TUser):Boolean;
begin
  //WriteLog('添加到在线列表');
  cs.Enter;
  Result:=False;
  try
    try
      SetLength(OnlineUser,High(OnlineUser)+2);  //此处+2应该没错吧,
      OnlineUser[High(OnlineUser)]:=AUser;      //
      Result:=True;
      WriteLog('在线用户数:'+IntToStr(High(onlineuser)));    //此处为什么不是1,而依然是0
    except
      on e:Exception do begin
        WriteLog(AUser.UserName+',添加到在线用户列表失败:'+e.Message);
      end;
    end;
  finally
    cs.Leave;
  end;
end;

解决方案 »

  1.   

    OnlineUser初始时为NIL,使用High返回值是-1.这样子应该好理解吧?
      

  2.   

    if Hign(OnlineUser) = -1 then
     SetLength(OnlineUser,2)
    else
     SetLength(OnlineUser,High(OnlineUser)+2); 估计真正想要的是:if Hign(OnlineUser) = -1 then
     SetLength(OnlineUser,1)
    else
     SetLength(OnlineUser,High(OnlineUser)+1); 
      

  3.   


    Delphi syntax:function High(X);DescriptionIn Delphi code, call High to obtain the upper limit of an Ordinal, Array, or ShortString value. The result type is X, or the index type of X.X is either a type identifier or a variable reference. The type denoted by X, or the type of the variable denoted by X, must be one of the following:For this type High returnsOrdinal type (includes Int64) The highest value in the range of the type
    Array type The highest value within the range of the index type of the array. For empty arrays, High returns –1.
    short string type The declared size of the string
    Open array  The value, of type Integer, giving the number of elements in the actual parameter minus one
    short string parameter The value, of type Integer, giving the number of elements in the actual parameter minus one
      

  4.   

          SetLength(OnlineUser,High(OnlineUser)+2);  //此处+2应该没错吧, 
          OnlineUser[High(OnlineUser)]:=AUser;      // 
          Result:=True; 
          WriteLog('在线用户数:'+IntToStr(High(onlineuser)));    //此处为什么不是1,而依然是0 
     跟下: SetLength(OnlineUser,High(OnlineUser)+2);   這個  high(onlineUser)  是多少??