Click to See Complete Forum and Search --> : Does object exist?
vchapran
August 21st, 2001, 08:20 AM
In a pretty complicated application I have a function which does something with an object created by another procedure. There is a lot of things which should be properly set to create that object successfully. Sometimes object is not created at all. And an attempt to call one of its methods crashes application.
So, my question is: How can I find that object exists? I tried
If not(obj is nothing) then
obj.method
End if
but it doesn't work.
To find the reason why object was not created is difficult as well as to use error handler, because error handler itself (and it's standard for this application) contains that object.
Thank you.
Vlad
michi
August 21st, 2001, 11:09 AM
Did you try this?
=======
If TypeName(obj)<> "Nothing" then
obj.method
End If
=======
Regards,
Michi
vchapran
August 21st, 2001, 11:29 AM
Unfortunately it doesn't work too. The same "Object Variable or With..." error occures when program comes to the name of that object. Now it produces an error on line with "If TypeName(obj)<> "Nothing" then".
I beleive it's very common situation, when there is a need to know the contents of variable. With simple variables we do whatever we want, but how about object type of variable?
Thank you
Vlad
michi
August 21st, 2001, 12:11 PM
If you declared obj as Variant, can you change it to Object? What I mean is:
====
Dim obj As Object
If Not (obj Is Nothing) Then
obj.Method
End If
====
Regards,
Michi
vchapran
August 21st, 2001, 12:39 PM
It's declared as Object, but "If Not (obj Is Nothing)" gives the same error.
Thank you
Vlad
vlad8682
August 22nd, 2001, 01:08 PM
I can't beleive that nobody met this situation before. Is this very rare if you do want to know if object exists?
Thank you.
Vlad
gershovich
August 24th, 2001, 08:53 AM
You can try to declare variable of Variant type and assign our object to this variable. After assignment you can try IsEmpty on your variable.
Maybe it will help
Cimperiali
August 24th, 2001, 10:22 AM
you should be able to use:
if typeof(obj) is yourObjectSupposedType
or
if not isempty(obj)
provided you have a
set obj = something
before these instructions. But if this object is not passed (as it is supposed to be existent before and outside your procedure)and it has not been created, I think the only working solution should reside in error handling:
on error resume next
obj.method
if err.number >0 then
...
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
vchapran
August 24th, 2001, 05:50 PM
In this case just on assignment statement I'm getting an error #91 "Object variable or..."
Thank you
Vlad
vchapran
August 24th, 2001, 05:54 PM
It's already funny, but even On Error Resume Next doesn't do anything. Just after this statement on line reffering to Object wich was not created I'm getting error #91 "Object Variable or ..."
I decided that something wrong with vy VB installation or settings and tested on 2 other developer workstations. The same bad result. Nothing works.
Thank you
Cimperiali
August 27th, 2001, 02:06 AM
'I do not think this is the solution. I thonk the first answer you get
'(use a varianat) is the most accurate you get (with a slight adjust):
public sub myObj(theObj as variant)
If isObject(theObj) then
theobj.method
End if
End sub
'However, the fact you get an error as if you've not setted the object lead me
'thinking the component where you have this procedure does not know anything
'about your object (this is normal if you're using late binding) and it seems
'as if the object is private (=you cannot pass private objects through
'different components, but you may in the same component or you may use the
'workaround to pass them in variant).
'Have a try with the above code.
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
vlad8682
August 27th, 2001, 03:09 PM
Thank you. Due to company policy, I'm pretty restricted. Also, this approach (declaring an object type variable and assigning to it many different objects in hundreds of places) is common for this application. Pretty often we have problem I described in my first post (object was not created by some reason and we are trying to use one of its methods, and even error handlers contain that object - so run time error as a result). I was hoping to find the way to analize if object was created , then use its methods, if no - at least not to use it to prevent run time. But it looks like I did not find anything.
Thank you
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.