what is difference between int.Parse("123") and Convert.ToInt32("456")?

  • Thread starter Thread starter Flip
  • Start date Start date
F

Flip

I know the int.Parse("123") will result in an int of 123, but what happens
with a null? I believe it give a null exception (seems like I get either
NullArgumentException or ArgumentNullException if I'm running it in a
console app or in a web app, what's up with that?).

I'm trying to get a counter (int) value out of the Application object. When
I do the Convert.ToInt32(Application["PageCounter"]), the first time (it's
null), the method converts it to zero, great. But when I use the
int.Parse(Application["PageCounter"] and check for == null, it works the
first time, but not the second time (when it has a value). Huh?

Any help would be greatly appreciated. Thank you.
 
In Application.Start initialize the variable to 0. From then on, it will
always have an integer value. You can always safely retrieve it, and know
you will have a valid integer.
 
Hi,

Insert ( if you do not have it ) a global.asax file, the in the
Application_Start method set it to 0, in this way you will be sure that it
always exist.

Weird that of Int32.Parse it should give the same no matter what, I bet you
have another error somewhere.

No idea what you are doing but Application will be accessed from any thread
( request ) so you should keep especial attention to concurrency issues.


cheers,
 
I know the int.Parse("123") will result in an int of 123, but what happens
with a null?

Why don't you just test it? ...

// In void Main() ...

try {
int i = Int32.Parse("-1");
Console.WriteLine("Parse did not throw; i = {0}", i);
}
catch(Exception ex)
{
Console.WriteLine("Parse threw, message = {0}", ex.Message);
}


Flip said:
I know the int.Parse("123") will result in an int of 123, but what happens
with a null? I believe it give a null exception (seems like I get either
NullArgumentException or ArgumentNullException if I'm running it in a
console app or in a web app, what's up with that?).

I'm trying to get a counter (int) value out of the Application object. When
I do the Convert.ToInt32(Application["PageCounter"]), the first time (it's
null), the method converts it to zero, great. But when I use the
int.Parse(Application["PageCounter"] and check for == null, it works the
first time, but not the second time (when it has a value). Huh?

Any help would be greatly appreciated. Thank you.
 
I know the int.Parse("123") will result in an int of 123, but what happens
with a null?

Should have had a null case in there too...

// Wrap in try/catch like my previous message...

string s = null;
Int32.Parse(s);


Flip said:
I know the int.Parse("123") will result in an int of 123, but what happens
with a null? I believe it give a null exception (seems like I get either
NullArgumentException or ArgumentNullException if I'm running it in a
console app or in a web app, what's up with that?).

I'm trying to get a counter (int) value out of the Application object. When
I do the Convert.ToInt32(Application["PageCounter"]), the first time (it's
null), the method converts it to zero, great. But when I use the
int.Parse(Application["PageCounter"] and check for == null, it works the
first time, but not the second time (when it has a value). Huh?

Any help would be greatly appreciated. Thank you.
 
No idea what you are doing but Application will be accessed from any
thread ( request ) so you should keep especial attention to concurrency
issues.
The ultimate objective is to learn. :> So if you have any suggestions, I'm
open. :>

re concurrency issue
Yup, I'm locking/unlocking the sessions while getting/writting the counter
value. Hinders performance a tad, but like you say, concurrency is the
issue.

Thanks.
 
Why don't you just test it? ...
I tried that, but what I was getting was an error on the second time through
when it indeed have something valid. Also I'm getting different exception
from a testing console application than from IIS. I wonder why that is? I
still get exceptions thrown either way, but they are different exceptions.
Weird that I need to catch something when ideally I would like to check for
null, otherwise go ahead with the conversion (assuming, for now at least)
that the value is a valid int.
 
In Application.Start initialize the variable to 0. From then on, it will
always have an integer value. You can always safely retrieve it, and know
you will have a valid integer.
Thank you for your reply. I will do that as my next step, but for now, I'm
focusing in on the string to int conversion. Next I will look at the better
way of implementing this type of pattern/solution. Yes, I know your answer
will certainly solve my problem (and another one I believe, I have a problem
of the value getting reset to null/zero when the application pool resets the
application and removes it from memory, ideas for me there?), but as I
mentioned, I would like to focus on this one small issue first.

Thanks.
 
Thanks for the response.
Insert ( if you do not have it ) a global.asax file, the in the
Application_Start method set it to 0, in this way you will be sure that
it always exist.
That is my next step, but for now, I'm trying to learn about the string to
int conversions.
Weird that of Int32.Parse it should give the same no matter what, I bet
you have another error somewhere.
That's what I was thinking too, but I don't believe so.
No idea what you are doing but Application will be accessed from any
thread ( request ) so you should keep especial attention to concurrency
issues.
Yup, I thought about that and do a lock/unlock on it. :> Thanks for
thinking of something I might not have thought about. :> I appreciate it!
:> Got anymore, I'm all ears! :>
 
Hi,

Yup, I thought about that and do a lock/unlock on it. :> Thanks for
thinking of something I might not have thought about. :> I appreciate it!
:> Got anymore, I'm all ears! :>

Yes, loccking/unlocking could be VERY performance heevy !!! , only one
thread ( web page request ) will be able to access that code at the same
time, so be careful with it.

I had a link about it, but I have no idea where it went to, heck it's
possible that I had it in one of my previous jobs :)

in anyway, be very careful with app wide objects , the same apply to static
methods/variables . A web app is by default multithreaded so a dead lock can
be fatal


cheers,
 
A given methos like Int32.Parse will throw the same exception whether
executed in ASP.NET, COnsole app, windows app, win service... There may be
higher level error handling that you're seeing, but that method will throw
the same exception.

Have you tried stepping thru your code in the debugger -- that's always a
helpful thing to do.

Here's another possible solution to your problem ...

int i = 0;

if (Application["PageCounter"] is System.Int32)
{
i = System.Int32.Parse(Application["PageCounter"]);
}
 
Back
Top