473,692 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cint and int32.parse

I'm familiar with CInt(value) but I'm not with Int32.Parse(val ue). They seem
to do the same thing. Is there a big difference between the two and when
would one be best applied over the other?

Thanks,
Jim
Nov 21 '05 #1
16 5263
Hi Jim,

According to the documentation, the biggest difference between
CInt/CStr/C<Etc> is that the code to coerce the variable to the correct
type (integer in this case) is compiled inline.

This makes it's performance (apparently) faster than Int32.Parse as
there is no method call.

I disagree with the above statements, because when I compile code that
uses CInt("3"), the CInt() call is converted to
IntegerType.Par seString...and that sure looks like a method call to me
;) Even the IL calls IntegerType.Par seString, so I don't know about all
these "inline" claims.

The built-in conversion functions (C<etc>) do some extra checks before
they try to cast the type. For example, CInt will return 0 if you pass
Nothing, but Int32.Parse will throw an ArgumentNullExc eption.

If you want to see how the "guts" of CInt works, open up Reflector and
load the Microsoft.Visua lBasic assembly. From there, drill down to
Microsoft.Visua lBasic.Compiler Services.Intege rType. The various methods
in that module do the conversion.

Regards,
-Adam.

Jim in Arizona wrote:
I'm familiar with CInt(value) but I'm not with Int32.Parse(val ue). They seem
to do the same thing. Is there a big difference between the two and when
would one be best applied over the other?

Thanks,
Jim

Nov 21 '05 #2
Jim,

To say it very simple.

First of all are it less characters to type.

Second have the Microsoft.Visua l Basic conversion methods in some cases some
extras that can improve speed or automatic handling in situations where you
should need with the parse extra commands.

In fact they should do the same, I find the conversions commands beside the
IDE one of the strongest part of VBNet.

(You did not mention it, there is as well a convert class)

Just my thought,

Cor
Nov 21 '05 #3
"Jim in Arizona" <ti*******@hotm ail.com> schrieb:
I'm familiar with CInt(value) but I'm not with Int32.Parse(val ue). They
seem to do the same thing. Is there a big difference between the two and
when would one be best applied over the other?


In addition to the other replies:

Conversion operators in VB
<URL:http://www.panopticonc entral.net/archive/2004/06/07/1200.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
"Adam Goossens" <ad***********@ gmail.com> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
Hi Jim,

According to the documentation, the biggest difference between
CInt/CStr/C<Etc> is that the code to coerce the variable to the correct
type (integer in this case) is compiled inline.

This makes it's performance (apparently) faster than Int32.Parse as there
is no method call.

I disagree with the above statements, because when I compile code that
uses CInt("3"), the CInt() call is converted to
IntegerType.Par seString...and that sure looks like a method call to me ;)
Even the IL calls IntegerType.Par seString, so I don't know about all these
"inline" claims.

The built-in conversion functions (C<etc>) do some extra checks before
they try to cast the type. For example, CInt will return 0 if you pass
Nothing, but Int32.Parse will throw an ArgumentNullExc eption.

If you want to see how the "guts" of CInt works, open up Reflector and
load the Microsoft.Visua lBasic assembly. From there, drill down to
Microsoft.Visua lBasic.Compiler Services.Intege rType. The various methods in
that module do the conversion.

Regards,
-Adam.


That is some very good insight into the subject. I'm new to VB.NET and was
still fairly new with VB6 before I decided to go with .NET and try not to
look back.

I was reading a book on VB.NET and the author used CInt() in an early
chapter then was using Int32.Parse() in subsequent chapters, without
explaining why he chose one over the other.

I'm not familiar with Reflector or the assembly so your last paragraph went
wooosh right over my head. I'll get there someday.

Thanks Adam,
Jim
Nov 21 '05 #5
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uo******** ******@TK2MSFTN GP12.phx.gbl...
"Jim in Arizona" <ti*******@hotm ail.com> schrieb:
I'm familiar with CInt(value) but I'm not with Int32.Parse(val ue). They
seem to do the same thing. Is there a big difference between the two and
when would one be best applied over the other?


In addition to the other replies:

Conversion operators in VB
<URL:http://www.panopticonc entral.net/archive/2004/06/07/1200.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Following your document, I ended up here:

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

And when you take a look at the appendix at the bottom, you'll see that the
recommendation is to go ahead and use CType for most conversions.

Thanks,
Jim
Nov 21 '05 #6
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Jim,

To say it very simple.

First of all are it less characters to type.

Second have the Microsoft.Visua l Basic conversion methods in some cases
some extras that can improve speed or automatic handling in situations
where you should need with the parse extra commands.

In fact they should do the same, I find the conversions commands beside
the IDE one of the strongest part of VBNet.

(You did not mention it, there is as well a convert class)

Just my thought,

Cor


I didn't know there was a convert class as well, so I gave that a try:
Convert.ToInt32 ()
I don't quite understand why there would be so many different ways to
achieve the same result. I find that it makes learning the language much
more time consuming.

If I use CInt(), is the default data type a 32 bit integer?
Nov 21 '05 #7
Jim,
If I use CInt(), is the default data type a 32 bit integer?


Where you can can, you would you use in Net the Integer, which is at the
moment the Int32. That is confirming the processer word lengtht and
therefore the most efficient.

CInt does that Integer.

I hope this helps,

Cor

Nov 21 '05 #8
Jim,

The Convert.To<Type > functions are designed to be used from any language
in the framework. It's a central "one stop shop for all your intrinsic
type conversion needs". :)

The CInt/CStr/CBool/Etc functions are mostly to maintain compatibility
with the previous versions of Visual Basic.

The biggest advantage of the functions in the Convert class is that they
provide you with a lot more choices when you perform the conversion, for
example:

---
Dim i as Integer

i = Convert.ToInt32 ("0xFF", 16) ' converts the Hexadecimal number 0xFF
to an integer (255).
---

As long as you stick with the CInt/CStr/Etc functions for your basic
type conversion, you'll have no troubles at all. The functions in the
Convert class are really for the more specialized needs (like the
conversion from a hex string to an integer above).

Regards,
-Adam.

Jim in Arizona wrote:
I didn't know there was a convert class as well, so I gave that a try:
Convert.ToInt32 ()
I don't quite understand why there would be so many different ways to
achieve the same result. I find that it makes learning the language much
more time consuming.

If I use CInt(), is the default data type a 32 bit integer?

Nov 21 '05 #9
On 2005-04-12, Adam Goossens <ad***********@ gmail.com> wrote:
Jim,

The Convert.To<Type > functions are designed to be used from any language
in the framework. It's a central "one stop shop for all your intrinsic
type conversion needs". :)

The CInt/CStr/CBool/Etc functions are mostly to maintain compatibility
with the previous versions of Visual Basic.


The only thing I'd add is that the CInt/CStr functions aren't really
functions, they're language keywords, and will result in different code
depending on the declarations of their parameters.

I'd agree that it doesn't much matter which of the many available
conversion techniques one uses, although I do think that using a
CInt/Cstr/CType call when you really want a cast is a bad habit, and a
pretty common one.

Nov 21 '05 #10

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

Similar topics

2
8609
by: Tu-Thach | last post by:
Ron, What are you trying to do? Read from a database? This error indicates that you are trying to read an int, but the value is outside the range of an integer: - 2,147,483,648 through 2,147,483,647. Tu-Thach >-----Original Message----- >Hi,
3
11138
by: Mike P | last post by:
Can anybody convert this from VB.NET to C#? If Int32.Parse(ID) = Int32.Parse(dt.Rows(iLoop)("ID")) then Return iLoop End If Thanks, lfc77
6
15025
by: Jeff Reed | last post by:
Doing something like this (cast) doesn't work. Y.value = (Int32)stringvariable; I get this message: Cannot convert type 'string' to 'int' Any idea how I can convert string to Int32? thxs
2
1779
by: Kenneth | last post by:
What's the different between this CInt(iID) Integer.Parse(iID)
15
27842
by: Brian Henry | last post by:
Which one is better to use? CInt(string) or Integer.Parse(string)? thanks!
18
4866
by: Atara | last post by:
In my apllication I use the following code: '-- My Code: Public Shared Function strDate2Date(ByVal strDate As String) As System.DateTime Dim isOk As Boolean = False If (strDate Is Nothing) Then isOk = False ElseIf Not (strDate.Length() = 6) Then isOk = False
4
15331
by: Rich | last post by:
Hello txtID.Text contains a 6 digit Integer number with no decimals, no chars... Dim j As Integer = Cint(txtID.Text) or Dim j As Integer = Convert.ToInt32(txtID.Text)
3
2176
by: psycho | last post by:
what is the difference between the two forms of converting to integer from string string strVal = "10"; Convert.ToInt32(strVal); Int32.Parse(strVal); which method is more efficient in terms of performance?
0
8610
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
8544
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
9090
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...
1
8810
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8810
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...
0
7633
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
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
4559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.