GetHashCode()
-
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"
-
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"
-
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"
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. -
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.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".
-
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".
-
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"
daljv wrote: Any ideas what Hashcode is?
GetHashCode
is used to provide a value called a 'hash' for classes likeSystem.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 aHashtable
to map a string representing the name of a customer onto an object that contains more information about that customer.Hashtable
internally callsGetHashCode
on the string so that it can find the customer object quickly. Rather than callingGetHashCode
on theChecked
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".
-
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?
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".
-
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?