Results 1 to 9 of 9

Thread: [RESOLVED] If... Then (without using = true or = false)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Resolved [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?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: If... Then (without using = true or = false)

    Quote Originally Posted by dilettante View Post
    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
    Code:
    If fRecording Then

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: If... Then (without using = true or = false)

    Quote Originally Posted by MarkT View Post
    Some may even argue that
    Code:
    If fRecording = True then
    is more readable than
    Code:
    If fRecording Then
    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)

  6. #6
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    Re: If... Then (without using = true or = false)

    Quote Originally Posted by dilettante View Post
    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 View Post
    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...
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: If... Then (without using = true or = false)

    Thank you very much to all for the answers. I understand it now.

  8. #8
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] If... Then (without using = true or = false)

    Welcome to Nitpickers Anonymous!

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] If... Then (without using = true or = false)

    vb Code:
    1. Dim IsRecording As Boolean
    2.  
    3. 'This
    4. If IsRecording Then
    5.  
    6. 'Is the same as
    7. If IsRecording = True Then
    8.  
    9. 'Just as this
    10. If Not IsRecording Then
    11.  
    12. 'Is the same as
    13. 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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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
  •  



Click Here to Expand Forum to Full Width