[RESOLVED] If... Then (without using = true or = false)
I'm reading the following code:
Quote:
Dim fRecording as Boolean
If fRecording Then
...
End If
I thought the use of If... Then using a Boolean variable should be
Quote:
If fRecording = true then
....
End If
What means If fRecording Then... without the use of = false or = true?
Re: If... Then (without using = true or = false)
All that needs to be between the If and Then is a boolean expression. Meaning it can be anything that evaluates down to a true or false value. If you want to see if a variable equals 2, you do this, right? If x = 2 Then .... you don't write If (x=2)=True Then ... right? Why not? as long as the expression, what ever it is, results in a true/false value, it can be what ever you need it to be.
-tg
Re: If... Then (without using = true or = false)
Something like:
Code:
If fRecording = True then
Is sort of silly and redundant. Almost as silly as faux C hungarian using "f" ("flag?) for a Boolean.
For that matter the expression must be Boolean or else the compiler coerces it to a Boolean value, which is why even when testing a Long, etc. any non-zero value takes the True branch.
Re: If... Then (without using = true or = false)
Quote:
Originally Posted by
dilettante
Something like:
Code:
If fRecording = True then
Is sort of silly and redundant. Almost as silly as
faux C hungarian using "f" ("flag?) for a Boolean.
For that matter the expression must be Boolean or else the compiler coerces it to a Boolean value, which is why even when testing a Long, etc. any non-zero value takes the True branch.
I would agree with redundant but silly could be a little harsh. Some may even argue that
Code:
If fRecording = True then
is more readable than
Re: If... Then (without using = true or = false)
Quote:
Originally Posted by
MarkT
Some may even argue that
Code:
If fRecording = True then
is more readable than
IMHO, a more descriptive variable name would be both readable and efficient:
Code:
Dim IsRecording As Boolean
If IsRecording Then
Personally, I have nothing against using Hungarian notation (such as fRecording). In fact, I find it very helpful when reading code using an editor other than the VB6 IDE.
Re: If... Then (without using = true or = false)
Quote:
Originally Posted by
dilettante
Something like:
Code:
If fRecording = True then
Is sort of silly and redundant. Almost as silly as
faux C hungarian using "f" ("flag?) for a Boolean.
Agreed, "f" has always struck me as an odd choice. True hungarian uses "f" for "float" and "b" for "boolean", which makes it all the more confusing.
Quote:
Originally Posted by
Bonnie West
IMHO, a more descriptive variable name would be both readable
and efficient:
Code:
Dim IsRecording As Boolean
If IsRecording Then
This is great advice. Not sure if others would agree, but I also prefer this when evaluating conditions to False, e.g. instead of...
Code:
If IsRecording = False Then...
use:
Code:
If Not IsRecording Then...
Re: If... Then (without using = true or = false)
Thank you very much to all for the answers. I understand it now.
Re: [RESOLVED] If... Then (without using = true or = false)
Welcome to Nitpickers Anonymous! ;)
Re: [RESOLVED] If... Then (without using = true or = false)
vb Code:
Dim IsRecording As Boolean
'This
If IsRecording Then
'Is the same as
If IsRecording = True Then
'Just as this
If Not IsRecording Then
'Is the same as
If IsRcording = False Then
In both cases I recommend avoiding the double evaluation, don't go with the choice involving an = True or = False in it when the variable is already a boolean.