Cast Integer as Boolean?

  • Thread starter Thread starter Joel Whitehouse
  • Start date Start date
J

Joel Whitehouse

Hello All,

How can I cast a System.Int32 as a boolean in VB.NET?

I know I used to used Cbool(myInt) in VB6, but I cxan't seem to find the
.NET equivalent.

-Joel
 
Dim temp As Int32 = 1
Dim temp2 As Boolean
temp2 = System.Convert.ToBoolean(temp)
 
David said:
Dim temp As Int32 = 1
Dim temp2 As Boolean
temp2 = System.Convert.ToBoolean(temp)

Thanks so much for your help! Have a great day!

-Joel
 
Joel,

Don't mixup in VBNet the single System namespace with .Net
The framework has more things than that.
By instance the Microsoft.VisualBasic namespace

The correct (most optimized) VBNet notation is:

Dim IsFalse As Boolean = CBool(New System.Int32)

I hope this helps,

Cor
 
Hello All,

well this is probably some gassoline on this thread but
i doubt that this is a true statement
The correct (most optimized) VBNet notation is: Dim IsFalse As Boolean =
CBool(New System.Int32)


why should

this be (more optimized)
IsFalse = CBool(test)
as this
IsFalse = System.Convert.ToBoolean(test)
or this
IsFalse = CType(test, Boolean)

isn`t it so that this are just different ways of doing the same ,, will it
not produce the same IL code ?

ps.

Cor your example will always return false ,,, so it would be even more
optimized to define it as a false boolean contstant ;-)


Regards

Michel Posseth [MCP]
 
Michael,
isn`t it so that this are just different ways of doing the same ,, will it
not produce the same IL code ?

If there is no more optimizing possible, than there is probably no more
optimizing the VisualBasic Convert functions.

However is it than not still the most optimized one, all was it alone that
somebody else had taken the time to check for that?

:-)))

Cor
 
ofcourse there are always ways to optimize code ,, in the code flow for
instance , however in builtin method \ function calls i doubt that


are you telling me that somebody benchmarked this ??? where can i find this
just for my interest ?

regards

Michel
 
m.posseth said:
ofcourse there are always ways to optimize code ,, in the code flow
for instance , however in builtin method \ function calls i doubt
that


are you telling me that somebody benchmarked this ??? where can i
find this just for my interest ?



CBool and CType are keywords, not function calls. They are (IL-)compiled
inline, at least using Boolean as destination and Integer as the source data
type. CBool and CType behave excactly the same in this case (CBool is
shorter). The other one is a function call. Depending on the optimization
settings, the functin call might also be (JIT-)compiled inline (I love this
:-) ). In this case it is done for System.Convert.ToInt32:


isfalse = CBool(test)
00000022 test eax,eax
00000024 setne bl
00000027 movzx ebx,bl

isfalse = System.Convert.ToBoolean(test)
00000060 test eax,eax
00000062 setne bl
00000065 movzx ebx,bl


isfalse = CType(test, Boolean)
0000009e test eax,eax
000000a0 setne bl
000000a3 movzx ebx,bl

So we never know what's actually fastest. ;-)


Armin
 
Joel,
As Cor & Armin suggests,

| How can I cast a System.Int32 as a boolean in VB.NET?
CBool is a valid .NET VB.NET keyword.

Convert.ToBoolean will always result in a call to another routine (that the
JIT may or may not inline).

CBool(Int32) is an IL statement.

For details of using CBool verses calling Convert.ToBoolean see:

http://www.panopticoncentral.net/archive/2004/06/07/1200.aspx

www.panopticoncentral.net is Paul Vick's blog, Paul Vick is the Technical
Lead of the Visual Basic.NET Development team at Microsoft.

Hope this helps
Jay



| Hello All,
|
| How can I cast a System.Int32 as a boolean in VB.NET?
|
| I know I used to used Cbool(myInt) in VB6, but I cxan't seem to find the
| .NET equivalent.
|
| -Joel
 
No i had not yet read this page ,,, now i did ... thank you for pointing me
at it ,, it had some verry interesting info

now i know for sure that

IsFalse = CBool(test)
and
IsFalse = CType(test, Boolean)

produce the same IL code

if you meant with "most optimized" the shortest way of coding the fastest
method you are right ,,, but i might have misunderstood that for "the one
and only fastest method in code execution " this is were my doubts were

regards ( groeten )

Michel Posseth
 

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

Back
Top