Hi,

Instead of set x=nothing does it do the same
thing to do this:

x=""


After all when you set x=nothing you find
that x is still an object.

set x=nothing
msgbox isObject(x) 'still object

Thanks,K

Re: set x=nothing by Shiva

Shiva
Tue Jan 30 00:10:44 CST 2007

x = "" is not the same as x = nothing

But anyway do you close the object before you set it to nothing?
x.close


On 29 Jan., 06:10, "kiley" <x...@y.com> wrote:
> Hi,
>
> Instead of set x=nothing does it do the same
> thing to do this:
>
> x=""
>
> After all when you set x=nothing you find
> that x is still an object.
>
> set x=nothing
> msgbox isObject(x) 'still object
>
> Thanks,K


Re: set x=nothing by mayayana

mayayana
Tue Jan 30 00:15:50 CST 2007

It's kind of ambiguous. In the help it
says this:

"IsObject returns True if expression is a variable
of Object subtype or a user-defined object;
otherwise, it returns False."

If you run the following test you'll get
True, True, True, False

What it shows is that IsObject is only testing whether
it's an object variable. It doesn't test whether the
variable is actually referencing anything. So IsObject
is probably very rarely of any use.

Setting an object to nothing releases the reference
to it. Theoretically, if that was the last reference then the
library will be unloaded. I don't know what x = "" will
do. VBScript might protect you from the mistake.
Hopefully it will dereference the object. Maybe it will
drop it's reference, leaving the object loaded in memory,
and switch the variable to a string. I don't know, but either
way there's no reason to write wrong code.

--------------
test
--------------

Dim FSO, Bool
Set FSO = createobject("Scripting.filesystemobject")

MsgBox isobject(FSO)

If FSO Is Nothing Then
Bool = False
Else
Bool = True
End If

MsgBox Bool

Set FSO = Nothing
MsgBox isobject(FSO)

If FSO Is Nothing Then
Bool = False
Else
Bool = True
End If

MsgBox Bool
------------

> Hi,
>
> Instead of set x=nothing does it do the same
> thing to do this:
>
> x=""
>
>
> After all when you set x=nothing you find
> that x is still an object.
>
> set x=nothing
> msgbox isObject(x) 'still object
>
> Thanks,K
>



Re: set x=nothing by Al

Al
Tue Jan 30 00:40:00 CST 2007


"mayayana" <mayayana1a@mindspring.com> wrote in message
news:qoBvh.16136$pQ3.1400@newsread4.news.pas.earthlink.net...
> It's kind of ambiguous. In the help it
> says this:
>
> "IsObject returns True if expression is a variable
> of Object subtype or a user-defined object;
> otherwise, it returns False."
>
> If you run the following test you'll get
> True, True, True, False
>
> What it shows is that IsObject is only testing whether
> it's an object variable. It doesn't test whether the
> variable is actually referencing anything. So IsObject
> is probably very rarely of any use.
>
> Setting an object to nothing releases the reference
> to it.

The way I look at it, you are not setting an object to nothing, but setting
an object variable (i.e. a variable referencing an object) to nothing. And
the reference that is being released is that of the object that was
instantiated in the variable before it was set to nothing.

IMHO, setting an object variable to nothing is a flag to the scripting host
to dereference the previously existing object, rather than simply change the
variable so it now references a different object. That said, there are some
objects that need not be explicitly dereferenced in this way, one of which
being the nothing object. So you could, theoretically, do this:

set x = createobject(whatever)
set x = nothing
set x = ""

without causing anything weird to happen. But remove the "set x = nothing",
and the havoc that will fly will depend on what kind of object was just
created and what state it was in.

/Al

> Theoretically, if that was the last reference then the
> library will be unloaded. I don't know what x = "" will
> do. VBScript might protect you from the mistake.
> Hopefully it will dereference the object. Maybe it will
> drop it's reference, leaving the object loaded in memory,
> and switch the variable to a string. I don't know, but either
> way there's no reason to write wrong code.
>
> --------------
> test
> --------------
>
> Dim FSO, Bool
> Set FSO = createobject("Scripting.filesystemobject")
>
> MsgBox isobject(FSO)
>
> If FSO Is Nothing Then
> Bool = False
> Else
> Bool = True
> End If
>
> MsgBox Bool
>
> Set FSO = Nothing
> MsgBox isobject(FSO)
>
> If FSO Is Nothing Then
> Bool = False
> Else
> Bool = True
> End If
>
> MsgBox Bool
> ------------
>
> > Hi,
> >
> > Instead of set x=nothing does it do the same
> > thing to do this:
> >
> > x=""
> >
> >
> > After all when you set x=nothing you find
> > that x is still an object.
> >
> > set x=nothing
> > msgbox isObject(x) 'still object
> >
> > Thanks,K
> >
>
>



Re: set x=nothing by Alexander

Alexander
Tue Jan 30 03:51:19 CST 2007

kiley schrieb:
> Hi,
>
> Instead of set x=nothing does it do the same
> thing to do this:
>
> x=""

