 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
 | This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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
|
|
|
|
|

March 10th, 2009, 07:36 PM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try, Catch
Hi Imar, I'm working on page 169 and would like to add a Try / Catch if the user enters a letter instead of a number, this is for my own knowledge since it's been quite a while since I programmed.
I came up with this, but it doesn't work:
Code:
If txtValue1.Text.Length > 0 AndAlso txtValue2.Text.Length > 0 Then
Dim result As Double = 0
Try
Dim value1 As Double = Convert.ToDouble(txtValue1.Text)
Dim value2 As Double = Convert.ToDouble(txtValue2.Text)
Dim myCalculator As New Calculator()
Select Case lstOperator.SelectedValue
Case "+"
result = myCalculator.Add(value1, value2)
Case "-"
result = myCalculator.Subtract(value1, value2)
Case "*"
result = myCalculator.Multiply(value1, value2)
Case "/"
result = myCalculator.Divide(value1, value2)
End Select
Catch ex As Exception
lblResult.Text = "Please enter a number"
End Try
lblResult.Text = result.ToString()
Else
lblResult.Text = String.Empty
End If
Thanks if you can steer me in the right direction...
Kevin
|
|

March 10th, 2009, 09:50 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
I think a more elegant solution might be something like:
Dim value1, value2 as Double If (Double.TryParse(txtValue1.Text, value1)) AndAlso (Double.TryParse(txtValue2.Text, value2)) Then 'Being Case statement Else lblResult.Text = String.Empty End If
TryParse does two things:
-It returns a bool value if the first parameter (in your case txtValue1 or 2) can be converted to a double
-It returns a double via an output parameter (the second parameter) if the Parse completes successfully.
Obviously, the TryParse will return false if a string is passed in.
Imar might have other/additional suggestions.
hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|
|

March 10th, 2009, 10:24 PM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Doug.
Well as you can see I'm still learning. Still trying to grasp vb, thank you, i need all the help i can get.
Better to use TryParse?
Quote:
|
It returns a double via an output parameter (the second parameter) if the Parse completes successfully.
|
Not quite sure what you meen by (the second parameter)
Kevin
|
|

March 10th, 2009, 10:43 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
No problem Kevin.
TryParse, in this case, takes two parameters:
TryParse(valueToParse, returnValue)
In this case you would pass either the value of either txtValue1 or txtValue2 in as the first parameter and then you would supply either value1 or value2 (the variables that have been declared as doubles) for the second parameter. What this does is that if TryParse returns True the variable you passed in as the second parameter will contain the double value.
To help explain think of this example:
Suppose that a user entered the value 3.14 into txtValue 1 and 0 into txtValue2. Then suppose you have the following code in the click event handler of a button:
Dim value1, value2 as Double If (Double.TryParse(txtValue1.Text, value1)) AndAlso (Double.TryParse(txtValue2.Text, value2)) Then Response.Write(value1.ToString()) Response.Write("<br />") Response.Write(value2.ToString()) Else lblResult.Text = "Both values must be numeric!" End If
The output to the browser window would be:
3.14
0
So all an output parameter is is a variable you provide as one of the parameters in the method signature (in your case either value1 or value2) which will then contain a value after the function or method has finished executing. In this case, provided TryParse returns true, the output parameter assumes the value of whatever was passed in as the first parameter; if TryParse were to fail in this case value1 or value2 would contain the value 0 (the default value for non-nullable doubles).
The only rules for output parameters, off the top of my head, is that they must be the last parameter of the signature and you can only have 1 output parameter pre method/function.
Does this make more sense?
hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
Last edited by dparsons; March 10th, 2009 at 10:47 PM..
|
|

March 11th, 2009, 09:42 AM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Doug.
Well, it's starting to sink in. I get confused with parameters flying everywhere(newbie syndrone).
I just wanted to catch the error if someone entered a letter in context to the example on page 169, to see the code in action would probably clear things up for me.
Kevin
|
|

