Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Regarding boolean type variable

Regarding boolean type variable

Scheduled Pinned Locked Moved Windows Forms
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ahamed Azeem
    wrote on last edited by
    #1

    When i use below code, it returns true and then i changed the condition to check m=1 then its returns false. I am little confused about booleans so please let me know the internal implementation about boolean. Dim m As Boolean = 1 If m = -1 Then MsgBox("true") Else MsgBox("false") End If I know that using "True" or "False" is best practise but for better understanding i need this Thanks in advance Regards Azeem

    A R L 4 Replies Last reply
    0
    • A Ahamed Azeem

      When i use below code, it returns true and then i changed the condition to check m=1 then its returns false. I am little confused about booleans so please let me know the internal implementation about boolean. Dim m As Boolean = 1 If m = -1 Then MsgBox("true") Else MsgBox("false") End If I know that using "True" or "False" is best practise but for better understanding i need this Thanks in advance Regards Azeem

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      A boolean can be either true or false. Best practice is not to use 1, -1,0 etc but use true and false as the actual values can be operating system dependant.

      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

      1 Reply Last reply
      0
      • A Ahamed Azeem

        When i use below code, it returns true and then i changed the condition to check m=1 then its returns false. I am little confused about booleans so please let me know the internal implementation about boolean. Dim m As Boolean = 1 If m = -1 Then MsgBox("true") Else MsgBox("false") End If I know that using "True" or "False" is best practise but for better understanding i need this Thanks in advance Regards Azeem

        R Offline
        R Offline
        Ray Cassick
        wrote on last edited by
        #3

        Once you set the Boolean = to 1 are you sure that it is staying at the value of 1 and not being altered to equal how VB internally stores a true or false value? Why do you want to create a Boolean then assign it a number and compare it to a number?


        LinkedIn[^] | Blog[^] | Twitter[^]

        1 Reply Last reply
        0
        • A Ahamed Azeem

          When i use below code, it returns true and then i changed the condition to check m=1 then its returns false. I am little confused about booleans so please let me know the internal implementation about boolean. Dim m As Boolean = 1 If m = -1 Then MsgBox("true") Else MsgBox("false") End If I know that using "True" or "False" is best practise but for better understanding i need this Thanks in advance Regards Azeem

          R Offline
          R Offline
          Ray Cassick
          wrote on last edited by
          #4

          Trying to locate a link that shows you why this happens, but what you are seeing is type conversion related. VB.NET stores false as a 0 and any other value as true, but that defaults to being stored as = -1. So, when you assign the Boolean = 1 it gets converted internally to -1 because it is not a false value (0). Try doing a few debug.prints and setting the Boolean to a few values and see what happens. It looks to me like you do not have option strict turned on. If not, you should because it forces your code to be far more explicit and not allow type coercion (dynamic type conversion) like this.


          LinkedIn[^] | Blog[^] | Twitter[^]

          1 Reply Last reply
          0
          • A Ahamed Azeem

            When i use below code, it returns true and then i changed the condition to check m=1 then its returns false. I am little confused about booleans so please let me know the internal implementation about boolean. Dim m As Boolean = 1 If m = -1 Then MsgBox("true") Else MsgBox("false") End If I know that using "True" or "False" is best practise but for better understanding i need this Thanks in advance Regards Azeem

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            0 = nothing = false, the rest is always true :) A more proper boolean;

            enum MyBool = {
            true,
            false,
            EFileNotFound}

            I are troll :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups