Forcing Checkbox to Return integer instead of boolean

  • Thread starter Thread starter Shane Story
  • Start date Start date
Using V Web 2005:
I have checkbox on a page. I want to store either 0 if the box is not checked or 1 if it is (as integers - because the sql table
will ultimately be used by other software that can't handle booleans ).
"CBAnswer1.Checked.tostring" gives me "True" or "False" so that won't work. The code below works to do what I
want it to do (using a function to do the actual update to an SQL table) - but there must be a more
direct method to initialize the checkbox so that something that returns an integer 1 instead of the boolean "true" if it is checked
(as I have to do this multiple times).

Dim A1 As Integer = 0
If CBAnswer1.Checked Then
A1 = 1 : End If
ReadAndStoreIntAnswer(A1, "XXX")

Any ideas?

Thanks in advance

Jeff
 
No need to convert to string if you want an integer.
Would you buy icecream if you wanted a roast chicken?
Of course not. Just convert to the types that you need, as below:

Dim A1 as integer = Convert.ToInt32(CBAnswer1.Checked)
ReadAndStoreIntAnswer(A1, "XXX")
 
Hello Jeff,

There is no need at all for any conversion of any kind. Simply tell your
SQL table that this field has a data type of BIT. Then you can pass a BOOLEAN
as the parameter and all is well. It's a 0 or 1 in the database and a True
or False in .NET. Simplicity.

-Boo
 
Steven Nagy said:
No need to convert to string if you want an integer.
Would you buy icecream if you wanted a roast chicken?
Of course not. Just convert to the types that you need, as below:

Dim A1 as integer = Convert.ToInt32(CBAnswer1.Checked)
ReadAndStoreIntAnswer(A1, "XXX")

Yup, that did it.

Thanks.

....will have these basics down soon.

Jeff
 
No problems.

This concept also applies to Enums as well.
For example:

public enum Shopping
RoastChicken = 1
IceCream = 2
JoltCola = 3
end enum

Dim e as Shopping = Shopping.IceCream
Dim s as string = e.ToString()

In this example, s = "IceCream", not "2"

Generally ToString() describes more about the Type than the actual
value.
This applies to your own classes that inherit from Object as well.
However you CAN override the ToString() method to provide more useful
information if necessary (in your own classes that is).
 
No idea what you mean dude. I was converting to an int the same as you did.

cint is the same thing as your convert.toint32
 
Yes I know that.
But I was never replying to you, I was replying to the OP.
I gave them a solution that was easier to understand related to their
original question, thus the reason they responded to me and not you.

Hope there's no hard feelings, I'm just trying to be helpful.
 
GhostInAK said:
Hello Jeff,

There is no need at all for any conversion of any kind. Simply tell your
SQL table that this field has a data type of BIT. Then you can pass a BOOLEAN
as the parameter and all is well. It's a 0 or 1 in the database and a True
or False in .NET. Simplicity.

-Boo


Thanks. I'll remember that one also. ...would have worked given the scenario I actually described in the post, but what I'm doing is
creating a page so that the code and table structure will require minimal alteration if the page changes in the future. One of the
likely changes is that the checkbox may be replaced by a listbox with more than two possible choices. So if the original code
converts to an integer and the table is setup to accept integers, then I don't have to change that code at all if I change from a CB
to something else.

...and Steven was right, his solution was easier for me to understand at my beginning level. I would have figured out Shane's
solution quickly, but I understood Steven's immediately.

Thanks again to all who replied

Jeff
 
Hello Jeff,

That's assinine. You should know what you want to do with something BEFORE
you implement it. A checkbox is for asking questions that have "Yes" / "No"
answers. A question that has a yes/no answer will never have a different
answer. If asked, "Is the sky blue?", the response "Purple." is not an answer.
If what you really want is an enumerated value then you should design the
database and the UI for that from the start.

-Boo
 
"Maybe" is an answer, and checkboxes can be used for more than just
Yes/No.
They can implement 3 options. So you can't make an assumption that
Checkbox has a 1-to-1 relationship with Yes/No options. Similarly,
radios and drop downs could be used instead of checkboxes. For example,
"What is your sex?" with a drop down of male or female is better than a
checkbox with "Are you a male?".

Also, system requirements change. The trick is to write code that is
adaptive and scalable. For example, a good MVP implementation will not
care what control is used to capture your "Yes/No" option.
 
Exactly. Right now, I only need yes/no for this particular problem. I anticipate that I may
need 3 or more categories in the future. I want to be able to adapt the code rapidly if the need changes.
It's a unique application that is difficult to describe quickly. The idea is to take a mostly completed coding template
and adapt it into a few different applications with small changes in the easiest manner possible. In other words,
I may end up with 2 or more applications where each differs only by whether yes/no or yes/no/maybe is required.
I don't want to write two completely different applications and two different table specifications, but one where I can change a
line of code or two and have the second app up and running immediately.
 
Back
Top