PC Review


Reply
Thread Tools Rate Thread

Cast Integer as Boolean?

 
 
Joel Whitehouse
Guest
Posts: n/a
 
      4th Aug 2005
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
 
Reply With Quote
 
 
 
 
David A. Osborn
Guest
Posts: n/a
 
      4th Aug 2005
Dim temp As Int32 = 1
Dim temp2 As Boolean
temp2 = System.Convert.ToBoolean(temp)

"Joel Whitehouse" <(E-Mail Removed)> wrote in message
news:O%(E-Mail Removed)...
> 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
>




 
Reply With Quote
 
Joel Whitehouse
Guest
Posts: n/a
 
      4th Aug 2005
David A. Osborn wrote:

> 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
 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      5th Aug 2005
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


 
Reply With Quote
 
m.posseth
Guest
Posts: n/a
 
      5th Aug 2005
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]



"Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      5th Aug 2005
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


 
Reply With Quote
 
m.posseth
Guest
Posts: n/a
 
      5th Aug 2005
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




"Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
news:%23K%(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      5th Aug 2005
"m.posseth" <(E-Mail Removed)> schrieb
> 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

 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      5th Aug 2005
"Armin Zingler" <(E-Mail Removed)> schrieb
> :-) ). In this case it is done for System.Convert.ToInt32:


.....done for System.Convert.ToBoolean



Armin



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      5th Aug 2005
Michel,

Do you know this page?

http://msdn.microsoft.com/library/de...tinternals.asp

And than about this in the section
Conversion Functions, CType, DirectCast, and System.Convert

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reasons to use (Boolean) or (Integer) in VBA code? Clif McIrvin Microsoft Access Forms 10 8th Apr 2008 05:34 AM
Cast to Boolean Darren Microsoft Access Queries 9 12th Jun 2007 07:11 PM
How to cast Boolean[] to Object[] fj Microsoft Dot NET 1 20th Dec 2006 06:29 AM
Access Database boolean and invalid cast =?Utf-8?B?c3VidA==?= Microsoft ADO .NET 2 22nd Mar 2004 02:37 PM
boolean to integer sphenisc Microsoft Excel Programming 4 13th Jan 2004 12:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 AM.