全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1811
推到 Plurk!
推到 Facebook!

UnTyped Parameter 的判斷

答題得分者是:william
Nelson Lo
一般會員


發表:35
回覆:87
積分:24
註冊:2003-04-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-04-10 11:18:39 IP:61.221.xxx.xxx 未訂閱
請教各位 我在函數UnTyped_Test中宣告一個UnTyped 的參數 如果我想要先判斷傳過來的參數是哪種Type (integer or string 等等) 我用 if (intPar is String) then ...... 可是編譯會error 能否指點一下 謝謝 procedure TForm1.Button1Click(Sender: TObject); var MyParameter : String; begin MyParameter := '123'; UnTyped_Test(MyParameter); end; procedure TForm1.UnTyped_Test(var MyPar); begin if (intPar is String) then ShowMessage(String(intPar)); end;
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-04-10 11:32:41 IP:147.8.xxx.xxx 未訂閱
Quoted from Delphi help:    Within a procedure or function body, untyped parameters are incompatible with every type. To operate on an untyped parameter, you must cast it. In general, the compiler cannot verify that operations on untyped parameters are valid.    So the compiler never know the type of an untyped parameter. BTW, is is used for object/class/interface. If you don't mind the performance, you could use variant, e.g.
procedure TForm1.Button1Click(Sender: TObject);
var
    MyParameter: Variant;
begin
    MyParameter := '123';
    UnTyped_Test(MyParameter);
    MyParameter := 1.23;
    UnTyped_Test(MyParameter);
end;    procedure TForm1.UnTyped_Test(var MyPar: Variant);
begin
    if VarIsStr(MyPar) then
        ShowMessage('string')
    else if VarIsFloat(MyPar) then
        ShowMessage('float');
end;
Nelson Lo
一般會員


發表:35
回覆:87
積分:24
註冊:2003-04-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-04-10 11:50:08 IP:61.221.xxx.xxx 未訂閱
謝謝您的回答 若是用Variant的話 我必須宣告var MyParameter: Variant; 才能傳Variant 型態的參數 但是如果我是寫成DLL要給VB 或 VC 等其他語言用的話 那VB 或 VC 要宣告何種 type 呢 還有我在Help中查不到 VarIsStr,VarIsFloat這個function ㄝ
Nelson Lo
一般會員


發表:35
回覆:87
積分:24
註冊:2003-04-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-04-10 11:57:45 IP:61.221.xxx.xxx 未訂閱
再請教一下 在VB中有一種 DataType 可宣告成 as any 例如 Declare Function viGetAttribute Lib "VISA32.DLL" Alias "#133" (ByVal vi As Long, ByVal attrName As Long, attrValue As Any) As Long 那我如果要用delphi呼叫此function的話 參數attrValue 我要宣告成何種type variant 可以嗎
Nelson Lo
一般會員


發表:35
回覆:87
積分:24
註冊:2003-04-04

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-04-10 11:58:20 IP:61.221.xxx.xxx 未訂閱
再請教一下 在VB中有一種 DataType 可宣告成 as any 例如 Declare Function viGetAttribute Lib "VISA32.DLL" Alias "#133" (ByVal vi As Long, ByVal attrName As Long, attrValue As Any) As Long 那我如果要用delphi呼叫此function的話 參數attrValue 我要宣告成何種type variant 可以嗎
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-04-10 12:17:07 IP:147.8.xxx.xxx 未訂閱
引言:再請教一下 在VB中有一種 DataType 可宣告成 as any 例如 Declare Function viGetAttribute Lib "VISA32.DLL" Alias "#133" (ByVal vi As Long, ByVal attrName As Long, attrValue As Any) As Long 那我如果要用delphi呼叫此function的話 參數attrValue 我要宣告成何種type variant 可以嗎
I guess Variant or OleVariant will do it. But I don't have VB (and hence less experience with VB), if it is not varaint, then I guess it is some kind of pointer/untype parameter and you will need the information in vi and attrName to type cast it. If you cannot find VarIsFloat, etc. you can use VarType function and check. P.S. I am using Delphi 7 and VasIsxxx are in the Variants unit.
Nelson Lo
一般會員


發表:35
回覆:87
積分:24
註冊:2003-04-04

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-04-10 12:37:43 IP:61.221.xxx.xxx 未訂閱
謝謝您 其實是有一個大概是C寫的DLL的API C Syntax ViStatus viGetAttribute(ViObject vi, ViAttr attribute,void * ttrState) Visual Basic Syntax viGetAttribute&(ByVal vi&, ByVal attribute&, attrState as Any) 現在我要用Delphi去Call 我想 as any 應該就是delphi的 variant 不過還是得實際試試
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-04-10 13:58:38 IP:147.8.xxx.xxx 未訂閱
According to your C prototype, ttrState should be a pointer 
Nelson Lo
一般會員


發表:35
回覆:87
積分:24
註冊:2003-04-04

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-04-10 14:03:42 IP:61.221.xxx.xxx 未訂閱
無論如何 謝謝您熱心的解答
系統時間:2024-05-04 5:29:59
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!