|
-
Jan 23rd, 2002, 07:30 AM
#1
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
-
Jan 23rd, 2002, 07:46 AM
#2
Addicted Member
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.
-
Jan 23rd, 2002, 08:06 AM
#3
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!
-
Jan 23rd, 2002, 08:36 AM
#4
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.
-
Jan 23rd, 2002, 08:39 AM
#5
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.
-
Jan 23rd, 2002, 08:43 AM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|