|
C# 2005 For discussion of Visual C# 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
January 7th, 2008, 07:35 AM
|
Registered User
|
|
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
which control is firing the exception
hi....
i am using a code like this :
double total = double.Parse(txtPF.Text) + double.Parse(txtIT.text);
where txtPF and txtIT are two text boxes on a win forrm.
if txt box doesn't contain any value it is firing exception.
i want :
try
{
double total = double.Parse(txtPF.Text) + double.Parse(txtIT.text);
}
catch(Exception exc)
{
//some code through which i can find out exactly which text box fired the exception.so tht i can use :
MessageBox.Show("please enter a value in "+ <name of tht txt box > +"field.");
//....
}
i've tried much bt nothing seems to work.
please help.
|
January 7th, 2008, 08:02 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Neither textbox is 'firing the exception'. Each textbox is quite happily returning its Text value as a string (an empty string in one instance). Its only after that that the exception is firing.
I would use validators if I were you, one, or even two for each box (on to check if the box is empty, the other to check it is a number).
/- Sam Judson : Wrox Technical Editor -/
|
January 7th, 2008, 08:07 AM
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try this code
double dblPF;
try
{
dblPF = double.Parse(txtPF.Text);
}
catch(Exception ex)
{
MessageBox.Show("please enter a value in PF field.");
}
try
{
//Now we are sure that there is no problem in txtPF sso try to add, if it throws exception then the problem must be in txtIT.
total = double.Parse(txtPF.Text) + double.Parse(txtIT.Text);
}
catch(Exception exc)
{
MessageBox.Show("please enter a value in IT field.");
}
Mujahid Khaleel
Web designing, development, E-commerce applications.
|
January 7th, 2008, 09:29 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Take a look at the Double.TryParse function.
--
Joe ( Microsoft MVP - XML)
|
January 8th, 2008, 05:47 AM
|
Registered User
|
|
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by samjudson
Neither textbox is 'firing the exception'. Each textbox is quite happily returning its Text value as a string (an empty string in one instance). Its only after that that the exception is firing.
I would use validators if I were you, one, or even two for each box (on to check if the box is empty, the other to check it is a number).
/- Sam Judson : Wrox Technical Editor -/
|
thanks for a quick reply...
i've used validator for a number...but not for an empty string...
your idea seems to be a good one....
thanks again...
now i think i shud've ask the question like this..:
if a statement contains more than one control's participation, and then fires an exception...then is there any way to know exactly which control's action was responsible for the exception....
thanks in advance for this one....
|
January 8th, 2008, 05:55 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
And the answer is, no. As I said, the exception is not firing from inside the control, therefore the stack trace would not contain the control anywhere in its history. Even then, trying to analyse the stack trace would be the wrong way to do this.
On an aside, if you expecting an action to fail then using exceptions to trap the error is not the most performant way to perform this - far better to do two TryParse operations, check if either fails, then add the two resulting numbers.
/- Sam Judson : Wrox Technical Editor -/
|
January 8th, 2008, 07:24 AM
|
Registered User
|
|
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by samjudson
And the answer is, no. As I said, the exception is not firing from inside the control, therefore the stack trace would not contain the control anywhere in its history. Even then, trying to analyse the stack trace would be the wrong way to do this.
On an aside, if you expecting an action to fail then using exceptions to trap the error is not the most performant way to perform this - far better to do two TryParse operations, check if either fails, then add the two resulting numbers.
/- Sam Judson : Wrox Technical Editor -/
|
aha....
tht seems to be exactly the defination i was looking for....
thanks a lot....
|
January 8th, 2008, 07:27 AM
|
Registered User
|
|
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by joefawcett
Take a look at the Double.TryParse function.
--
Joe (Microsoft MVP - XML)
|
hi....
tryParse is working perfectly....
thanks for the support....
|
January 8th, 2008, 07:30 AM
|
Registered User
|
|
Join Date: Jan 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by mujahidkhaleel
Try this code
double dblPF;
try
{
dblPF = double.Parse(txtPF.Text);
}
catch(Exception ex)
{
MessageBox.Show("please enter a value in PF field.");
}
try
{
//Now we are sure that there is no problem in txtPF sso try to add, if it throws exception then the problem must be in txtIT.
total = double.Parse(txtPF.Text) + double.Parse(txtIT.Text);
}
catch(Exception exc)
{
MessageBox.Show("please enter a value in IT field.");
}
Mujahid Khaleel
Web designing, development, E-commerce applications.
|
hi....
interesting solution...and working perfectly also...
thanks for ur support....
|
January 8th, 2008, 02:17 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
raykaash - A forum tip: It's not necessary to quote every response when replying. We can follow the thread on the forum itself. If you want to address a specific statement in a previous post then by all means quote just that statement to provide response context.
-Peter
|
|
|