Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Monder

Why can't VB do IF statements

This topic is 5614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
try "" instead of null or vbnull

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 Function
Else


or possibly


If (Not (Thing Is Nothing)) Then
Exit Function
Else



[edited by - Waverider on March 3, 2003 5:35:39 PM]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!