473,558 Members | 2,949 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
NullArgumentExc eption or ArgumentNullExc eption 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["PageCounte r"]), the first time (it's
null), the method converts it to zero, great. But when I use the
int.Parse(Appli cation["PageCounte r"] 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 5860
In Application.Sta rt 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******@hotmai l.com> wrote in message
news:uU******** *****@TK2MSFTNG P14.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
NullArgumentEx ception or ArgumentNullExc eption 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["PageCounte r"]), the first time
(it's null), the method converts it to zero, great. But when I use the
int.Parse(Appli cation["PageCounte r"] 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_Sta rt 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******@hotmai l.com> wrote in message
news:uU******** *****@TK2MSFTNG P14.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
NullArgumentEx ception or ArgumentNullExc eption 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["PageCounte r"]), the first time
(it's null), the method converts it to zero, great. But when I use the
int.Parse(Appli cation["PageCounte r"] 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.WriteLi ne("Parse did not throw; i = {0}", i);
}
catch(Exception ex)
{
Console.WriteLi ne("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
NullArgumentExc eption or ArgumentNullExc eption 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["PageCounte r"]), the first time (it's
null), the method converts it to zero, great. But when I use the
int.Parse(Appli cation["PageCounte r"] 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
NullArgumentExc eption or ArgumentNullExc eption 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["PageCounte r"]), the first time (it's
null), the method converts it to zero, great. But when I use the
int.Parse(Appli cation["PageCounte r"] 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_Sta rt 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.Sta rt 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

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

Similar topics

3
1873
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 syntax to for this? Thanks #include <stdio.h> #include <string> #include <vector> union AllType {
15
122103
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: 123px; height: 456px;" alt="">
134
7788
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 means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you...
6
6622
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 I move the second integer declaration to the beginning of the function, it compiles and runs correctly. I'm sure I read that you could declare a...
17
2400
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: #include <stdlib.h> #include<stdio.h> #define MAX 1000000000
12
3204
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1)...
11
2224
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: // conversion of temperature // using Centigrade and Fahrenheit #include <iostream> int main() {
3
1784
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 represent RecordID's from rows I will select from a datagridview. I tried storing these values in an arrayList, and even a string array and tried this:
0
7629
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7549
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7593
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6183
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5172
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3602
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1164
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.