473,500 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Nov 17 '05 #1
10 5853
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.

"Flip" <[remove_me]ph******@hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP14.phx.gbl...
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.

Nov 17 '05 #2
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,

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

"Flip" <[remove_me]ph******@hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP14.phx.gbl...
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.

Nov 17 '05 #3
KH
> 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" wrote:
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.

Nov 17 '05 #4
KH
> 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" wrote:
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.

Nov 17 '05 #5
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! :>
Nov 17 '05 #6
> 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.
Nov 17 '05 #7
> 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.
Nov 17 '05 #8
> 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.
Nov 17 '05 #9
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,

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

Nov 17 '05 #10
KH
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"]);
}

"Flip" wrote:
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.

Nov 17 '05 #11

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

Similar topics

3
1867
by: Peter Olcott | last post by:
Everything in this program produces the correct results except the part dealing with the RealList. The "for" loop does not output the values that were input to the ALL.RealList. What is the correct...
15
122072
by: Gérard Talbot | last post by:
Hello all, I'd like to know and understand the difference between, say, <img src="/ImageFilename.png" width="123" height="456" alt=""> and <img src="/ImageFilename.png" style="width:...
134
7745
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
6
6615
by: craigbeanhead | last post by:
Hi, I'm teaching myself C from K&R2. I've come across something that I really don't understand. When I try to compile the following code (in VC++7), I get an "undeclared identifier" error. When...
17
2387
by: Chen Shusheng | last post by:
Hi all, In fact, I want to let my memory run out. And see what will happen. My system is windowsXp. Memory is 256M.I think my cdes will apply more memory than I have. Codes are below: ...
12
3191
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
11
2218
by: arnuld | last post by:
i have created a new temperature conversion programme. it converts a temperature from Fahrenheit to Celsius or from Celsius to Fahrenheit at user's will. tell em if i need some improvements: ...
3
1781
by: =?Utf-8?B?UmljaA==?= | last post by:
I need to build a sql string that looks like this: strSql = "Select * from tbl1 Where x In (123,456,789)" or strSql = "Select * from tbl1 Where x In (123,456,789,527,914)" The numbers...
0
7182
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
7232
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...
1
6906
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
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4923
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...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.