2015-09-05 104 views
1

我有一個類TRate降序從IDispatchTAutoObject傳遞對象作爲OleVariant

Type 
    IRate = interface(IDispatch) 
    ['{60B5A5D1-5BA9-4D28-BBF9-DD5BE2B83ED2}'] 
    function Get_Minimum: Double; safecall; 
    procedure Set_Minimum(Value: Double); safecall; 
    function Get_Maximum: Double; safecall; 
    procedure Set_Maximum(Value: Double); safecall; 
    property Minimum: Double read Get_Minimum write Set_Minimum; 
    property Maximum: Double read Get_Maximum write Set_Maximum; 
end; 

TRate = class(TAutoObject, IRate) 
protected 
    function Get_Maximum: Double; safecall; 
    function Get_Minimum: Double; safecall; 
    procedure Set_Maximum(Value: Double); safecall; 
    procedure Set_Minimum(Value: Double); safecall; 
public 
    fMaximum,fMinimum:double; 
    destructor Destroy; override; 
end; 


//INSIDE mscorlib_tlb.pas 
// *********************************************************************// 
// Interface : IEnumerator 
// Indicateurs : (4416) Dual OleAutomation Dispatchable 
// GUID :  {496B0ABF-CDEE-11D3-88E8-00902754C43A} 
// *********************************************************************// 
IEnumerator = interface(IDispatch) 
['{496B0ABF-CDEE-11D3-88E8-00902754C43A}'] 
    function MoveNext: WordBool; safecall; 
    function Get_Current: OleVariant; safecall; 
    procedure Reset; safecall; 
    property Current: OleVariant read Get_Current; 
end; 

TRateEnum = class(TAutoObject, IEnumerator) 
protected 
    function Get_Current: OleVariant; safecall; 
    function MoveNext: WordBool; safecall; 
    procedure Reset; safecall; 
public 
    fAxe,fCount,fIndex:integer; 
end; 

我有TRate類型的對象的下一個g_Rate陣列。我必須通過IEnumerator訪問g_Rate成員。在IEnumerator的非標準功能Get_Current,看起來像(返回OleVariant):

Type 
    g_Rate:Array [0..2,0..2] of TRate; 
end 

function TRateEnum.Get_Current: OleVariant; 
begin 
    result:= g_Rate[i,j]; 
end; 

我得到一個編譯錯誤,「類型missmatch,Olevariant TRate」,我怎麼能避免這個問題? Regards

+0

您是否需要將TRate轉換爲IRate。你爲什麼不顯示g_Rate的聲明? [mcve]會有所幫助。知道你正在做什麼類型的COM可能會有用。 –

+0

你能告訴我們你是如何聲明TRateEnum的嗎?這裏有一個很好的例子來說明如何在這裏定義枚舉器:http://stackoverflow.com/questions/32416017/pass-an-object-as-olevariant – uri2x

+0

我已經找到了一個解決方案,答案是David.'function TRateEnum.Get_Current :OleVariant; 開始 結果:= g_Rate [i,j]作爲IRate; end;',但我不知道它爲什麼工作。我對使用接口不太熟悉。我學習,我學習。 – michastro

回答

0

您正在實現一個返回COM變體的COM方法。 OleVariant類型用於表示COM變體。來自documentation

OleVariant是一個OLE兼容的變體。 Variant和OleVariant之間的主要區別在於Variant可以包含只有當前應用程序知道如何處理的數據類型。 OleVariant只能包含定義爲與OLE自動化兼容的數據類型,這意味着包含的數據類型可以在程序之間或通過網絡傳遞,而不用擔心另一端是否知道如何處理數據。

你正試圖把一個Delphi類到OleVariant,而被阻止,因爲德爾福類不Automation類型。相反,您需要將自動化接口放入您返回的OleVariant

function TRateEnum.Get_Current: OleVariant; 
begin 
    Result := g_Rate[i,j] as IRate; 
end;