Why can't VB do IF statements

Started by
15 comments, last by Monder 21 years, 1 month ago
Right I'm sure I'm missing something here I have this if statement in VB (I'm actually using VBA in Access 2000 but it probably doesn't matter)

If (Thing <> Null) Then
        Exit Function
Else
....
   
This bit wasn't working so I put a breakpoint on If(Thing <> Null) Then And so the debugger kicked in on that line and I checked what thing was buu holding the cursor over it and it pops up in a tool tip, it said
quote:Thing = "5"
So it didn't equal Null and so should've gone onto exit function and for some reason it skipped that and went into the else block. Can anybody tell me why VB isn't doing would it should do? [edited by - Monder on March 3, 2003 5:09:32 PM] [edited by - Monder on March 3, 2003 5:10:25 PM]
Advertisement
READ YOUR CODE

If (Thing <> Null) Then Exit FunctionElse....

psuedo code: if thing is anything but null exit function


5 is not null, so it will exit function

try "" instead of null or vbnull
Are you adding those parenthesis? You dont want to use those on VB If statements. Not like in Java or something..

If thing <> null then
...
Else

and so on I think works better, could be wrong. Been a bit since ive used VB.
VB is doing exactly what it should. The question really is, why don''t you understand the proper use of null?

The only variable type that can utilize a Null value is the Variant data type, and it is reserved for those times when the contained value does not exist. If you''ve declared your variable (Thing) as anything else, it cannot be evaluated as null as all variables are initialized upon declaration. If you have declared Thing as a class, then don''t use Null, use Nothing.

-Kirk
Try using Nothing instead of Null. I didn't even know Null was valid VB syntax.

Side note, you can also use the word NOT instead of <>.

[edited by - FieroAddict on March 3, 2003 5:26:04 PM]
Check out this help page:

Null, Empty, Nothing, and vbNullString

Scroll down to find that section.

Based on what is there, I think your code should read:

If (Not IsNull(Thing)) Then           Exit FunctionElse   


or possibly

If (Not (Thing Is Nothing)) Then           Exit FunctionElse   



[edited by - Waverider on March 3, 2003 5:35:39 PM]
It's not what you're taught, it's what you learn.
Thanks Waverider, that page had the solution I now use

If IsNull(Thing) Then    ....Else    Exit Function 


I''m a C coder so I when I something that''s meant to equal Null (As is said Thing was(not in the example I used)) and when I use something like If Thing = Null or if Thing <> Null and it doesn''t work I get confused
May I recommend this book:

Programming Visual Basic 6.0

I wasn''t even aware of the Nothing keyword or the Is Nothing syntax until I read it. Nor was I aware of cluster data types like collections and property bags.
It's not what you're taught, it's what you learn.
The question to ask, is why VB even let that code compile.


- Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement