469,925 Members | 1,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,925 developers. It's quick & easy.

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

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!
Nov 21 '05 #1
15 9425
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#
"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:Oi**************@TK2MSFTNGP11.phx.gbl...
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.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Teresa" <Te****@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
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!


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

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Teresa" <Te****@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
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!

Nov 21 '05 #3
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#
"W.G. Ryan eMVP" <Wi*********@gmail.com> wrote in message
news:Oi**************@TK2MSFTNGP11.phx.gbl...
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.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Teresa" <Te****@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
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!


Nov 21 '05 #4
Bill,

"W.G. Ryan eMVP" <Wi*********@gmail.com> schrieb:
2) use the string class- avoid the VB compatibility library as much as
possible.


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5
Bill,

"W.G. Ryan eMVP" <Wi*********@gmail.com> schrieb:
2) use the string class- avoid the VB compatibility library as much as
possible.


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
schrieb:
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.

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

Nov 21 '05 #7
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
schrieb:
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.

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

Nov 21 '05 #8
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
Nov 21 '05 #9
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
Nov 21 '05 #10
Terresa,

In addition to Herfried in this thread I would use CInt

Search on this page for Cint.
http://msdn.microsoft.com/library/de...tinternals.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
Nov 21 '05 #11
Terresa,

In addition to Herfried in this thread I would use CInt

Search on this page for Cint.
http://msdn.microsoft.com/library/de...tinternals.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
Nov 21 '05 #12
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
Nov 21 '05 #13
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
Nov 21 '05 #14
Yep - you guys are right- my bad. Sorry about that.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Bill,

"W.G. Ryan eMVP" <Wi*********@gmail.com> schrieb:
2) use the string class- avoid the VB compatibility library as much as
possible.


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

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

Nov 21 '05 #15
Yep - you guys are right- my bad. Sorry about that.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Bill,

"W.G. Ryan eMVP" <Wi*********@gmail.com> schrieb:
2) use the string class- avoid the VB compatibility library as much as
possible.


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

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

Nov 21 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Douglas Buchanan | last post: by
11 posts views Thread by tshad | last post: by
6 posts views Thread by Ot | last post: by
7 posts views Thread by Joe | last post: by
8 posts views Thread by =?Utf-8?B?d2lubGlu?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.