March 11th, 2009, 09:57 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Don't misunderstand me, what you have will work just fine with one minor modification. Move this line:
lblResult.Text = result.ToString()
inside of your Try. Otherwise, regardless of what happens in your Try Catch, lblResult will always be set to the value of result.
Now here is the obvious problem with the code you have provided (don't take this as nitpicking just trying to help you out). If I were to enter the value 3.14 for value1 and the value 0 for value2 and then chose / as my operator the exception that is going to be thrown is a System.DivideByZeroException because, obviously, you can't divide by zero. So your Catch statement is going to catch this exception and then populate the label and tell me to provide a number. Well I provided two valid numbers so the message you display is a bit misleading to a user in that case.
IMHO you should try and code in a proactive way (e.g. validating the data you have coming in and make sure that it is what you expect otherwise kick it back out to the user and inform them as such) rather than reactive where you assume that the data coming in is good and have a myriad of Try Catches in place to handle user error. Again, don't misunderstand me, Try Catches are an essential part of programming and you should continue to use them but they should be there in a situation where the data you have coming in is valid but something happened during the execution that caused an exception to be thrown.
hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|
|

March 12th, 2009, 07:51 PM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Doug for the insight, like I said, I need all the help I can get.
I did try to move:
lblResult.Text = result.ToString()
into my Try Catch, but it didn't work.
I have moved on in the book, but would like to see how to fix my Try Catch, can you help me?
Kevin
|
|

March 13th, 2009, 04:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Can you show your final code?
Imar
|
|

March 14th, 2009, 09:15 AM
|
|
Authorized User
|
|
Join Date: Feb 2009
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Imar.
Code:
ProtectedSub btnCalculate_Click(ByVal sender AsObject, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
If txtValue1.Text.Length > 0 AndAlso txtValue2.Text.Length > 0 Then Dim result AsDouble = 0 Try Dim value1 AsDouble = Convert.ToDouble(txtValue1.Text) Dim value2 AsDouble = Convert.ToDouble(txtValue2.Text) Dim myCalculator AsNew Calculator() SelectCase lstOperator.SelectedValue Case"+" result = myCalculator.Add(value1, value2) Case"-" result = myCalculator.Subtract(value1, value2) Case"*" result = myCalculator.Multiply(value1, value2) Case"/" result = myCalculator.Divide(value1, value2) EndSelect Catch ex As Exception lblResult.Text = "Please enter a number" lblResult.Text = result.ToString() EndTry Else lblResult.Text = String.Empty EndIf EndSub
This is for my own learning, just curious on how to use Try Catch for this example, in beginner terms
Kevin
|
|

March 14th, 2009, 09:23 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
As doug suggested, move the line that assigns the result to the Label to inside the Try block. Currently you have it in the Catch block:
Code:
Try
...
result = myCalculator.Divide(value1, value2)
...
lblResult.Text = result.ToString()
Catch ex As Exception
lblResult.Text = "Please enter a number"
lblResult.Text = result.ToString()
End Try
Else
lblResult.Text = String.Empty
End If
Hope this helps,
Imar
---------------------------------------------------------------------------------------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
While typing this post, I was listening to: Future Proof by Massive Attack (Track 1 from the album: 100th Window) What's This?
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Try...Catch |
lowell |
VB.NET |
3 |
July 23rd, 2007 06:35 AM |
| catch the alerts |
catchrohith |
Classic ASP Basics |
1 |
October 26th, 2006 06:39 AM |
| Try Catch not working?? |
dparsons |
ASP.NET 1.0 and 1.1 Professional |
3 |
September 18th, 2006 06:54 PM |
| Try and Catch? |
mujju |
PHP How-To |
2 |
January 20th, 2005 12:27 PM |
| How to catch error. |
lancet2003 |
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 |
1 |
December 3rd, 2003 02:16 PM |
|
 |