Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the diff between these two statements?

Status
Not open for further replies.

jwarmuth

IS-IT--Management
Sep 22, 2001
151
CA
In terms of function, not in syntax.

Code:
If regKey1 Is Nothing Then
     'do stuff
End If


Code:
If (regKey1 Is Nothing) Then
     'do stuff
End If

I'm curious what the parenthesis do. Thanks!

Jeff W.
MCSE, CNE
 
nothing. Parenthesis in conditions opperate just like they do in math functions.

Code:
IF x AND y OR z THEN
IF (x AND y) OR z THEN
These both evaluate the same thing. They will be true if x AND y are true, OR z is true.

Code:
IF x AND (y OR z) THEN
This will be true is x is true AND y OR z is true.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Parentheses determine the order of operation when comparing or calculating variables.

In your case it doesn't make a difference, but in order cases it does. Just look up 'operator precedence' in your help files.
 
In VB the parenthesis are optional for a simple comparison like you have. In other languages (the C and Java families) they're required.

They can also be used to help illustrate and simplify a piece of code. For example:
Code:
Public Function OutOfInventory() As bool
   OutOfInventory = (Me.QtySold = Me.InitialQtyOnHand)
End Function
Putting the parenthesis makes it explicit that you're doing a comparison first, and then assigning the result to the return of the function. (*)

IMO, using them all the time would be a good habit to develop.

Chip H.


(*) This is one of the problems with the VB language -- the assignment operator (=) looks just like the comparison operator (=), and you have to examine how they're being used to understand what's going on.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the input. I had a feeling they operated like their math counterparts. :)

Jeff W.
MCSE, CNE
 
(*) This is one of the problems with the VB language

Or one of the features. Depends on how you look at it. Personally, I get annoyed working in Java/C/Crystal when I have to == and := in places where the context makes it obvious which one to use.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top