#### Archived

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

# Why can't VB do IF statements

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

## 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 on other sites

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 on other sites
try "" instead of null or vbnull

##### Share on other sites
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 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 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 on other sites
Check out this help page:

Null, Empty, Nothing, and vbNullString

Scroll down to find that section.

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]

##### 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 on other sites
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.

##### 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 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 on other sites
Thing is a number right??

500

##### 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 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 thisSub Main() Dim MyArray() As Long Redim MyMisspeltArray(10) MyArray(0) = 1End 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 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 on other sites
Remember in VB you have to put End If''s after all your if statements.