473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cast Integer as Boolean?

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
Nov 21 '05 #1
12 23212
Dim temp As Int32 = 1
Dim temp2 As Boolean
temp2 = System.Convert.ToBoolean(temp)

"Joel Whitehouse" <jo************@gmail.com> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #2
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
Nov 21 '05 #3
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
Nov 21 '05 #4
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]" <no************@planet.nl> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl... 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

Nov 21 '05 #5
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
Nov 21 '05 #6
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]" <no************@planet.nl> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #7
"m.posseth" <mi*****@nohausystems.nl> 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

Nov 21 '05 #8
"Armin Zingler" <az*******@freenet.de> schrieb
:-) ). In this case it is done for System.Convert.ToInt32:


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

Armin

Nov 21 '05 #9
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
Nov 21 '05 #10
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/arc...6/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

"Joel Whitehouse" <jo************@gmail.com> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
| 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
Nov 21 '05 #11
Thanks Armin ,,,
for the clear explanation


"Armin Zingler" <az*******@freenet.de> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
"Armin Zingler" <az*******@freenet.de> schrieb
:-) ). In this case it is done for System.Convert.ToInt32:


....done for System.Convert.ToBoolean

Armin

Nov 21 '05 #12
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


"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
21
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
6
by: John-Arne Lillebø | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ?...
18
by: mark | last post by:
class SORef {...}; class SelectedFORef : public SORef {...}; SOFORef SOCastToSOF(SORef sor) { SelectedFORef sof=nil; sof=dynamic_cast<SelectedFORef>(sor); return sof; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.