Data只是一个指针,建议先分配,后用。最好不要像你怎样直接取别的地址。

解决方案 »

  1.   

    data的类型是tobject,是指针,可以当做一个整数,只要赋值时进行强制转换data:=tobject(i)i:=integer(data)
      

  2.   

    var i: ^Integer;
        j: Integer;i^ := 2;
    Node.Data := i;j := Integer(Node.Data);
      

  3.   

    i在这里是全局的静态变量,没事的,主要是想知道怎么写法啊!它报错:
    [Error] XXX.pas(84): Incompatible types: 'Integer' and 'Char'
      

  4.   

    看看Delphi 6自带的例子:
    The following code defines a record type of TMyRec and a record pointer type of PMyRec.type
    PMyRec = ^TMyRec;
    TMyRec = record
      FName: string;
      LName: string;
    end;Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value.procedure TForm1.Button1Click(Sender: TObject);var
      MyRecPtr: PMyRec;
      TreeViewIndex: LongInt;
    begin
      New(MyRecPtr);
      MyRecPtr^.FName := Edit1.Text;
      MyRecPtr^.LName := Edit2.Text;
      TreeViewIndex := StrToInt(Edit3.Text);
      with TreeView1 do
      begin
        if Items.Count = 0 then
          Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
        else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then      Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
      end;
    end;After an item containing a TMyRec record has been added, the following code retrieves the FName and LName values associated with the item and displays the values in a label.procedure TForm1.Button2Click(Sender: TObject);begin
      Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
                      PMyRec(TreeView1.Selected.Data)^.LName;
    end;
    也许你就有正确的想法了。
      

  5.   

    可能是这么写吧,
    j := Integer(node.data)^.I
    bigfoolcat(大笨猫)的建议是delphi文档里的建议。
    这样对于指针的分配,释放都比较好控制。
    如果你只想加一个标识,可以用node.itemid唯一标识一个node
      

  6.   

    data只不过是个指针,你把当做整数用没什么关系,只要进行强制类型转换
      

  7.   

    data是个指针,要存放东西,必须先分配空间,用完后释放
    用的时候根据具体的数值类型,强制转换!!
    :):)
      

  8.   

    j := integer(node.data)^.I
    可能是这么写,我也不清楚
    其实还是应该像delphi里建议的那么做,分配一个结构的指针。
    如果你只是想加一个表示用node.itemid可以唯一表示一个node
      

  9.   

    呵呵, 是这样的,如下:var
      MyData: Integer;
      pMyData: ^Integer;
      TreeNode.Data := MyData;  MyData := TreeNode.Data或
      TreeNode.Data := pMyData;
      
      pMyData := TreeNode.data或
      TreeNode.Data := pMyData^;
      pMyData^ := TreeNode.Data归根到底,Data属性只保存数据,至于是什么数据它不管,可以是个指针,也可以是个整数, 存进去后,取出来还是原样!
      

  10.   

    所以,你可以改为
    node : TTreenode;node.Data := @i;  //i为integer型 ,值 为2
    j := PInteger(node.Data)^;  //取出node.data里的指针里的值给j, 是为integer型
      

  11.   

    指针引用的位置不对,data是指针,不是node,应该这样
    Integer(node.data^)