how to return a "null" object 
Author Message
 how to return a "null" object

I am writing ASP code with VBScript.
I have a function that normally returns an object.  For example a
"Find" function would return the object found.
Sometimes I want the function to return an indication that nothing was
found.
On the caller side, the returned value is assigned to a variable, and
tested for the special indicator.

I can not find an indicator that will work --

I) I have tried returning "nothing":

1   function Func
2     set Func = nothing
3   end function
4
5   dim obj, a
6   set obj = Func
7   if obj = nothing then
8     a = 1
9   else
10    a = 2
11  end if

This gives the error "Object variable not set" at line 7.
I have also tried for line 7  IsEmpty(obj)  which returns False even
though obj is "nothing".

II) I have also tried returning "null"

1   function Func
2     Func = null
3   end function
4
5   dim obj, a
6   set obj = Func
7   if IsNull(obj) then
8     a = 1
9   else
10    a = 2
11  end if

This gives an error at line 6 because of the keyword "set".  Removing
"set" will cause an error when Func actually does return an object.

Can anyone give me a solution to this problem?

  Thank you,
  Paul



Wed, 02 Mar 2005 04:03:32 GMT  
 how to return a "null" object
As documented in If...Then...Else Statement:
If objectname is objecttype then...

1   function Func
2     set Func = nothing
3   end function
4
5   dim obj, a
6   set obj = Func
7   if obj is nothing then
8     a = 1
9   else
10    a = 2
11  end if

regards,
--
Gurgen Alaverdian
http://www.gurgensvbstuff.com

Quote:

> I am writing ASP code with VBscript.
> I have a function that normally returns an object.  For example a
> "Find" function would return the object found.
> Sometimes I want the function to return an indication that nothing was
> found.
> On the caller side, the returned value is assigned to a variable, and
> tested for the special indicator.

> I can not find an indicator that will work --

> I) I have tried returning "nothing":

> 1   function Func
> 2     set Func = nothing
> 3   end function
> 4
> 5   dim obj, a
> 6   set obj = Func
> 7   if obj = nothing then
> 8     a = 1
> 9   else
> 10    a = 2
> 11  end if

> This gives the error "Object variable not set" at line 7.
> I have also tried for line 7  IsEmpty(obj)  which returns False even
> though obj is "nothing".

> II) I have also tried returning "null"

> 1   function Func
> 2     Func = null
> 3   end function
> 4
> 5   dim obj, a
> 6   set obj = Func
> 7   if IsNull(obj) then
> 8     a = 1
> 9   else
> 10    a = 2
> 11  end if

> This gives an error at line 6 because of the keyword "set".  Removing
> "set" will cause an error when Func actually does return an object.

> Can anyone give me a solution to this problem?

>   Thank you,
>   Paul



Wed, 02 Mar 2005 04:48:50 GMT  
 how to return a "null" object
Gurgen,
  Thank you for your reply.  However you must have missed, in my
original post, where I said, this code gives an error at line 7 --
Quote:
> > This gives the error "Object variable not set" at line 7.

  Paul
Quote:

> As documented in If...Then...Else Statement:
> If objectname is objecttype then...

> 1   function Func
> 2     set Func = nothing
> 3   end function
> 4
> 5   dim obj, a
> 6   set obj = Func
> 7   if obj is nothing then
> 8     a = 1
> 9   else
> 10    a = 2
> 11  end if

> regards,
> --
> Gurgen Alaverdian
> http://www.gurgensvbstuff.com

> > I am writing ASP code with VBscript.
> > I have a function that normally returns an object.  For example a
> > "Find" function would return the object found.
> > Sometimes I want the function to return an indication that nothing was
> > found.
> > On the caller side, the returned value is assigned to a variable, and
> > tested for the special indicator.

> > I can not find an indicator that will work --

> > I) I have tried returning "nothing":

> > 1   function Func
> > 2     set Func = nothing
> > 3   end function
> > 4
> > 5   dim obj, a
> > 6   set obj = Func
> > 7   if obj = nothing then
> > 8     a = 1
> > 9   else
> > 10    a = 2
> > 11  end if

> > This gives the error "Object variable not set" at line 7.
> > I have also tried for line 7  IsEmpty(obj)  which returns False even
> > though obj is "nothing".

> > II) I have also tried returning "null"

> > 1   function Func
> > 2     Func = null
> > 3   end function
> > 4
> > 5   dim obj, a
> > 6   set obj = Func
> > 7   if IsNull(obj) then
> > 8     a = 1
> > 9   else
> > 10    a = 2
> > 11  end if

> > This gives an error at line 6 because of the keyword "set".  Removing
> > "set" will cause an error when Func actually does return an object.

> > Can anyone give me a solution to this problem?

> >   Thank you,
> >   Paul



Fri, 04 Mar 2005 21:23:14 GMT  
 how to return a "null" object
Paul,
I did not miss it. The way you are trying to evaluate the object "if obj = nothing" is wrong.
Use "if obj is nothing" or could also try If TypeName(obj) = "Nothing"

Consider:
-------------------------------------------------------------------------
function Func(eval)
Select Case eval
 Case 1 set Func = CreateObject("Scripting.FileSystemObject")
 Case 2 set Func = new regexp
 Case 3 set Func = nothing
End Select
end function

dim obj, a
For i=1 to 3
set obj = Func(i)
if obj is Nothing then
  Wscript.Echo "The Object Type is: " & TypeName(obj)
  Wscript.Echo "Nothing Else to Do..."
else Wscript.echo "The Object Type is: " & TypeName(obj)
   Wscript.Echo "Continue execution...." & vbcrlf
end if
Next
-----------------------------------------------------------------------

regards,
--
Gurgen Alaverdian
http://www.gurgensvbstuff.com

Quote:

> Gurgen,
>   Thank you for your reply.  However you must have missed, in my
> original post, where I said, this code gives an error at line 7 --
> > > This gives the error "Object variable not set" at line 7.
>   Paul


> > As documented in If...Then...Else Statement:
> > If objectname is objecttype then...

> > 1   function Func
> > 2     set Func = nothing
> > 3   end function
> > 4
> > 5   dim obj, a
> > 6   set obj = Func
> > 7   if obj is nothing then
> > 8     a = 1
> > 9   else
> > 10    a = 2
> > 11  end if

> > regards,
> > --
> > Gurgen Alaverdian
> > http://www.gurgensvbstuff.com

> > > I am writing ASP code with VBscript.
> > > I have a function that normally returns an object.  For example a
> > > "Find" function would return the object found.
> > > Sometimes I want the function to return an indication that nothing was
> > > found.
> > > On the caller side, the returned value is assigned to a variable, and
> > > tested for the special indicator.

> > > I can not find an indicator that will work --

> > > I) I have tried returning "nothing":

> > > 1   function Func
> > > 2     set Func = nothing
> > > 3   end function
> > > 4
> > > 5   dim obj, a
> > > 6   set obj = Func
> > > 7   if obj = nothing then
> > > 8     a = 1
> > > 9   else
> > > 10    a = 2
> > > 11  end if

> > > This gives the error "Object variable not set" at line 7.
> > > I have also tried for line 7  IsEmpty(obj)  which returns False even
> > > though obj is "nothing".

> > > II) I have also tried returning "null"

> > > 1   function Func
> > > 2     Func = null
> > > 3   end function
> > > 4
> > > 5   dim obj, a
> > > 6   set obj = Func
> > > 7   if IsNull(obj) then
> > > 8     a = 1
> > > 9   else
> > > 10    a = 2
> > > 11  end if

> > > This gives an error at line 6 because of the keyword "set".  Removing
> > > "set" will cause an error when Func actually does return an object.

> > > Can anyone give me a solution to this problem?

> > >   Thank you,
> > >   Paul



Sat, 05 Mar 2005 02:07:36 GMT  
 how to return a "null" object
Gurgen,
  My sincerest apologies to you. It is I who missed what you had said
in your first post.
  Also, my extreme gratitude.  Not only did you solve the Nothing
problem, but you also gave me TypeName().  This is something else I
had been wishing for, and could not find.
  I had been using http://www.devguru.com/Technologies/vbscript/quickref/vbscript_intro....
as my reference.  I find that site to be very quick and friendly.  But
alas, TypeName() is not documented there.
  I just looked at http://www.webf1.com/ScriptDoc/VBScript/vbstoc.htm
and TypeName() is documented there.  This site is not the easiest for
me to use.
  Is there another really good reference site you would recommend?
  Again, thank you.

  One thing though, your use of Wscript gives me an error -- "Object
Required".
  For my ASP server, I am using MicroSoft's IIS, that comes with
Windows 2000.  Is Wscript something unique to some other server?

  Once again, thank you for your help.
  Paul Levin

Quote:

> Paul,
> I did not miss it. The way you are trying to evaluate the object "if obj = nothing" is wrong.
> Use "if obj is nothing" or could also try If TypeName(obj) = "Nothing"

> Consider:
> -------------------------------------------------------------------------
> function Func(eval)
> Select Case eval
>  Case 1 set Func = CreateObject("Scripting.FileSystemObject")
>  Case 2 set Func = new regexp
>  Case 3 set Func = nothing
> End Select
> end function

> dim obj, a
> For i=1 to 3
> set obj = Func(i)
> if obj is Nothing then
>   Wscript.Echo "The Object Type is: " & TypeName(obj)
>   Wscript.Echo "Nothing Else to Do..."
> else Wscript.echo "The Object Type is: " & TypeName(obj)
>    Wscript.Echo "Continue execution...." & vbcrlf
> end if
> Next
> -----------------------------------------------------------------------

> regards,
> --
> Gurgen Alaverdian
> http://www.gurgensvbstuff.com



Sat, 05 Mar 2005 23:42:41 GMT  
 how to return a "null" object
Paul,
I missed the part that you need this for IIS hosted script. Wscript object is a native object of WSH and inappropriate for ASP
scripts.
In the example Wscript.Echo just directs visual output to the command console. You may use Response.Write instead in your script
(I guess you already knew that).
BTW the "webf1.com" has reference for TypeName() function. Also see the link provided by Joe. Most of the documentation references
are appropriate for the IIS hosted scripts.
Finally see this quick reference to ASP specific objects:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iisr...

Regards,
--
Gurgen Alaverdian
http://www.gurgensvbstuff.com

Quote:

> Gurgen,
>   My sincerest apologies to you. It is I who missed what you had said
> in your first post.
>   Also, my extreme gratitude.  Not only did you solve the Nothing
> problem, but you also gave me TypeName().  This is something else I
> had been wishing for, and could not find.
>   I had been using http://www.devguru.com/Technologies/vbscript/quickref/vbscript_intro....
> as my reference.  I find that site to be very quick and friendly.  But
> alas, TypeName() is not documented there.
>   I just looked at http://www.webf1.com/ScriptDoc/VBScript/vbstoc.htm
> and TypeName() is documented there.  This site is not the easiest for
> me to use.
>   Is there another really good reference site you would recommend?
>   Again, thank you.

>   One thing though, your use of Wscript gives me an error -- "Object
> Required".
>   For my ASP server, I am using MicroSoft's IIS, that comes with
> Windows 2000.  Is Wscript something unique to some other server?

>   Once again, thank you for your help.
>   Paul Levin


> > Paul,
> > I did not miss it. The way you are trying to evaluate the object "if obj = nothing" is wrong.
> > Use "if obj is nothing" or could also try If TypeName(obj) = "Nothing"

> > Consider:
> > -------------------------------------------------------------------------
> > function Func(eval)
> > Select Case eval
> >  Case 1 set Func = CreateObject("Scripting.FileSystemObject")
> >  Case 2 set Func = new regexp
> >  Case 3 set Func = nothing
> > End Select
> > end function

