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. C#
  4. boolean variable as null

boolean variable as null

Scheduled Pinned Locked Moved C#
question
20 Posts 9 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.
  • L ltmnm

    Hello all, I am working on debugging a web application. I need to make a boolean value equal to true, false or null. Is it possible to make it null?

    J Offline
    J Offline
    Justin Perez
    wrote on last edited by
    #2

    I don't believe you can set a boolean to null. Why do you need to set it to null, why will false not suffice?

    I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

    C 1 Reply Last reply
    0
    • L ltmnm

      Hello all, I am working on debugging a web application. I need to make a boolean value equal to true, false or null. Is it possible to make it null?

      A Offline
      A Offline
      Abisodun
      wrote on last edited by
      #3

      bool? myBool = null;

      J 1 Reply Last reply
      0
      • A Abisodun

        bool? myBool = null;

        J Offline
        J Offline
        Justin Perez
        wrote on last edited by
        #4

        Duh, same as setting an int to null :-O

        I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

        1 Reply Last reply
        0
        • L ltmnm

          Hello all, I am working on debugging a web application. I need to make a boolean value equal to true, false or null. Is it possible to make it null?

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #5

          You should set set it to false. Boolean is supposed to hold either true or false.

          "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          P L B C 4 Replies Last reply
          0
          • P Paul Conrad

            You should set set it to false. Boolean is supposed to hold either true or false.

            "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

            P Offline
            P Offline
            PhilDanger
            wrote on last edited by
            #6

            If he's going to be grabbing the value from a database, null could be a "valid" entry.

            P B 2 Replies Last reply
            0
            • P Paul Conrad

              You should set set it to false. Boolean is supposed to hold either true or false.

              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

              L Offline
              L Offline
              ltmnm
              wrote on last edited by
              #7

              The problem is that the application is already built to use boolean values. The user has a section where they chose an option from two drop down boxes. Based on what they select there are two check boxes that are either enabled or not. The user wants to see the results of these choices in the reporting as true equal yes and false equaling no and if it is not enabled they want it to show a blank. the way that it is designed right now is it uses a findcontrol on the check boxes, which returns true or false no matter if it is enabled or not. It then takes that true/false and puts it into the database table. I first thought to just put the assignment into an if statement checking to see if it is enabled. If not then I would not assign anything to that value in the table. This will work for the first time creation of this but the user can go back later and change their answers and this will not change the value that is in the table.

              L P 2 Replies Last reply
              0
              • P PhilDanger

                If he's going to be grabbing the value from a database, null could be a "valid" entry.

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #8

                There is always that possibility. But based on his reply to this post, I am not sure if that's the case.

                "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                1 Reply Last reply
                0
                • L ltmnm

                  The problem is that the application is already built to use boolean values. The user has a section where they chose an option from two drop down boxes. Based on what they select there are two check boxes that are either enabled or not. The user wants to see the results of these choices in the reporting as true equal yes and false equaling no and if it is not enabled they want it to show a blank. the way that it is designed right now is it uses a findcontrol on the check boxes, which returns true or false no matter if it is enabled or not. It then takes that true/false and puts it into the database table. I first thought to just put the assignment into an if statement checking to see if it is enabled. If not then I would not assign anything to that value in the table. This will work for the first time creation of this but the user can go back later and change their answers and this will not change the value that is in the table.

                  L Offline
                  L Offline
                  ltmnm
                  wrote on last edited by
                  #9

                  Here is part of the code that I am talking about: CheckBox chkPU = (CheckBox)dgi.FindControl(c_grd_ctrl_id_chkPU); CheckBox chkCP = (CheckBox)dgi.FindControl(c_grd_ctrl_id_chkCP); . . . . vudr.INCL_PERS_USE_IND = chkPU.Checked; vudr.COMN_CRY_PSGR_IND = chkCP.Checked;

                  1 Reply Last reply
                  0
                  • L ltmnm

                    The problem is that the application is already built to use boolean values. The user has a section where they chose an option from two drop down boxes. Based on what they select there are two check boxes that are either enabled or not. The user wants to see the results of these choices in the reporting as true equal yes and false equaling no and if it is not enabled they want it to show a blank. the way that it is designed right now is it uses a findcontrol on the check boxes, which returns true or false no matter if it is enabled or not. It then takes that true/false and puts it into the database table. I first thought to just put the assignment into an if statement checking to see if it is enabled. If not then I would not assign anything to that value in the table. This will work for the first time creation of this but the user can go back later and change their answers and this will not change the value that is in the table.

                    P Offline
                    P Offline
                    Paul Conrad
                    wrote on last edited by
                    #10

                    ltmnm wrote:

                    it is not enabled

                    If it is the checkbox that you are talking about, then you have to check the enabled property if it is true or false. If you need to have the dropdown show a blank when the checkbox is not enabled, then you just need to add the logic to check for that. Don't confuse Checkedproperty with Enabled...

                    "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    L 1 Reply Last reply
                    0
                    • P Paul Conrad

                      ltmnm wrote:

                      it is not enabled

                      If it is the checkbox that you are talking about, then you have to check the enabled property if it is true or false. If you need to have the dropdown show a blank when the checkbox is not enabled, then you just need to add the logic to check for that. Don't confuse Checkedproperty with Enabled...

                      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      L Offline
                      L Offline
                      ltmnm
                      wrote on last edited by
                      #11

                      Sorry, I should be more clear. The drop down boxes are just answers that go into the database. the code also checks to see if the check boxes are checked and then stores that in the database as well. It returns false when the check box is not checked even if the check box is not enabled. The data that is in the table is then queried and used for reporting services. In those reports the user wants to see yes when the check box is checked and no when it is not. They also want to see just a blank for when the check box was not enabled. So, I am just trying to see if there is a way to make a boolean value equal true, false and anything else that could represent the check box not being enabled. Hope that helps clear things up a little. thank you for all your help.

                      P 1 Reply Last reply
                      0
                      • L ltmnm

                        Sorry, I should be more clear. The drop down boxes are just answers that go into the database. the code also checks to see if the check boxes are checked and then stores that in the database as well. It returns false when the check box is not checked even if the check box is not enabled. The data that is in the table is then queried and used for reporting services. In those reports the user wants to see yes when the check box is checked and no when it is not. They also want to see just a blank for when the check box was not enabled. So, I am just trying to see if there is a way to make a boolean value equal true, false and anything else that could represent the check box not being enabled. Hope that helps clear things up a little. thank you for all your help.

                        P Offline
                        P Offline
                        Paul Conrad
                        wrote on last edited by
                        #12

                        The only way you can represent the checkbox not being enabled is testing for CheckBox.Enabled = false ...

                        "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                        L 1 Reply Last reply
                        0
                        • P Paul Conrad

                          The only way you can represent the checkbox not being enabled is testing for CheckBox.Enabled = false ...

                          "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                          L Offline
                          L Offline
                          ltmnm
                          wrote on last edited by
                          #13

                          right, that's why I tried this: if(chkPU.Enabled) vudr.INCL_PERS_USE_IND = chkPU.Checked; else vudr.INCL_PERS_USE_IND = null; problem is that this: vudr.INCL_PERS_USE_IND is expecting a boolean value. I wanted to see if there was an easy way to make this happen to assign any other value other than true or false. I guess I will have to start re-coding everything to use a diff value.

                          M P L 3 Replies Last reply
                          0
                          • L ltmnm

                            right, that's why I tried this: if(chkPU.Enabled) vudr.INCL_PERS_USE_IND = chkPU.Checked; else vudr.INCL_PERS_USE_IND = null; problem is that this: vudr.INCL_PERS_USE_IND is expecting a boolean value. I wanted to see if there was an easy way to make this happen to assign any other value other than true or false. I guess I will have to start re-coding everything to use a diff value.

                            M Offline
                            M Offline
                            Martin 0
                            wrote on last edited by
                            #14

                            Hello, I think the only way to make it clear is to change the type to "int" could be easyly used as an enum also! Or if this is not possible for you, you have to add an additional bool value! All the best, Martin

                            All the best, Martin

                            1 Reply Last reply
                            0
                            • L ltmnm

                              right, that's why I tried this: if(chkPU.Enabled) vudr.INCL_PERS_USE_IND = chkPU.Checked; else vudr.INCL_PERS_USE_IND = null; problem is that this: vudr.INCL_PERS_USE_IND is expecting a boolean value. I wanted to see if there was an easy way to make this happen to assign any other value other than true or false. I guess I will have to start re-coding everything to use a diff value.

                              P Offline
                              P Offline
                              Paul Conrad
                              wrote on last edited by
                              #15

                              Take a look at Martin#'s suggestion. It sounded like to me you needed to populate the dropdowns, there's probably many ways to achieve what you are trying to do.

                              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                              1 Reply Last reply
                              0
                              • P PhilDanger

                                If he's going to be grabbing the value from a database, null could be a "valid" entry.

                                B Offline
                                B Offline
                                BoneSoft
                                wrote on last edited by
                                #16

                                Then you can use DBNull. That's the whole reason that class exists.


                                Try code model generation tools at BoneSoft.com.

                                1 Reply Last reply
                                0
                                • P Paul Conrad

                                  You should set set it to false. Boolean is supposed to hold either true or false.

                                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                                  B Offline
                                  B Offline
                                  BoneSoft
                                  wrote on last edited by
                                  #17

                                  I can think of some situation where I might really want a boolean, but to know that it hasn't been initialized yet. Nullable boolean makes sense sometimes.


                                  Try code model generation tools at BoneSoft.com.

                                  1 Reply Last reply
                                  0
                                  • L ltmnm

                                    right, that's why I tried this: if(chkPU.Enabled) vudr.INCL_PERS_USE_IND = chkPU.Checked; else vudr.INCL_PERS_USE_IND = null; problem is that this: vudr.INCL_PERS_USE_IND is expecting a boolean value. I wanted to see if there was an easy way to make this happen to assign any other value other than true or false. I guess I will have to start re-coding everything to use a diff value.

                                    L Offline
                                    L Offline
                                    Luc Pattyn
                                    wrote on last edited by
                                    #18

                                    to store three possible states one boolean is clearly insufficient. they invented boolean? for this when C# 2.0 was launched but I would also consider using a pair of booleans, one for hasValue, one for value. that seems the most logical approach in your case. Anyhow, you need to make sure the three states somehow get stored in the DB too. :)

                                    Luc Pattyn


                                    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                                    1 Reply Last reply
                                    0
                                    • J Justin Perez

                                      I don't believe you can set a boolean to null. Why do you need to set it to null, why will false not suffice?

                                      I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

                                      C Offline
                                      C Offline
                                      Colin Angus Mackay
                                      wrote on last edited by
                                      #19

                                      Justin Perez wrote:

                                      Why do you need to set it to null, why will false not suffice?

                                      I would imaging that something can be true, something can be false and something can be unknown (hence null). There is a distict semantic difference between false and unknown. I used a language, many years ago, that had the key words true, false and maybe which was used by functions like PointInPolygon(somePoint, somePolygon) the result was true if the point was inside the polygon, false if outside the polygon and maybe if it was on the line.


                                      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

                                      1 Reply Last reply
                                      0
                                      • P Paul Conrad

                                        You should set set it to false. Boolean is supposed to hold either true or false.

                                        "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                                        C Offline
                                        C Offline
                                        Colin Angus Mackay
                                        wrote on last edited by
                                        #20

                                        There is a clear semantic difference between null and false. null typically represents unknown information.


                                        Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

                                        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