What the follow line do

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidp51
    New Member
    • Apr 2012
    • 1

    What the follow line do

    What does the following line do?

    Code:
    Dim num As Single = CSng(-((num2 = Single.MinValue) > False))
    Last edited by Frinavale; Apr 11 '12, 06:32 PM. Reason: Added the question to the body of the thread and added code tags around the code snippet.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It instantiates a Single type based on the return value from the CSng Function.

    The CSng function converts an expression to type Single.

    The value that it is converting is the result of:
    Code:
    -((num2 = Single.MinValue) > False)
    If you break it down:

    You are checking to see if the num2 value is equal to the Single.MinValue (-3.402823e38)

    This will return a boolean type (true or false)

    For some reason you are checking to see if this returned value is greater than false. Since true/false is just a yes/no, or 1/0 this is strange and could possibly throw errors.

    I'm going to assume that when you use the [icode]>[icode] operator on a boolean, that it casts it into a number in order to do the comparison.

    So, assuming that true=1 and false=0, you are checking to see if the result returns true since true>false.
    This is not needed and is quite confusing.

    Anyways, the code determines if num2 is equal to -3.402823e38...it then checks to see if the result is greater than false (in other words it checks to see if it is true).

    Then the you are setting the true or false value to it's negative (flipping it). Assuming you aren't given any errors when you set a boolean to it's negative value.

    The CSng function then converts the resulting (flipped) true or false into a Single.

    I have no idea what you are trying to accomplish.
    This code really is nonsensical.

    In the end, assuming you don't have any errors thrown when you treat booleans as numbers, num will be instantiated as a -1 or 0 depending on the result of checking num2.

    My advise to you: Treat booleans as booleans and treat numbers as numbers. Don't cast between them unless you know what you are doing and have a very specific reason for doing so.
    Last edited by Frinavale; Apr 11 '12, 06:50 PM.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      In VB 6 and earlier versions, while False is 0 as expected, True is actually -1. So a boolean value can never be "> False". However, that code is most certainly not VB6.

      Using a boolean value as a number really isn't a problem, as long as you remember that True is negative. For example sometimes if I want to do something like
      Code:
      If Flag = True Then
        Value = Value + 1
      End If
      I will code it as
      Code:
      Value = Value - Flag
      The end result is the same, it's just a little shorter to code. If the Flag is False, it subtracts zero. If True, it subtracts -1 which is the same as adding 1.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Thanks Killer,

        I learned something new today.

        Comment

        Working...