Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, December 11, 2014 3:50 AM
My English level is really...bad...If there's something make you confused, plz tell me and I will give much more details.
First of all, this is my code(s)
protected void btnPeopleCount_Click(object sender, EventArgs e)
{
SetBookingInfoTable(GetPeopleCount());
}
private int GetPeopleCount()
{
int intPeopleCount = 0;
try
{
intPeopleCount = Int32.Parse(txtPeopleCount.Text);
}
catch (Exception)
{
lblPeopleCountTip.BackColor = System.Drawing.Color.Red;
lblPeopleCountTip.ForeColor = System.Drawing.Color.White;
txtPeopleCount.Text = "";
return 0;
}
return intPeopleCount;
}
I need that when GetPeopleCount() catches any error, the program would finish the catch (exception) code(s)
and then
stop running any code(stop from running SetBookingInfoTable()).
like the below image, when i input an integer number in that box and press the button near to it, the GetPeopleCount start to execute. If the input value is not an integer, GetPeopleCount stops running and SetBookingInfoTable won't run, neither.
For now, I use return 0 and then use IF to make a choice whether intPeopleCount is bigger than 0 or not.
It did could reach my goal, but I just want to learn
HOW TO STOP EXECUTING THE CODE......
Thanks for help.
All replies (3)
Thursday, December 11, 2014 12:32 PM âś…Answered
It depends on the pattern you want to use.. But the easiest approach is TryParse():
protected void btnPeopleCount_Click(object sender, EventArgs e)
{
int peopleCount;
if (Int32.TryParse(txtPeopleCount.Text, out peopleCount))
{
SetBookingInfoTable(peopleCount);
}
}
Thursday, December 11, 2014 11:34 AM
When you RETURN, that's the end of the function, so the method stopped.
What else would you like to achieve?
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
Thursday, December 11, 2014 12:02 PM
hello
coding with error code result it's a relevant pattern for me.
Return 0 in GetPeopleCount... and check if it's 0 before launch SetBooking...
But, if for some raison, you don't want to use this way, you could remove your try/catch in getPeopleCount... and surround your call SetBooking.. by a try/catch.
In this case, GetPeopleCount... will raise the exception and SetBooking... won't be execute.
But, for me, programming by exception is not a right way for performance raisons. Using return code from method is better.
Regards
Cedric