C#'s equivilant of PHP's isset()

Hey all,

Is there such an equivilant? Here is what I’m trying to do, maybe there’s a better way to do it

$var = ( isset($lowest) ? $lowest : 1 );

Heres what I have but I want to compensate for if the values aren’t set.

lowest = Convert.ToInt32(txtLowest.Text);
highest = Convert.ToInt32(txtHighest.Text);

Thanks for the help :slight_smile:

You could just catch the nullreferenceexception.

Ahhhhh, care to share a code example?

I started with C# yesterday :wink:

Ah, okay, here goes. I’ll also explain exceptions, if you don’t know what it is. Whenever there is an error in your application while running, C# throws an exception. Now, if you don’t do anything with that exception, it will eventually end up on the users screen as an error. However, you can catch it, which is done like this:

try {
  lowest = Convert.ToInt32(txtLowest.Text);
  highest = Convert.ToInt32(txtHighest.Text);
catch (NullReferenceException e) {
  // Stuff to do if the code inside the try-block produces an error.

Exception handling is one of the coolest things in ASP.NET, IMHO. If you don’t catch an exception, it will be thrown along up one level (i.e. if the error is produced inside a method, and not caught there, it will be thrown out of the method, and see if it’s catched there. It will actually be thrown all the way up to Application_Error in Global.asax. This is very nifty, since you can write an error handler there for all unhandled exceptions, and email them to yourself. Reeeally cool.

:lol: I’m familiar with the idea of using exceptions from playin around with try/catch in PHP5 but no real clue about exceptions themselves. I take it now I have that code I can head over to the .NET Documentation and look up some stuff I can do with NullReferenceException?

I’ll do that now and post back if I run into difficulties.

Thanks :slight_smile:


I got this error so I changed e to foo. Figured it was already declared somewhere and the easiest thing to do would be to change e to something else :wink: If I’ve done something wrong, let me know.

Compiler Error Message: CS0136: A local variable named ‘e’ cannot be declared in this scope because it would give a different meaning to ‘e’, which is already used in a ‘parent or current’ scope to denote something else

Well, you really don’t have to do anything with the NullReferenceException at all, really.

try {
  lowest = Convert.ToInt32(txtLowest.Text);
  highest = Convert.ToInt32(txtHighest.Text);
catch (NullReferenceException e) {
  lowest = 1;
  highest = 99999999;


OK, well it’s still not liking me. I’m online at the moment so you have a go, here is a link. Try not entering any numbers. [url=]Here is the same file saved with .txt so you can see all the source.

First of all - change
lblError.Text = e.ToString();
lblError.Text = foo.ToString();

so we have that out of the way. Also, what is the error? As I’m coming from a remote location, I cannot see the error details.

Lol, thanks for pointing that out. Nifty that only local users can see the details :wink:

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 10:
Line 11: try {
Line 12: lowest = Convert.ToInt32(txtLowest.Text);
Line 13: highest = Convert.ToInt32(txtHighest.Text);
Line 14: } catch ( NullReferenceException foo ) {

Source File: C:\Inetpub\ASP_NET\ esting\rnd.aspx Line: 12

Stack Trace:

[FormatException: Input string was not in a correct format.]
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +0
System.Convert.ToInt32(String value) +47
ASP.rnd_aspx.Button_Click(Object sender, EventArgs e) in C:\Inetpub\ASP_NET\ esting\rnd.aspx:12
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277

ASP.NET is very nifty overall. :wink:

Anyway, notice that it throws a FormatException and not a NullReferenceException, which I thought it would. The solution is to either make the catch block catch FormatExceptions:

catch (FormatException fe) {

or to make it catch all exceptions:

catch (Exception foo) {

You can also have multiple catch statements for different exceptions, if you like.

Lovely!! Thank you so much!

Also just a quick question, since I installed the .NET Framework and admittedly had a play with Directory Security in IIS, now normal ASP pages (ie. localstart.asp) won’t work. Any ideas for that? I don’t really have a use for old-ASP but if something is there I like it to work.

Thanks again for helping with the exceptions :tup:

Sorry, have never really worked with ASP.old, and I’m also pretty dumbfounded on IIS configuration.