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. GetHashCode()

GetHashCode()

Scheduled Pinned Locked Moved C#
csharpquestion
8 Posts 5 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.
  • D Offline
    D Offline
    deanoA
    wrote on last edited by
    #1

    Hi. I want to implement a program that sends 1 as a numerical value if a checkbox is checked and zero otherwise. So I read MSDN. I read from MSDN that: "The Boolean class implements true as the integer, one, and false as the integer, zero. However, a particular programming language might represent true and false with other values. " I did this in C#: Checkbox1.checked.GetHashCode(); I get the 1 and 0. But is this safe? I'm worried about the part where it says "However, a particular programming language might represent true and false with other values. " I'm afraid this might cause any side effect. Any ideas what Hashcode is? "To teach is to learn twice"

    D F J 3 Replies Last reply
    0
    • D deanoA

      Hi. I want to implement a program that sends 1 as a numerical value if a checkbox is checked and zero otherwise. So I read MSDN. I read from MSDN that: "The Boolean class implements true as the integer, one, and false as the integer, zero. However, a particular programming language might represent true and false with other values. " I did this in C#: Checkbox1.checked.GetHashCode(); I get the 1 and 0. But is this safe? I'm worried about the part where it says "However, a particular programming language might represent true and false with other values. " I'm afraid this might cause any side effect. Any ideas what Hashcode is? "To teach is to learn twice"

      D Offline
      D Offline
      dog_spawn
      wrote on last edited by
      #2

      MSDN link[^] Ever tried doing a search?

      1 Reply Last reply
      0
      • D deanoA

        Hi. I want to implement a program that sends 1 as a numerical value if a checkbox is checked and zero otherwise. So I read MSDN. I read from MSDN that: "The Boolean class implements true as the integer, one, and false as the integer, zero. However, a particular programming language might represent true and false with other values. " I did this in C#: Checkbox1.checked.GetHashCode(); I get the 1 and 0. But is this safe? I'm worried about the part where it says "However, a particular programming language might represent true and false with other values. " I'm afraid this might cause any side effect. Any ideas what Hashcode is? "To teach is to learn twice"

        F Offline
        F Offline
        freshthinking
        wrote on last edited by
        #3

        when it says some languages represent it different, most, frmo my experience, either follow the C# convetion or use 1 to represent true and 0 or less as false (e.g. C). You can easily convert bools to ints using Convert.ToInt32(value). However Convert.ToBoolean will acturately convert 1 to true and 0 to false, but when i tried it, converted -1 to true.

        J 1 Reply Last reply
        0
        • F freshthinking

          when it says some languages represent it different, most, frmo my experience, either follow the C# convetion or use 1 to represent true and 0 or less as false (e.g. C). You can easily convert bools to ints using Convert.ToInt32(value). However Convert.ToBoolean will acturately convert 1 to true and 0 to false, but when i tried it, converted -1 to true.

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          freshthinking wrote: 0 or less as false (e.g. C). Whoa there! In C (and most other languages), false is 0; anything that is not zero is considered true. That is why -1 was converted to true when passed into Convert.ToBoolean. James At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven".

          D 1 Reply Last reply
          0
          • J James T Johnson

            freshthinking wrote: 0 or less as false (e.g. C). Whoa there! In C (and most other languages), false is 0; anything that is not zero is considered true. That is why -1 was converted to true when passed into Convert.ToBoolean. James At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven".

            D Offline
            D Offline
            dog_spawn
            wrote on last edited by
            #5

            Exactly. Amazing how people get confused by 'bool' :rolleyes: But wait, what the heck is the connection between GetHashCode and bool? Is the original poster insane or is it me?

            J L 2 Replies Last reply
            0
            • D deanoA

              Hi. I want to implement a program that sends 1 as a numerical value if a checkbox is checked and zero otherwise. So I read MSDN. I read from MSDN that: "The Boolean class implements true as the integer, one, and false as the integer, zero. However, a particular programming language might represent true and false with other values. " I did this in C#: Checkbox1.checked.GetHashCode(); I get the 1 and 0. But is this safe? I'm worried about the part where it says "However, a particular programming language might represent true and false with other values. " I'm afraid this might cause any side effect. Any ideas what Hashcode is? "To teach is to learn twice"

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              daljv wrote: Any ideas what Hashcode is? GetHashCode is used to provide a value called a 'hash' for classes like System.Collections.Hashtable. These classes use that value to create a collection which can grow but still retain a fast look-up to get the proper value. A hash table is also called a Map by some people because you can map one value on to another. For example, you can use a Hashtable to map a string representing the name of a customer onto an object that contains more information about that customer. Hashtable internally calls GetHashCode on the string so that it can find the customer object quickly. Rather than calling GetHashCode on the Checked property you could just use the ?: operator. Checkbox1.Checked ? 1 : 0; That basically expands out to,

              if( Checkbox1.Checked )
              return 1;
              else
              return 0;

              HTH, James At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven".

              1 Reply Last reply
              0
              • D dog_spawn

                Exactly. Amazing how people get confused by 'bool' :rolleyes: But wait, what the heck is the connection between GetHashCode and bool? Is the original poster insane or is it me?

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                I think the original poster was just looking for something on the Boolean class which returned an int, and GetHashCode just happens to do that. I've replied with what I hope we can agree is a better way of doing what was wanted. James At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven".

                1 Reply Last reply
                0
                • D dog_spawn

                  Exactly. Amazing how people get confused by 'bool' :rolleyes: But wait, what the heck is the connection between GetHashCode and bool? Is the original poster insane or is it me?

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

                  Actually, m not goin insane. hehhe. I was actually looking for a way to convert the boolean method without resorting to using Convert. Anyway, m just exploring C#. Thanks anyway! :)

                  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