无效的变体类型错误德尔福2010年 [英] Invalid variant type error Delphi 2010

查看:196
本文介绍了无效的变体类型错误德尔福2010年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// interface

    iccItem =
        class
            ID   : String;
            DATA : Variant;
            constructor Create( _id : String; _data : Variant);
        end;

    iccDynamicObject =
        class
             private
                 FItems : TList;
                 function locate( _id : String) : iccItem;
             public
                 constructor Create();
                 destructor Destroy(); override;
             public
                 procedure define( _id : String; _dta : Variant);
                 //function get( _ndx : DWORD)  : Variant; overload;// link to original data
                 function get( _id  : String) : Variant; overload;
             public
                 property Items[_id : String] : Variant read get write define; default;
        end;


// implementation

{ iccDynamicObject }

constructor iccItem.Create( _id : String; _data : Variant);
begin
    ID   := _id;
    DATA := _data;
end;

function iccDynamicObject.locate( _id : String) : iccItem;
var ndx : integer;
    tmp : iccItem;
begin
    result := nil;

    for ndx := 0 to FItems.Count - 1 do
        begin
            tmp := iccItem( FItems[ndx]);
            if tmp.ID = _id
                then begin
                         result := tmp;
                         exit;
                     end;
        end;
end;

constructor iccDynamicObject.Create();
begin
    FItems := TList.Create();
end;

destructor iccDynamicObject.Destroy();
begin
    {$MESSAGE 'clear here'}

    FItems.Destroy();
    inherited;
end;

procedure iccDynamicObject.define( _id : String; _dta : Variant);
var tmp : iccItem;
begin
    tmp := locate( _id);
    if tmp = nil
        then FItems.Add( iccItem.Create( _id, _dta) )
        else tmp.DATA := _dta;
end;

//function iccDynamicObject.get( _ndx : DWORD) : Variant;
//begin
//    result.vPointer := nil;
//end;

function iccDynamicObject.get( _id : String) : Variant;
var tmp : iccItem;
begin
    tmp := locate( _id);
    if tmp = nil
        then result.vaNull := true
        else result := locate( _id).DATA;
end;


// using
procedure TForm1.FormCreate(Sender: TObject);
var c : iccDynamicObject;
begin
    c := iccDynamicObject.Create;

    c['asd'] := 123;

    c.Destroy;
end;

在delphi 2010中设置断点为iccDynamicObject.define() - > tmp:= locate(_id);
将导致@ Project Project1.exe引发异常类EVariantBadVarTypeError,并显示消息'Invalid variant type'。 @

Set breakpoint in DELPHI 2010 at iccDynamicObject.define() -> tmp := locate( _id); will cause @Project Project1.exe raised exception class EVariantBadVarTypeError with message 'Invalid variant type'.@

DELPHI 7,并没有遇到这个问题!

Code was tested in DELPHI 7, and this problem was not encountered!

ps。代码被重写为delphi-7风格,没有类内类型来演示一个问题...

ps. code was rewritten in delphi-7 style without in-class types for demonstrating a problem...

已解决 - >不要使用类内泛型,例如

SOLVED -> Do not use in-class generic types, such as

classDef<_type> = 
    class
        type
            // this
            internalClass<_anotherType> =
                class
                    private
                        FSomething : _anotherType;
                end;
            // or this one
            internalClass2 =
                class
                    private
                        FSomething : _type;
                end;
        private
            FInternalClass  : internalClass<_type>;
            FInternalClass2 : internalClass;
    end;

这样的事情将促使调试器或编译器执行UNEXCECTED THINGS !!!
代码编译并正常工作。但在我的情况下,随着单位增长代码变得不稳定
,并强迫我做一些代码重构,只是一点点,但不仅仅是麻烦......

Such things will procure debugger or compiler to do UNEXCECTED THINGS!!! Code compiles and work correctly. But in my case, with Unit growth code become unstable and coerce me to make some code-refactoring, just a little, but more than inconvenient...

你注意到:)))

推荐答案

这是D2010中的一个已知错误,它已被在QualityCentral中报告并在XE中修复。

This is a known bug in D2010 which has been reported in QualityCentral and fixed in XE.

这篇关于无效的变体类型错误德尔福2010年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