473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 9646
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
11
by: tshad | last post by:
I have the following: sub submitQuestion_click(Sender as Object, e as EventArgs) Dim submitButton as Button = CType(sender,Button) This gives me an error that says: "...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
5
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
7
by: | last post by:
I am trying to validate a textfield to ensure it holds a time. I use a try/catch block to see if I can convert to a time & if it works then I blindly 'assume' it was OK. This single line Dim...
1
by: Teresa | last post by:
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,...
5
by: Joergen Bech | last post by:
Basically, I want to convert hex values in the range "00000000" to "FFFFFFFF" to a signed, 32-bit Integer value. In VB6, I could just write lngValue = Val(hexstring$). In VB.Net, I seem to be...
7
by: Joe | last post by:
Hello All: Does anyone know the difference between CType(x,Integer) vs. Integer.Parse(x)? Is there a performance advantage to one or the other? TIA, -- Joe
8
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello Everything in .Net is an object and CTYPE converts one datatype to another datatype so why use Cstr, Cint or any other method to convert from one datatype to another?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.