We help IT Professionals succeed at work.

convert to Int32, Convert to boolean

jsbsudha
jsbsudha asked
on
1,843 Views
Last Modified: 2008-02-01
In C#

How can I convert a Boolean varaiable to integer or double effectively

convert.toint32(boolean variable)
convert.todouble(boolean varaiable)

is working
but I  could not use
(int) or ( double)
could you tell me the effective way to convert
Comment
Watch Question

CERTIFIED EXPERT

Commented:
You can convert with

convert.toint32(boolean variable)

ctype(boolean variable, int32)

directcast

etc.

convert is framework centric, not language centric, while some other methods (like casting or cint) are language centric. Every method has is approach to the dbnull value, so you have to choose the conversion thinking about the possibility of Null values.


CERTIFIED EXPERT

Commented:
The most performant cast is Directcast that doesn't use extraconversion, so invoking :

DirectCast(o, Integer), if o does not contain an Integer, you'll get an exception, whereas CType(o, Integer) will work if o contains a Long, Double, String, etc.

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
I would use this way:

double d = 3.55;
double b = d==0.0?false:true;

similar case for Int32;
Software Engineer
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.