Bad Boolean

B

BobAchgill

I tried to use either a CheckBox or Radio Button to set a
Boolean variable but I get Cast error message with either
when I try to use the Boolean in an IF statement.

How can I use the Check box control to set the Boolean
and not get a cast error later in the code with I try to
use the Boolean in an If statement?

Thanks!

Bob


----------------------------

blnGetAllFolders = RadioButton1
blnGetAllFolders = CheckBox1
....
If blnGetAllFolders Then
....
-----------------------------
Resulting Error message:

An unhandled exception of
type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Operator is not valid for Boolean
and RadioButton (or CheckBox).
 
P

Peter Proost

Hi,

try:

blnGetAllFolders = RadioButton1.Checked
blnGetAllFolders = CheckBox1.Checked

hth

Peter
 
P

Phill. W

BobAchgill said:
I tried to use either a CheckBox or Radio Button to set a
Boolean variable but I get Cast error message with either
when I try to use the Boolean in an IF statement.

A Boolean variable can only hold True or False, which can /always/
be used in an IF.
blnGetAllFolders = RadioButton1

RadioButton1 is a variable containing an object reference (to the
RadioButton control). You are assigning this object reference
into the variable blnGetAllFolders.

1) Turn on Option Strict. It really, /really/, *really* is worth it.
(If you're not running with Option Explicit on, I give up!)

2) VB.Net doesn't use default properties like VB "Proper" did.
So,when you now talk about RadioButton1, you really /are/
talking about the RadioButton /itself/, not the Checked property.

In fact, VB did actually tell you this in the Exception:
Additional information: Operator is not valid for Boolean
and RadioButton (or CheckBox).

blnGetAllFolders = RadioButton1.Checked

will work rather better.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top