Cint or Integer.Parse?

  • Thread starter Thread starter Brian Henry
  • Start date Start date
Which one is better to use? CInt(string) or Integer.Parse(string)? thanks!

Depends on whether you want to retain the VB "feel" or go hardcore Framework
all the way.
 
been trying to knock out all the old vb stuff out of my head so sounds like
the parse one is good to go with :)
 
* "Brian Henry said:
been trying to knock out all the old vb stuff out of my head so sounds like
the parse one is good to go with :)

'CInt' is not "old vb". It's current VB syntax that /should/ be used
/instead/ of the framework functions whenever possible. That's why we
have these commands.
 
I agree that Integer.Parse is the wrong thing to use - but CType(xx,
Integer) is functionally identical to CInt - and if you happen to not have
Microsoft.VisualBasic referenced... it's fine :D
_______________________________
The Grim Reaper
 
* "The Grim Reaper said:
I agree that Integer.Parse is the wrong thing to use - but CType(xx,
Integer) is functionally identical to CInt - and if you happen to not have
Microsoft.VisualBasic referenced... it's fine :D

Both, 'CInt' and 'CType' are VB /keywords/ that should work without
referencing this library.
 
any reason specific reason why you suggest i should use CInt instead of the
framework versions? I'm trying to maintain as much compatability in tearms
of code with C# incase they ever want this code directly coverted over to
it..
 
Hi Brian,

One of the things in the language itself where VB is much better than all
the other languages in dotNet are the *convert* functions.

You have the change to use that benefit, not using them is for me the same
as getting a million and not accepting it because it is not given to
everybody.

Just my thougth,

Cor
 
* "Brian Henry said:
any reason specific reason why you suggest i should use CInt instead of the
framework versions? I'm trying to maintain as much compatability in tearms
of code with C# incase they ever want this code directly coverted over to
it..

Readability. Code is more compact, there is no useless stuff like
'Parse'. It's like using '+' instead of 'op_Addition'. If we start
using the greatest common subset of language features in our code,
language interoperability and more than one .NET programming language
are useless.
 
Brian,
Because CInt may actually be faster then Integer.Parse or Convert.ToInt.

http://www.panopticoncentral.net/archive/2004/05/31/1100.aspx

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


Using Integer.Parse, "cause its more C#" may actually be developing bad
habits. In that you will use Integer.Parse or Convert.ToInt "cause its more
..NET", where as if this was C# code I am sure you would have no problems
using an (int) cast. Guess what CInt is both an (int) cast & a
Convert.ToInt! VB.NET picks the correct on based on usage. Somehow that
makes it more preferred in my book, rather then less preferred... More RAD
somehow...
I'm trying to maintain as much compatibility in terms
of code with C# incase they ever want this code directly converted over to
it..
I would worry about conversion when the conversion actually happened,
allowing me to write "correct" VB.NET code, by "correct" I mean use the
correct feature for the correct problem. If you want to use VB.NET to write
C# code, I would strongly recommend you use C# to write C# code!

As a friend of mine says: avoiding CInt in VB.NET is like buying a new car
and avoiding using the power windows.

Just a thought
Jay


Brian Henry said:
any reason specific reason why you suggest i should use CInt instead of the
framework versions? I'm trying to maintain as much compatability in tearms
of code with C# incase they ever want this code directly coverted over to
it..
 
Brian Henry said:
Which one is better to use? CInt(string) or Integer.Parse(string)? thanks!

Another thing to consider is that CInt will convert the string
"1,000", whereas Integer.Parse will not; you'll get an exception.

Marc.
 
Sorry. Ignore my previous message.
You can use something like Integer.Parse("1,234",
Globalization.NumberStyles.Any) to convert that string.
 
* Marc said:
thanks!

Another thing to consider is that CInt will convert the string
"1,000", whereas Integer.Parse will not; you'll get an exception.

That depends on your system's number style settings...
 
Yes, you're quite right. I must have been more tired than I thought at 1am
this morning!!
I was thinking of the Asc() function - sorry.
___________________________________
The Grim Reaper
 
Back
Top