"Not" usage.

rated by 0 users
This post has 6 Replies | 3 Followers

Top 25 Contributor
Posts 84
Megahand Posted: Mon, Feb 8 2010 1:22 PM

Hm, interesting.

[If Not var1 = "texttexttext"] does not throw an error.

[If var1 = Not "texttexttext"] does.

For the sake of expanding my database of knowledge, I ask the following question.

Why?

Top 10 Contributor
Posts 298
LFS replied on Mon, Feb 8 2010 2:40 PM
Your first case shows a syntax error on my system. It says var1 needs to be a boolean variable, which would be expected. It is rather obvious why the second case errors, strings are not Boolean types....
Top 25 Contributor
Posts 84

The second one was a DUH moment for me. *facepalm*

...and as for the first, are you sure that it's copied/been typed correctly? Doesn't throw an error here.

Top 10 Contributor
Posts 768

The first case compiles and executes flawlessly on my system*, odd that it doesn't on LFS'.

Interestingly, I just discovered that you can convert a non-boolean comparison into a boolean, like so:

Bool1 = ( Var1 = "texttexttext" )

Pretty nifty. Smile

*Vista Home Premium 32-bit SP2; Phrogram 2.5.3169.17574, non-admin user.

Top 10 Contributor
Posts 298
LFS replied on Mon, Feb 8 2010 6:24 PM
What type did you declare var1 as? (Phrogram Version 2.0.2608.24593)
Top 10 Contributor
Posts 768

I declared it as a String.

Top 10 Contributor
Posts 589
ZMan replied on Fri, Feb 12 2010 11:40 AM

Just be be clear is this what you had?

Program MyNewProgram
 Method Main()
  Define var1 As String
  Define itWorks As Boolean
  itworks = False
  
  If Not var1 = "texttexttest" Then
   itWorks = True
  End If
  
  If var1 = Not "texttexttext" Then
   itworks= True
  End If
 End Method
End Program

On my Phrogram (2.1.2751) the second If statement shows a syntax error on the Not (where I underlined)

The background here is to do with operator precedence when there are no parentheses

Not is an operator that operates on booleans. It takes TRUE/FALSE and flips it the other way.

= is either an assignment or an comparison. A comparison operator takes the 2 operands and returns a boolean.

So the 1st IF statement condition
   Not var1="texttexttext"
is really evaluated like this
  Not (var1="texttexttext)
which works becuase the = operator returns a boolean that Not can work on.

The 2nd IF statement condition
  var1 = Not "Texttexttext"
is evaluated like this
  var1 = (not "Texttexttext")
which makes no sense becuase you can't Not a string.

Since you didn't use parentheses how does the compiler know what to do? This is an age old question.
Whats the asnwer to this?
4 + 2 * 6 =?
Is it 36  becuase (4 +2) * 6 or is it 16 becuase 4 + (2*6)?
In most place in the workd its 16 becuase multiplication gets precendecen if parentheses are not there.

Where there is ambiguity the rules for Phrogram are defined such that = gets precendence over Not.

The 1st IF statement could be
   (Not var1) = "text"
or
  Not (var1 = "text")
Since = is more important the 2nd once is chosen.

The 2nd IF statment is not ambiguous. You can only write it as
  var1 = (Not "text")
So it fails.

Long explanation - hope it makes sense.

 

 

Managed DirectX and XNA ? Check out http://www.thezbuffer.com
Page 1 of 1 (7 items) | RSS