如何将一个记录类型转换成Variant类型.
这是在Com的连接点的时候出现的问题。在D5中在类型库中声明的记录类型的参数
当把把类型库作为事件源的时候产生的代码为Variant我应该如何转换呢?
TARec=record
   a:widestring;
   b:widestring;
   c:widestring;
end
这样的一个变量ARec如何转换成Variant呢?

解决方案 »

  1.   

    TVar=record
       a:variant;
       b:variant;
       c:variant;
    end;
    TARec:=TVar;
      

  2.   

    我需要的是
    v:Variant;
    我要用v来表示ARec
      

  3.   

    By default, Variants can hold values of any type except records, sets, static arrays, files, classes, class references, and pointers. In other words, variants can hold anything but structured types and pointers. They can hold interfaces, whose methods and properties can be accessed through them. (See Object interfaces.) They can hold dynamic arrays, and they can hold a special kind of static array called a variant array. (See Variant arrays.) Variants can mix with other variants and with integer, real, string, and Boolean values in expressions and assignments; the compiler automatically performs type conversions.
      

  4.   

    变通办法:TYourRecord=class
        a:Integer;
        b:Integer;
      end;
    TYourVarData = packed record
      VType: TVarType;
      Reserved1, Reserved2, Reserved3: Word;
      VComplex: TYourRecord;
      Reserved4: LongInt;
    end;var
     d:TYourVarData;
     v:Variant; v:=Variant(d);
      

  5.   

    TARec=record
       a:widestring;
       b:widestring;
       c:widestring;
    end
    //  TARec ---> Variant
    function RecordToVariant(value:TARec):OleVariant;
    var
      P:Pointer;
    begin
      Result := VarArrayCreate([0,sizeof(TARec)],varByte);
      P      := VarArrayLock(Result);
      Move(value,P^,sizeof(P));
      VarArrayUnlock(Result);
    end;
    // OleVariant ----> TARec
    function VariantToRecord(value:OleVariant):TARec;
    var
      P:Pointer;
    begin
      P:= VarArrayLock(value);
      Move(P^,Result,sizeof(TARec));
      VarArrayUnlock(value);
    end;
      

  6.   

    lifejoy(活着的穿马甲中) :
      用 dolphin2001(抢包山) 的办法即可,不过TARec必须是ShortString类型
    TARec=record
       a:ShortString;
       b:ShortString;
       c:ShortString;
    end