473,854 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 23306
Dim temp As Int32 = 1
Dim temp2 As Boolean
temp2 = System.Convert. ToBoolean(temp)

"Joel Whitehouse" <jo************ @gmail.com> wrote in message
news:O%******** ********@TK2MSF TNGP12.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.Visua lBasic 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******** ******@TK2MSFTN GP12.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.Visua lBasic 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******** **********@tk2m sftngp13.phx.gb l...
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*****@nohaus ystems.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*******@free net.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

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

Similar topics

4
10481
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 an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
3
2296
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 DataSet ' Create connection Dim cn As New OleDbConnection With cn .connectionstring =
3
10522
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 columns is a template column where I'd like to evaluate the data brought back from the db and populate the column with a "Y" or "N" depending on the value in the db. Here is the code that is causing it as well as the error (below). ERROR...
1
4921
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 specifiying my code: The Code used in Visual Studio: Function GetRank(ByVal ID As Integer, ByVal Comp As String, ByVal Sec As String, ByVal iDate As Date) As String 'Dim Ret As Integer
4
3844
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 absolutely no added functionality to it.
9
7138
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 when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
21
1935
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 DataSet ' Create connection Dim cn As New OleDbConnection With cn .connectionstring = "provider=Microsoft.Jet.OLEDB.4.0;" & _
6
2322
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 ? John-Arne Lillebø
18
3128
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
9903
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9754
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11044
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10375
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7927
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7084
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5754
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5952
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.