Use CType or Integer.Parse to parse a string into an integer

G

Guest

1) Should I use Integer.Parse to convert a string into an integer in .NET
now?
CType(sUserID, Integer) OR Integer.Parse(sUserID)

2) And is it better to use the string class to trim, get length, etc in .NET?
s1.trim OR trim(s1)
s1.length OR len(s1)

Thank you!
 
W

W.G. Ryan eMVP

1) There's no CType in C# so on gp - I'd run with Parse - unless you are
sure you're never going to switch between the two languages
2) use the string class- avoid the VB compatibility library as much as
possible.
 
C

Chris, Master of All Things Insignificant

Why do you say to avoid the VB compatibility library? Is there a technical
reason or just a philosophical reason?

Chris

PS. CType(String, Integer) is equivalent to (int)String in C#
 
H

Herfried K. Wagner [MVP]

Bill,

W.G. Ryan eMVP said:
2) use the string class- avoid the VB compatibility library as much as
possible.

"Microsoft.VisualBasic.dll" <> "Microsoft.VisualBasic.Compatibility.dll"!
 
H

Herfried K. Wagner [MVP]

Chris said:
Why do you say to avoid the VB compatibility library? Is there a
technical reason or just a philosophical reason?

I would use 'CInt' instead of 'CType(String, Integer)' because it's fewer to
type.
 
C

Cor Ligthert

Chris,

You should avoid the VB "compatiblity" library because it is said that it
will be removed in future. What is not of course the situation for the VB
library.

Cor
 
C

Cor Ligthert

Bill
2) use the string class- avoid the VB compatibility library as much as
possible.

You are talking about the VisualBasic class, I made in the general
newsgroup a short while ago this sample from a sample from Greg Burns and I
think it is very good use of the VisualBasic dll

\\\
DateTime mydate;
mydate = DateTime.Now;
string msg;
msg = "Quarter: " + DateAndTime.DatePart(DateInterval.Quarter,
mydate,FirstDayOfWeek.Monday,FirstWeekOfYear.Jan1);
///

I hope this gives the idea why it is not right to avoid the very strong
VisualBasic namespace.

Cor
 
C

Cor Ligthert

Terresa,

In addition to Herfried in this thread I would use CInt

Search on this page for Cint.
http://msdn.microsoft.com/library/d...tml/vbtchmicrosoftvisualbasicnetinternals.asp

And you will read at the 3th find
Conversion Functions, CType, DirectCast, and System.Convert
Visual Basic .NET includes data type conversion keywords, many of which are
carried over from Visual Basic 6. But unlike the Visual Basic 6 functions,
these keywords are not function calls but intrinsic language features. The
keywords CBool, CByte, CChar, CShort, CInt, CLng, CSng, CDbl, CDec, CDate,
CObj, and CStr map to Visual Basic Runtime method calls, .NET Framework
class library method calls, or IL type conversion instructions. The exact
method call or IL instructions generated depends on the expression against
which the conversion is being applied. Some conversions are optimized away,
such as CInt(123.45) which is replaced with the integer constant 123 in the
IL.

I hope this helps

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top