Results 1 to 6 of 6

Thread: How to check if an object is valid

  1. #1
    evileconomist
    Guest

    How to check if an object is valid

    Hi

    How do I check if an object returned by some function call is valid?
    In C++/MFC I'd use a Macro (such as FAILED or SUCCEDED), so whats the magic word in VB6

    Private Sub MyTreeView_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

    Dim currentNode As Node

    'If a node is hit then OK, however if HitTest failed I get
    'node = nothing
    Set currentNode = TreeViewRics.HitTest(x, y)

    If currentNode = Nothing Then 'This doesn't work
    Exit Sub
    End If

    If Button = vbRightButton Then
    PopupMenu mnuTest, vbPopupMenuLeftAlign
    End If
    Set currentNode = Nothing

    End Sub

  2. #2
    Addicted Member Sand_Hawk's Avatar
    Join Date
    Dec 2001
    Location
    The Netherlands
    Posts
    138
    I don't know for sure but you could replace = Nothing with isNull
    So the code should loke something like this:

    Code:
    if CurrNode IsNull Then
       'Place stuff here
    End if
    Sand Hawk
    - We wish to assimilate more information.

  3. #3
    evileconomist
    Guest
    Almost there

    If IsNull(currentNode) Then
    Exit Sub
    End If

    except it doesn't work, I tried

    If IsEmpty(currentNode) Then
    Exit Sub
    End If

    which also doesn't work, a "nothing" value seems to be different from "Null".

    Help!

  4. #4
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Hi,

    I got the definitions at the bottom of this reply from MSDN.

    Can you use the following code?

    Code:
        If currentNode Is Nothing Then
    Code:
    "": A zero-length string (commonly called an "empty string") is technically a zero-length BSTR that actually uses six bytes of memory. In general, you should use the constant vbNullString instead, particularly when calling external DLL procedures. 
    Empty: A variant of VarType 0 (vbEmpty) that has not yet been initialized. Test whether it is "nil" using the IsEmpty function. 
    Nothing: Destroys an object reference using the Set statement. Test whether it is "nil" using the Is operator: 
    If obj Is Nothing Then... 
    
    
    Null: A variant of VarType 1 (vbNull) that means "no valid data" and generally indicates a database field with no value. Don't confuse this with a C NULL, which indicates zero. Test whether it is "nil" using the IsNull function. 
    vbNullChar: A character having a value of zero. It is commonly used for adding a C NULL to a string or for filling a fixed-length string with zeroes: 
    Path = String(255, vbNullChar) 
    
    
    vbNullString: A string having a value of zero, such as a C NULL, that takes no memory. Use this string for calling external procedures looking for a null pointer to a string. To distinguish between vbNullString and "", use the VBA StrPtr function: StrPtr(vbNullString) is zero, while StrPtr("") is a nonzero memory address.
    This world is not my home. I'm just passing through.

  5. #5
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    oops!

    The formatting didn't come out too well there did it...


    Try looking through MSDN for "Avoid Programming Pitfalls" which is in the March edition of "Visual Basic Programmer's Journal" under "Periodicals 1998"

    Tris.
    This world is not my home. I'm just passing through.

  6. #6
    evileconomist
    Guest
    BINGO

    Thanks

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