CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Does object exist?

    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



  2. #2
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: Does object exist?

    Did you try this?
    =======
    If TypeName(obj)<> "Nothing" then
    obj.method
    End If
    =======



    Regards,

    Michi

  3. #3
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: Does object exist?

    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


  4. #4
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: Does object exist?

    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

  5. #5
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: Does object exist?

    It's declared as Object, but "If Not (obj Is Nothing)" gives the same error.
    Thank you
    Vlad


  6. #6
    Join Date
    Aug 2001
    Posts
    5

    Re: Does object exist?

    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


  7. #7
    Join Date
    Aug 2001
    Posts
    1

    Re: Does object exist?

    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


  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Does object exist?

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: Does object exist?

    In this case just on assignment statement I'm getting an error #91 "Object variable or..."
    Thank you
    Vlad


  10. #10
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: Does object exist?

    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


  11. #11
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Does object exist?


    '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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  12. #12
    Join Date
    Aug 2001
    Posts
    5

    Re: Does object exist?

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured