Archived

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

Monder

Why can't VB do IF statements

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
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
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
quote:
Original post by Magmai Kai Holmlor
The question to ask, is why VB even let that code compile.


Good question. One thing I really love are uninitialized arrays.

Share this post


Link to post
Share on other sites
The original question; I think you had a type mismatch.

Thing was being reported as a string or number, right? The "Nothing" and "Null" keywords are related to Objects (All classes are derived from the Object class, User-Defined Types are something else entirely).

Yes, the "xxx Is yyy" syntax is correct for comparing objects. In essence, it is like (in C/++) comparing the addresses of two variables, ie "&xxx == &yyy", or the values of two pointers.

Handy note; using the "=" or "<>" syntax for testing equivalency of objects will actually compare their "default" public member, not the entire class.

Hope this helps!

Share this post


Link to post
Share on other sites
quote:

The question to ask, is why VB even let that code compile.



VB will go out of its way to compile crappy code


  
Option Explicit ''//<- Note this


Sub Main()
Dim MyArray() As Long

Redim MyMisspeltArray(10)

MyArray(0) = 1
End Sub


Instead of noticing the misspelling and giving a compile time error, VB will compile the program, run it and then give a subscript out of range error after creating an 11 element array of variants . VB just has this way of knowing exactly what I want it to do (I shouldn''t complain, I''m now in the second 90% of my rts without it causing any headaches)

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
quote:
Original post by ragonastick
Instead of noticing the misspelling and giving a compile time error, VB will compile the program

That''s pretty amazing. I actually had to paste the code into VB and try it before I believed you

Share this post


Link to post
Share on other sites