> > dim obj, a
> > For i=1 to 3
> > set obj = Func(i)
> > if obj is Nothing then
> >   Wscript.Echo "The Object Type is: " & TypeName(obj)
> >   Wscript.Echo "Nothing Else to Do..."
> > else Wscript.echo "The Object Type is: " & TypeName(obj)
> >    Wscript.Echo "Continue execution...." & vbcrlf
> > end if
> > Next
> > -----------------------------------------------------------------------

> > regards,
> > --
> > Gurgen Alaverdian
> > http://www.gurgensvbstuff.com



Sun, 06 Mar 2005 03:28:18 GMT  
 how to return a "null" object
Hi Paul,

Download WSH/VBS chm file documentation (watch out for the wrap(s)):

http://download.microsoft.com/download/winscript56/Install/5.6/W982KM...
US/scrdoc56en.exe

Regards,
Joe Earnest


Quote:
> Gurgen,
>   My sincerest apologies to you. It is I who missed what you had said
> in your first post.
>   Also, my extreme gratitude.  Not only did you solve the Nothing
> problem, but you also gave me TypeName().  This is something else I
> had been wishing for, and could not find.
>   I had been using

http://www.devguru.com/Technologies/vbscript/quickref/vbscript_intro....
Quote:
> as my reference.  I find that site to be very quick and friendly.  But
> alas, TypeName() is not documented there.
>   I just looked at http://www.webf1.com/ScriptDoc/VBScript/vbstoc.htm
> and TypeName() is documented there.  This site is not the easiest for
> me to use.
>   Is there another really good reference site you would recommend?
>   Again, thank you.

>   One thing though, your use of Wscript gives me an error -- "Object
> Required".
>   For my ASP server, I am using MicroSoft's IIS, that comes with
> Windows 2000.  Is Wscript something unique to some other server?

>   Once again, thank you for your help.
>   Paul Levin




Quote:
> > Paul,
> > I did not miss it. The way you are trying to evaluate the object "if obj

= nothing" is wrong.

- Show quoted text -

Quote:
> > Use "if obj is nothing" or could also try If TypeName(obj) = "Nothing"

> > Consider:

> -------------------------------------------------------------------------
> > function Func(eval)
> > Select Case eval
> >  Case 1 set Func = CreateObject("Scripting.FileSystemObject")
> >  Case 2 set Func = new regexp
> >  Case 3 set Func = nothing
> > End Select
> > end function

> > dim obj, a
> > For i=1 to 3
> > set obj = Func(i)
> > if obj is Nothing then
> >   Wscript.Echo "The Object Type is: " & TypeName(obj)
> >   Wscript.Echo "Nothing Else to Do..."
> > else Wscript.echo "The Object Type is: " & TypeName(obj)
> >    Wscript.Echo "Continue execution...." & vbcrlf
> > end if
> > Next
> > -----------------------------------------------------------------------

> > regards,
> > --
> > Gurgen Alaverdian
> > http://www.gurgensvbstuff.com



Sun, 06 Mar 2005 01:45:44 GMT  
 how to return a "null" object
Joe & Gurgen,
  Thank you both for the excellent information.  It has been a lot of help.
  Paul


Mon, 07 Mar 2005 00:22:09 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. detecting "object required" or null object

2. detecting "object required" or null object

3. "...Is null or not an object"

4. Detecting "null"-object

5. object.Delete("IISWebVirtualDir", VDir) returns 1A8

6. "Invalid use of null"

7. *"*-.,._,.-*"* I"LL TRADE VISUAL C++ FOR VBASIC *"*-.,_,.-*"*

8. "Invalid use of null" error

9. replace null in field with "-"

10. NULLS to "" or 0

11. need help about "null"

12. Null EntryID on "Save and New"

 

 
Powered by phpBB® Forum Software