DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: cannot use isnothing to test for emty refs, why not ?

  1. #1
    MrRolf Guest

    cannot use isnothing to test for emty refs, why not ?


    This is a tip from the VB2TheMax web site:
    --------------------------------------------------------------------
    You can't reliably test an auto-instancing object variable using the Is Nothing
    test, because as soon as you reference the variable Visual Basic silently
    creates an object of the given class, and the test always returns False:

    Dim x As New MyClass
    If x Is Nothing Then
    ' this line will be never executed
    End If
    ------------------------------------------------------------------

    .. I have successfully used isnothing to test for emtpy references trough
    the whole project.

    Is the above example right if I used :

    dim x as myclass
    set x = new myclass
    If x Is Nothing Then ..



  2. #2
    Anthony Jones Guest

    Re: cannot use isnothing to test for emty refs, why not ?

    Yes.

    Don't use auto-instancing object variables. It just plain ugly.

    Of course in the actual code you have posted x will never be nothing also.

    --
    Anthony Jones
    Nuesoft Ltd



  3. #3
    Arthur Wood Guest

    Re: cannot use isnothing to test for emty refs, why not ?


    The problem is because when you declare an Object in the format

    Dim Obj as New Object

    ANY(repeat ANY) reference to obj will AUTOMATICALLY Create an Instance of
    obj, so, as you have discovered, it will NEVER be NOTHIG.

    The CORRECT way to do this is:

    Dim obj as MyClass ' note ---> DO NOT USE "NEW" here

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