Internally
x = ""
does at least two things, if x referenced to an object before.

First, it releases the object-reference, decrementing the object's
internal ref-counter (and fully destructing the object
it if there is no reference left).

Second, it assigns a new value to the variable and thereby changing
its variant-subtype, e.g the vt-member of the Variant-struct,
from object to string.


> After all when you set x=nothing you find
> that x is still an object.
>
> set x=nothing
> msgbox isObject(x) 'still object

In terms of VBS and COM, 'Nothing' is an object
since in VBS it is a Variant, whose vt-member is set
to VT_DISPATCH and VT_BYREF, independendly of the data
it contains.


MfG,
Alex

Re: set x=nothing by mayayana

mayayana
Tue Jan 30 09:42:28 CST 2007

I imagine you may be confused by now. :)
But the three answers here are all saying basically
the same thing. It's more confusing because
it's so easy. When you use CreateObject, the
script host has to load the actual file for that
into memory. When you set the object to nothing
(or the object variable, as Al points out) you're
telling the host you're through with the object.

The "ref-counting" is the process that happens
behind the scenes, whereby the number of references
to an object are tracked, so that when the last
reference is released the file can be unloaded
from memory.

None of that is very clear because VBScript tries
to hide all the work and make it easy. In VBS you
can usually get away with ignoring the whole issue,
and many people do because they haven't been
encouraged to understand it. So the idea of setting
things to Nothing ends up seeming like a vague,
theoretical idea that doesn't really affect code. One
of the official Microsoft script experts even wrote
a well known article wherein he adamantly insists
that using "Set x to Nothing" is a problem!

http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx

His position is a bit irresponsible, because it relies
on the WSH to be absolutely foolproof and discourages
script users from understanding the inner workings, but
it's in line with the attempt to make script easy and
accessible to people without them needing to understand
how it works.

> Instead of set x=nothing does it do the same
> thing to do this:
>
> x=""
>
>
> After all when you set x=nothing you find
> that x is still an object.
>
> set x=nothing
> msgbox isObject(x) 'still object
>
> Thanks,K
>



Re: set x=nothing by kiley

kiley
Tue Jan 30 15:51:47 CST 2007

Ok thanks.

By the way what I am working with is
control range objects. I noticed that
even when the object's outer html
is deleted from the html it's properties
still remain, for instance width,
height, position.

Bye,
Kiley

Re: set x=nothing by Michael

Michael
Tue Jan 30 18:44:15 CST 2007

> ... I don't know what x = "" will
> do. VBScript might protect you from the mistake.
> Hopefully it will dereference the object.

If x is a real object reference and that object has a default property that
will accept an empty string as a value (possibly after coercion), then
that's what will happen in the best (depending on expectation ;-) case .
Otherwise a runtime error is most likely to be thrown.

> ... Maybe it will
> drop it's reference, leaving the object loaded in memory,
> and switch the variable to a string. I don't know, but either
> way there's no reason to write wrong code.

Very good advice (as always)...

--
Michael Harris
Microsoft.MVP.Scripting



Re: set x=nothing by mayayana

mayayana
Tue Jan 30 19:19:22 CST 2007


>
> By the way what I am working with is
> control range objects. I noticed that
> even when the object's outer html
> is deleted from the html it's properties
> still remain, for instance width,
> height, position.
>
I don't understand that. "Control range
objects"? Maybe TextRange?



Re: set x=nothing by kiley

kiley
Tue Jan 30 23:48:27 CST 2007


"mayayana" wrote in message
>> By the way what I am working with is
>> control range objects. I noticed that
>> even when the object's outer html
>> is deleted from the html it's properties
>> still remain, for instance width,
>> height, position.
>>
> I don't understand that. "Control range
> objects"? Maybe TextRange?
>

Yes there is such a thing. . . oval,
rectangle,
polyline, etc. or images, or other controls
such as textarea, select list box, button,
etc. etc. are all controls and can be
selected. K.


Re: set x=nothing by kiley

kiley
Wed Jan 31 09:49:01 CST 2007




"Alexander Mueller" <millerax@hotmail.com>
wrote in message
news:45c06ff9$0$27625$9b4e6d93@newsspool2.arcor-online.net...
> 31.01.2007 06:48, kiley schrieb:
>> "mayayana" wrote in message
>>>> By the way what I am working with is
>>>> control range objects. I noticed that
>>>> even when the object's outer html
>>>> is deleted from the html it's
>>>> properties
>>>> still remain, for instance width,
>>>> height, position.
>>>>
>>> I don't understand that. "Control
>>> range
>>> objects"? Maybe TextRange?
>>>
>>
>> Yes there is such a thing. . . oval,
>> rectangle,
>> polyline, etc. or images, or other
>> controls
>> such as textarea, select list box,
>> button,
>> etc. etc. are all controls and can be
>> selected. K.
>
>
> If this is HTML or XML you may use the
> HTML a/o XML-DOM to remve
> an element, browse MSDN for methods named
> removeNode and removeChild
>
> Setting an object to nothing usually only
> removes a programatical
> refernce to the object. As for controls
> and GUI items in general
> setting it to nothing doesn't remove it
> 'physically'.
>
>
> MfG,
> Alex
>

I know. But try this. Shows that you can
undelete an element after you delete it. K.

set R=document.body.createcontrolrange
R.add in1
msgbox R(0).outerHtml
in1.removeNode true
msgbox R(0).outerHtml 'still there

<input id=in1 type=text value="hello world">


Re: set x=nothing by Alexander

Alexander
Wed Jan 31 11:31:11 CST 2007

kiley schrieb:
>
> "Alexander Mueller" <millerax@hotmail.com> wrote in message

>> 31.01.2007 06:48, kiley schrieb:

>>>>> By the way what I am working with is
>>>>> control range objects. I noticed that
>>>>> even when the object's outer html
>>>>> is deleted from the html it's properties
>>>>> still remain, for instance width,
>>>
>>> Yes there is such a thing. . . oval,
>>> rectangle,
>>> polyline, etc. or images, or other controls
>>> such as textarea, select list box, button,
>>> etc. etc. are all controls and can be
>>> selected. K.
>>
>>
>> If this is HTML or XML you may use the HTML a/o XML-DOM to remve
>> an element, browse MSDN for methods named removeNode and removeChild
>>
>> Setting an object to nothing usually only removes a programatical
>> refernce to the object. As for controls and GUI items in general
>> setting it to nothing doesn't remove it 'physically'.

>
> I know. But try this. Shows that you can
> undelete an element after you delete it. K.
>
> set R=document.body.createcontrolrange
> R.add in1
> msgbox R(0).outerHtml
> in1.removeNode true

removeNode requires an argument that tells which
node to remove from the parent.

If you want to remove the inputbox from the
controlrange, the syntax is:

R.remove 0

if 0 is the index of the inputbox amoung the
items of the controlrange.

if you want to remove the inputbox from the DOM the syntax
is afaik sth like

oForm.removeNode in1
or
document.body.removeNode in1

depends on the HTML

I can't tell if the element is also automatically removed
from the ctrl-range aswell, if you remove it from the DOM,
you'll have to give it a try.


MfG,
Alex



> msgbox R(0).outerHtml 'still there
>
> <input id=in1 type=text value="hello world">

Re: set x=nothing by Paul

Paul
Wed Jan 31 13:52:35 CST 2007


"Alexander Mueller" <millerax@hotmail.com> wrote in message
news:45c06ff9$0$27625$9b4e6d93@newsspool2.arcor-online.net...
> 31.01.2007 06:48, kiley schrieb:
>> "mayayana" wrote in message
>>>> By the way what I am working with is
>>>> control range objects. I noticed that
>>>> even when the object's outer html
>>>> is deleted from the html it's properties
>>>> still remain, for instance width,
>>>> height, position.
>>>>
>>> I don't understand that. "Control range
>>> objects"? Maybe TextRange?
>>>
>>
>> Yes there is such a thing. . . oval,
>> rectangle,
>> polyline, etc. or images, or other controls
>> such as textarea, select list box, button,
>> etc. etc. are all controls and can be
>> selected. K.
>
>
> If this is HTML or XML you may use the HTML a/o XML-DOM to remve
> an element, browse MSDN for methods named removeNode and removeChild
>
> Setting an object to nothing usually only removes a programatical
> refernce to the object. As for controls and GUI items in general
> setting it to nothing doesn't remove it 'physically'.
>
>
> MfG,
> Alex

Here is a example that demonstrates use of the control range collection by
JScript:
http://msdn.microsoft.com/workshop/samples/author/dhtml/collections/controlrange.htm



Re: set x=nothing by kiley

kiley
Wed Jan 31 17:17:27 CST 2007


"Alexander Mueller" > if you want to remove
the inputbox from the DOM the syntax
> is afaik sth like
>
> oForm.removeNode in1
> or
> document.body.removeNode in1

This is incorrect syntax. Correct is the way
I had it.

ObjectToBeRemoved.removeNode

K.


Re: set x=nothing by Anthony

Anthony
Sat Feb 03 16:26:10 CST 2007


"mayayana" <mayayana1a@mindspring.com> wrote in message
news:EHJvh.16909$yx6.7017@newsread2.news.pas.earthlink.net...
>
> http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx
>
> His position is a bit irresponsible, because it relies
> on the WSH to be absolutely foolproof and discourages
> script users

<tonge-in-cheek>

Whereas when we explicit use Set to nothing we are assured that VBScript is
foolproof in implementing that line of code and that the resource will
correctly be released.

And since VBScript can not be considered foolproof we also assign empty to
all variables that currently contain strings to ensure that the memory
allocated by these is released.

Additional when for eaching a collection containing objects we use set to
nothing just before the Next in case VBScript can't be trusted to release
the references to objects acquired from one iteration to next.

We will also delete the With block from the VBScript programmers toolbox
altogether.

</tonge-in-cheek>