|
-
Mar 11th, 2015, 04:21 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] If... Then (without using = true or = false)
I'm reading the following code:
Dim fRecording as Boolean
If fRecording Then
...
End If
I thought the use of If... Then using a Boolean variable should be
If fRecording = true then
....
End If
What means If fRecording Then... without the use of = false or = true?
-
Mar 11th, 2015, 05:25 PM
#2
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
-
Mar 11th, 2015, 05:36 PM
#3
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.
-
Mar 11th, 2015, 06:45 PM
#4
Re: If... Then (without using = true or = false)
 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
-
Mar 11th, 2015, 07:07 PM
#5
Re: If... Then (without using = true or = false)
 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.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Mar 11th, 2015, 07:29 PM
#6
Re: If... Then (without using = true or = false)
 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.
 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...
-
Mar 12th, 2015, 02:39 AM
#7
Thread Starter
Hyperactive Member
Re: If... Then (without using = true or = false)
Thank you very much to all for the answers. I understand it now.
-
Mar 12th, 2015, 10:15 AM
#8
Re: [RESOLVED] If... Then (without using = true or = false)
Welcome to Nitpickers Anonymous!
-
Mar 12th, 2015, 02:01 PM
#9
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|