Detecting "null"-object 
Author Message
 Detecting "null"-object

Hello VBA gurus!

I'm new to VBA and have the following problem:

I have a lookup-function which returns a reference to an object or the value
"Nothing" if no object was found. Then I need to compare the returned value
with another object. The comparison with "Nothing" should always result to
"false". But instead, I get an error message.

How can I compare two objects one of which is "Nothing"? I've found the
function IsNull but it seems not to do what I need.

Example:

function finder(arg as integer) as MyObject
  if arg > 0 then
    set finder = new MyObject
  else
    set finder = Nothing
  end if
end function

function tester () as boolean
  dim retVal as MyObject
  set retVal = finder()
  if retVal = Nothing then       ' This is what doesn't work (runtime error)
    ' do something
  else
    ' do something else
  end if
end function

Thanks in advance.

--
Alexander

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 07 Mar 2003 03:00:00 GMT  
 Detecting "null"-object

Sorry, the example should read (typing error)

function finder(arg as integer) as MyObject
  if arg > 0 then
    set finder = new MyObject
  else
    set finder = Nothing
  end if
end function

sub tester()
  dim retVal as MyObject
  set retVal = finder(-4)
  if retVal = Nothing then ' This is what doesn't work (runtime error)
    ' do something
  else
    ' do something else
  end if
end sub

--
Alexander

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 07 Mar 2003 03:00:00 GMT  
 Detecting "null"-object
Hi Alexander,

Change the line to

If retVal Is Nothing Then...

HTH,
    Nick


Quote:


> Sorry, the example should read (typing error)

> function finder(arg as integer) as MyObject
>   if arg > 0 then
>     set finder = new MyObject
>   else
>     set finder = Nothing
>   end if
> end function

> sub tester()
>   dim retVal as MyObject
>   set retVal = finder(-4)
>   if retVal = Nothing then ' This is what doesn't work (runtime error)
>     ' do something
>   else
>     ' do something else
>   end if
> end sub

> --
> Alexander

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Fri, 07 Mar 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. detecting "object required" or null object

2. detecting "object required" or null object

3. how to return a "null" object

4. Detecting "Nothing" object

5. Detecting "Alt"+"Ctrl"+"Del" Key press

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

7. "Invalid use of null" error

8. replace null in field with "-"

9. NULLS to "" or 0

10. need help about "null"

11. Null EntryID on "Save and New"

12. Null EntryID on "Save and New"

 

 
Powered by phpBB® Forum Software