GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Sep-2005, 13:19
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 230
Kacyndra will become famous soon enough

C#... Data Input


Hello,
i didn't see a Visual C# thread here, but i thought maybe someone could help me anyway...
this is for an into class, and my question is how to handle expections in C#.
for example, my first program is supposed to find an average of a number of integers entered by the user.

so when the user enters an integer everything is ok, but if a letter is entered it gives me an unhaldeled expection error.
this is how i get the input from the textbox.
C-SHARP / C# Code:
int Input = int.Parse(txtInput.Text);

please hlep.
__________________
Xrum!
  #2  
Old 15-Sep-2005, 21:00
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
First about the forum. You are right in that there is no gidforum designated just for C#. However the most applicable forum for your questions considering C# would be .NET Forum - GIDForums. In the introductory sticky message of that forum it is explained that the Microsofts "managed code" concept including languages C#, Visual Basic .NET and JScript .NET are part of .NET framework.

Since the C# is "Microsofts Java", I would guess that error handling is managed by "try{...} catch(...){...}" structure.
  #3  
Old 15-Sep-2005, 22:14
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 853
admin will become famous soon enough
Sorry, I couldn't resist...

We already have a working syntax highlighter just for C# on the new (test) forum. We hope to attract a bit more interest for the language once we upgrade the forums by the end of the month.

In the meantime, I will move this discussion to the .NET forum.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #4  
Old 16-Sep-2005, 10:00
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Kacyndra
Hello,
i didn't see a Visual C# thread here, but i thought maybe someone could help me anyway...
this is for an into class, and my question is how to handle expections in C#.
for example, my first program is supposed to find an average of a number of integers entered by the user.

so when the user enters an integer everything is ok, but if a letter is entered it gives me an unhaldeled expection error.
this is how i get the input from the textbox.
C-SHARP / C# Code:
int Input = int.Parse(txtInput.Text);

please hlep.

As maprich noted, you can make use of the try - catch mechanism. If you would have looked at the "Details" bit of the error message, you would have noticed that a System.FormatException was thrown. So catch this particular Exception.

C-SHARP / C# Code:
try
{
	int input = int.Parse(txtInput.Text);
}
catch(System.FormatException ex)
{
	MessageBox.Show(this, "You entered invalid data\nPlease only input numeric data", 
		"Wrong input", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


Note you always have the possibility to catch a generic Exception - named just like that. This will catch *all* exceptions, because every exception class is inherited from this generic class.

C-SHARP / C# Code:
try
{
	int input = int.Parse(txtInput.Text);
}
catch(Exception ex)
{
	MessageBox.Show(ex.ToString());
}

Catching like that is not advised, except maybe in the development phase; you should catch and treat every exception your program is likely to throw.
You can also display a much friendlier and comprehensive message, like I did in my little example.

For completeness, I will add there is the possibility of using the final keyword too. The code in the final group is guaranteed to execute "no matter what" happens after the exception is caught. You will usually put clean-up code in here - but un-like in C++, there's not much clean-up you can do

C-SHARP / C# Code:
try
{
	int input = int.Parse(txtInput.Text);
}
catch(System.FormatException ex)
{
	MessageBox.Show(this, "You entered invalid data\nPlease only input numeric data", 
		"Wrong input", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

finally
{
	MessageBox.Show("Oh, yeah");
}

Oh, and I advise against capitalization of the variable names; you will usually use a variable name of var and Var will be the accesor property for that variable (if any). But that's just the coding style I use, you are free to choose your own.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Mrs stacy12 C Programming Language 14 05-Feb-2005 18:02
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 02:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:15.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.