Check if object IS NOTHING


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Check if object IS NOTHING

  1. #1
    Igor Guest

    Check if object IS NOTHING


    Hello everybody,
    Simple test shows weird behavior of VB.
    Private Sub cmdTest_Click()
    Dim Test As New Collection

    Test.Add "Test"
    MsgBox "Before nothing count = " & Test.Count
    Set Test = Nothing
    If Test Is Nothing Then
    MsgBox "test=Nothing"
    Else
    MsgBox "test IS NOT Nothing"
    End If
    End Sub

    When I run this code object that was explicitly Set to nothing is still “something”.
    Well I guess my question is how do I check if object is Nothing.



  2. #2
    Phil Weber Guest

    Re: Check if object IS NOTHING

    > When I run this code object that was explicitly Set to
    > Nothing is still “something”.


    Igor: The problem is this line:

    Dim Test As New Collection

    Whenever Test is referenced in your code, VB checks to see if it's Nothing,
    and if so, it automatically instantiates it. To get the behavior you expect,
    change your code to this:

    Dim Test As Collection
    Set Test = New Collection

    Now, when you Set Test = Nothing, it will stay Nothing.
    ---
    Phil Weber



  3. #3
    Jim Pragit Guest

    Re: Check if object IS NOTHING


    "Igor" <irodionov@hotmail.com> wrote:
    >
    >Well I guess my question is how do I check if object is Nothing.


    You can't. Whenever you declare an object As New, VB will automatically
    instantiate the object if it doesn't already exist. To get around this,
    don't use New in the variable declaration. Instead, instantiate it on a
    separate line. (This will also give your code a performance boost.)

    - Jim

  4. #4
    Joe \Nuke Me Xemu\ Foster Guest

    Re: Check if object IS NOTHING

    "Igor" <irodionov@hotmail.com> wrote in message <news:3c1a6740$2@147.208.176.211>...

    > Hello everybody,
    > Simple test shows weird behavior of VB.
    > Private Sub cmdTest_Click()
    > Dim Test As New Collection


    "As New" objects, like the default form objects, are undead. If you so
    much as look at one directly, it comes back to shambling semi-life, looking
    for brains. Your brains. As you wrack them, invariably at oh-dark-thirty,
    trying to figure out why your app just will not shut down...

    --
    Joe Foster <mailto:jlfoster%40znet.com> Sacrament R2-45 <http://www.xenu.net/>
    WARNING: I cannot be held responsible for the above They're coming to
    because my cats have apparently learned to type. take me away, ha ha!



  5. #5
    huibert Guest

    Re: Check if object IS NOTHING


    "Igor" <irodionov@hotmail.com> wrote:
    >
    >Hello everybody,
    >Simple test shows weird behavior of VB.
    >Private Sub cmdTest_Click()
    > Dim Test As New Collection
    >
    >Test.Add "Test"
    > MsgBox "Before nothing count = " & Test.Count
    > Set Test = Nothing
    > If Test Is Nothing Then
    > MsgBox "test=Nothing"
    > Else
    > MsgBox "test IS NOT Nothing"
    > End If
    >End Sub
    >
    >When I run this code object that was explicitly Set to nothing is still

    “something”.
    >Well I guess my question is how do I check if object is Nothing.
    >
    >

    Yeah,

    those guys are right: you can't eat your cake and have it.
    When you use AS NEW, you give up control over the object's lifetime. VB,
    not YOU will instantiate the object when it thinks it needs to do so.
    Never use AS NEW.

    HTH
    Huibert




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center