Discussion:
OLE Automation & Unassigned Constant
(too old to reply)
Jon Robertson
2005-02-16 16:52:08 UTC
Permalink
I just came across this in the Delphi 6 Help for the Unassigned
constant. I feel like I'm missing something, because this disputes
something I thought I understood about Variants and Automation for a
long time...

"You can make a variant unassigned by assigning the Unassigned constant
to it. *This is useful with variants referencing OLE Automation Objects
that you want to keep "alive" until another value is assigned to the
variant.*"

Given the following code:

var
WordApp: OleVariant; // long lifetime, say a class field

begin
WordApp := CreateOleObject('Word.Application');
// blah blah
WordApp.Quit;
WordApp := Unassigned; // What happens here?

What's the difference between
WordApp := Unassigned;
and
WordApp := Null;

I know the difference between a Null value and Unassigned. But what's
the difference to OLE Automation? Does one hold onto resources where
the other doesn't?

I feel like a 1st grader all over again...
--
Jon Robertson
Borland Certified Advanced Delphi 7 Developer
MedEvolve, Inc
http://www.medevolve.com
Finn Tolderlund
2005-02-17 07:58:37 UTC
Permalink
Post by Jon Robertson
"You can make a variant unassigned by assigning the Unassigned constant
to it. *This is useful with variants referencing OLE Automation Objects
that you want to keep "alive" until another value is assigned to the
variant.*"
I know the difference between a Null value and Unassigned. But what's
the difference to OLE Automation? Does one hold onto resources where
the other doesn't?
The help could have been more clear. It easy to misunderstand it,, due to
the way it's written.
But there is a clue how to interpret what the help file says, at least in
Delphi 7.
Your quote probably comes from the topic "Unassigned function".
If you click at the "Delphi example" you can see this explanation:
"In the code fragment shown below, the statement that assigns Unassigned to
the MSWord variable causes the OLE Automation Object that was created to
interface with Word to be released."
So assigning Unassigned does NOT keep the OLEAO alive.
It works as you would expect by releasing it.
--
Finn Tolderlund
George Birbilis
2005-02-17 16:27:52 UTC
Permalink
Post by Jon Robertson
What's the difference between
WordApp := Unassigned;
and
WordApp := Null;
I know the difference between a Null value and Unassigned. But what's
the difference to OLE Automation? Does one hold onto resources where
the other doesn't?
I feel like a 1st grader all over again...
Null is special, targets mostly databases I think

Unassigned on the other hand is to make a variant hold nothing anymore.
However I think there's also a varEmpty constant? (to pass to automation
procs that take optional params when you call them using early binding -
this just adds to the confustion of course). Probably varEmpty is different
from Unassigned in that it does hold something (the "empty" value :-o

also see DbNull constant and isDbNull function instroduced in VB.NET to
separate from VB's Null and IsNull in case there's more explanation there.
I think the following is true:
1) VB.net's DbNull is same as Delphi's Null
2) VB.net's Null is same as Delphi's Unassigned
please correct if I'm wrong

-----
George Birbilis (***@kagi.com)
Microsoft Most Valuable Professional
MVP J# for 2004 & 2005
http://www.kagi.com/birbilis
--------------
George Birbilis
2005-02-18 11:31:48 UTC
Permalink
I meant EmptyParam here
Post by George Birbilis
However I think there's also a varEmpty constant? (to pass to automation
procs that take optional params when you call them using early binding -
this just adds to the confustion of course). Probably varEmpty is
different from Unassigned in that it does hold something (the "empty"
value :-o
Loading...