Adam 5 Posted October 25, 2022 Hi, Just wondering if anyone can please tell me what I'm doing wrong here. Calling VarToInt64 brings up an access violation: var v : Variant; N : Int64; VMGR : TVariantManager; begin v := 1234567890; N := VMGR.VarToInt64(v); end; Thanks & Regards Adam Share this post Link to post
Lajos Juhász 293 Posted October 25, 2022 (edited) It's obsolated do not use it (as it cannot be used). Checked even in XE5 was deprecated. I wonder why it is not removed from the source. Edited October 25, 2022 by Lajos Juhász Share this post Link to post
Adam 5 Posted October 25, 2022 (edited) Yet no mention of that in the help files. Thanks for pointing that out. What's safe to use then Just n := v? Edited October 25, 2022 by Adam Share this post Link to post
David Heffernan 2345 Posted October 25, 2022 (edited) 1 hour ago, Adam said: What's safe to use then Just n := v? Yes Edited October 25, 2022 by David Heffernan 1 Share this post Link to post
Remy Lebeau 1394 Posted October 25, 2022 (edited) 16 hours ago, Adam said: Calling VarToInt64 brings up an access violation: You are instantiating a TVariantManager instance without populating it, so it has unassigned function pointers at the time you are calling VarToInt64(). You would need to call GetVariantManager() first to initialize the record before you can use it, eg: var v : Variant; N : Int64; VMGR : TVariantManager; begin v := 1234567890; if IsVariantManagerSet then begin GetVariantManager(VMGR); N := VMGR.VarToInt64(v); end; end; However, TVariantManager has been deprecated for a very long time (since sometime between D7..D2006, so approx 20 years!), so wherever did you get the idea that you need to use it in the first place? Since at least D2006 (maybe earlier), GetVariantManager() just fills the record with nil pointers, and IsVariantManagerSet() always returns False. In modern coding, simply assign the Variant directly to the Int64 variable and let the compiler handle the conversion for you, eg: var v : Variant; N : Int64; begin v := 1234567890; N := v; // compiler generates: N := System.Variants._VarToInt64(v); end; Edited October 25, 2022 by Remy Lebeau 1 Share this post Link to post
Lajos Juhász 293 Posted October 25, 2022 38 minutes ago, Remy Lebeau said: You would need to call GetVariantManager() first to initialize the record before you can use it, eg: You should check the implementation for that procedure: { ----------------------------------------------------- } { Variant manager support (obsolete) } { ----------------------------------------------------- } procedure GetVariantManager(var VarMgr: TVariantManager); begin FillChar(VarMgr, sizeof(VarMgr), 0); end; This will not fill the record properly. Share this post Link to post
Adam 5 Posted October 25, 2022 Thanks all for your replies. 4 hours ago, Remy Lebeau said: However, TVariantManager has been deprecated for a very long time (since sometime between D7..D2006, so approx 20 years!), so wherever did you get the idea that you need to use it in the first place? I have no idea. I've been with Delphi since v1. I guess every now and then the brain does something funny and brings back something that I did years ago as though it's still applicable. I appreciate the clarification. Thanks all! Share this post Link to post
Remy Lebeau 1394 Posted October 26, 2022 (edited) 7 hours ago, Lajos Juhász said: You should check the implementation for that procedure: I did, actually. Quote This will not fill the record properly. I stated as much in my earlier reply: Quote Since at least D2006 (maybe earlier), GetVariantManager() just fills the record with nil pointers, and IsVariantManagerSet() always returns False. Edited October 26, 2022 by Remy Lebeau Share this post Link to post