473,406 Members | 2,705 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,406 software developers and data experts.

Give me an Int

int.Parse() OR Convert.ToInt32() OR (int)

Hi,

Is there any sort of convention as to when its appropriate to use
int.Parse(), Convert.ToInt32() or casting to int?

At the moment I am using them interchangeably. If one doesnt work, i try
another one.

I know that int.Parse is for creating an int from a string, but for
Convert.ToInt32() would do the same thing.

What are real life instances when one should be preferred over the other.?
thank you..
Nov 17 '05 #1
6 1132
=?Utf-8?B?Q29kZVJhem9y?= <Co*******@discussions.microsoft.com> wrote in
news:6A**********************************@microsof t.com:
Is there any sort of convention as to when its appropriate to use
int.Parse(), Convert.ToInt32() or casting to int?
If from a string, its better to Parse (ie parse or Convert)
I know that int.Parse is for creating an int from a string, but for
Convert.ToInt32() would do the same thing.


Convert calls Parse, tehy are nearly identical.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 17 '05 #2
Hi,

Convert and Int.Parse are similar, with one important exception, if you are
converting a String and this string is null it will give you an exception,
wheather Convert will give you 0

a cast is a completely different thing though. It will work only if the
instance is a boxed int, or a boxed/unboxed value type that can be
converted implicitely to int .

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Chad Z. Hower aka Kudzu" <cp**@hower.org> wrote in message
news:Xn**************************@127.0.0.1...
=?Utf-8?B?Q29kZVJhem9y?= <Co*******@discussions.microsoft.com> wrote in
news:6A**********************************@microsof t.com:
Is there any sort of convention as to when its appropriate to use
int.Parse(), Convert.ToInt32() or casting to int?


If from a string, its better to Parse (ie parse or Convert)
I know that int.Parse is for creating an int from a string, but for
Convert.ToInt32() would do the same thing.


Convert calls Parse, tehy are nearly identical.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/

Nov 17 '05 #3
thanks Ignacio. I didnt know the important difference between int.parse and
Convert. cheers, that will be useful in the future.

The final thing you said about casting and boxing Ints, lost me a bit. Could
you demonstrate what you mean.

thank you very much
Nov 17 '05 #4
=?Utf-8?B?Q29kZVJhem9y?= <Co*******@discussions.microsoft.com> wrote in
news:75**********************************@microsof t.com:
The final thing you said about casting and boxing Ints, lost me a bit.
Could you demonstrate what you mean.


Boxing is a way of represenignt value types as objects. If you have a string, and you want it converted
to an int, use parse or convert.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 17 '05 #5
Think of it this way: you can cast to an int only if it's already a
numeric type, or an object representing a numeric type (that's what
boxed means). So, for example,

double d = 15.23;
int i = (int)d;

is legal because d is numeric and can be converted to an int. The cast
is required because information will be lost (the 0.23).

int a = 15;
object o = a;
int b = (int)o;

is legal because o is a reference to a _boxed_ int: an int that has
been converted to an object so that it can be used like an object.
Since the object that o refers to is already an int, you can cast it to
an int with no problems.

I always prefer to cast if I can because it's cleaner and easier to
read. yes, I could say:

double d = 15.23;
int i = Convert.ToInt32(d);

but it's longwinded and doesn't buy me anything. I reserve Convert and
Parse for strings, and use casting for all numeric conversions.

Nov 17 '05 #6
excellent thorough explanation. I really get it now.

cheers Bruce.
Nov 17 '05 #7

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

Similar topics

21
by: Leonardo | last post by:
I have the following associative array: $user=1; $user=1; $user=0; $user=1; $user=0; $user=1; $user=1;
10
by: techy techno | last post by:
Hiii Hello.. I just wanted to know if anyone can tell me how can I give my website visitors the feature of "FRIENDLY PRINTING" through IE. I would definitely like to give a feature like...
3
by: Marc Walgren | last post by:
Greetings I have an ASP application to enter reservations. There are multiple user security settings that require some users to have a restricted list of client in a drop list on a form. I...
1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
6
by: vb. | last post by:
Why we give a function data type? when we declare a function we gave a name and datatype for that function what for? if i make a parameter i declare it and give it a datatype and if i use variabels...
11
by: Wayne | last post by:
I am a one man enterprise and have been asked by a prospective client what happens to their database regarding ongoing changes etc if I get hit by a bus. Obviously my databases are distributed...
25
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does parseInt('09') give an error? ----------------------------------------------------------------------- ...
1
by: Venkat | last post by:
Hi, Is there any way to give feedback to the application while a stored procedure execution is in progress? I am using sql server and my application is built using c#. The stored procedure tries...
3
by: Charlotte | last post by:
Hello, info: I'me a rookie with IIS I have on a WinXP Pro the IIS installed, so I can test some pages before uploading to the hostserver online on the hostserver is a possibility (with the...
2
dlite922
by: dlite922 | last post by:
This is a totally n00b question but I'm saying good bye to Windows slowly and I've come across where I need to own a directory but I want to give write permission to Smarty (Apache user). Right now I...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...
0
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...
0
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,...
0
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...

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.