TryParse menu

User Tag List

Thread: TryParse

Results 1 to 10 of 10
  1. #1
    Cush's Avatar Elite User
    Reputation
    504
    Join Date
    May 2006
    Posts
    528
    Thanks G/R
    3/19
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    TryParse

    Hey. I just started learning to use VB .Net and have been making extremely basic programs such as a small login form which requires a valid user/pass to get in, and practicing using cases.

    Today I got the task of creating a basic calculator, since I dont really think like a programmer yet I was sorta puzzled on how I should do everything at first, since it seems like a big task when you are just about to start it. After taking a look at a bunch of examples I got my head around it and created the layout, then got the number and backspace buttons working.

    When I get to the mathematical functions in the examples though, they all use Decimal.TryParse, and im kinda puzzled over what parsing actually does to the input you give it and how it gets the output. The other thing im confused about is the point in using TryParse if you arent going to use the output it gives, every function except converting between +/- had Decimal.TryParse(blahbalh.Text, Nothing)

    + for example,

    Code:
            If Decimal.TryParse(txt.Text, Nothing) Then
    
                memory = txt.Text
    
                operation = "+"
    
                txt.Text = Nothing
    
                lbl.Text = memory.ToString & "  " & operation
    Bearing in mind the whole thing has three inputs -

    Code:
        Dim memory As Decimal
    
        Dim operation As String
       
    
        'Decides whether a calculation has been made or not
        Dim calculated As Boolean = False
    I think my overall question is - What exactly is parsing and why should I use it?

    TryParse
  2. #2
    Cephalopod's Avatar Member
    Reputation
    5
    Join Date
    Sep 2008
    Posts
    85
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tryparse tests if it can be converted to a decimal, if it can't, it returns false.

    Most conversions, if not all, probably have it. I've only used boolean.tryparse when reading settings from a text file to ensure it's a true or a false.

    http://msdn.microsoft.com/en-us/library/9zbda557.aspx

  3. #3
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Erm...

    TryParse is just an easy way of wrapping Decimal.Parse in a try/catch block. Also, the second parameter is an [out] parameter. You should *never* use TryParse if you can avoid it. It can cause major errors in your code.

    In short, TryParse returns a Boolean. It also has an [out] param, that gives the parsed value of the input param.

    Code:
    Dim tmp As Decimal = 0
    Dim parsed As Boolean = Decimal.TryParse("Text", tmp)
    So now you have in effect, whether or not "Text" was parsed into a Decimal, and if it was parsed, what the value of "Text" is in the Decimal type.

  4. #4
    Cush's Avatar Elite User
    Reputation
    504
    Join Date
    May 2006
    Posts
    528
    Thanks G/R
    3/19
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, still kinda confused though :P Im not big on technical terms like try/catch block and what parsing actually is >_>

    Its kinda hard for me to explain, would it not parse successfully if I tried to convert it to decimal and there were letters in the textbox?

    Im still not clear on why it is used also, I mean when I remove tryparse from all of the buttons, I get a few problems - I get overflow errors when too many numbers are entered, and the program crashes if you press a maths function (+,/,-) when theres nothing in the text box.

    Is the output given from parsing a boolean? The only time the output is used is when converting between signed and unsigned numbers.

    Code:
        Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click
    
    
            Dim txtValue As Decimal
    
            If Decimal.TryParse(txt.Text, txtValue) Then
               
    
                If Math.Sign(txtValue) = "-1" Then
    
                    txt.Text = Math.Abs(txtValue)
    
                Else
    
                    txt.Text = -txt.Text
    
                End If
    
            End If
    
        End Sub

  5. #5
    Cephalopod's Avatar Member
    Reputation
    5
    Join Date
    Sep 2008
    Posts
    85
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cush View Post
    Thanks, still kinda confused though :P Im not big on technical terms like try/catch block and what parsing actually is >_>
    Try means exactly what it says, it tries something. Code example coming up! In C# but VB.net will be similar

    Code:
    try
    {
    //code to try, if it fails, it is "caught" below :D
    }
    catch (error)
    {
    //code when caught, if the code above failed aka produced an error, it appears here. Replace (error) with the error you are trying to catch. This is manual error handling, well, more manual :P You can catch multiple errors and treat them differently.
    }
    Just imagine Parse really said Convert. All .parse is doing is converting it to another format.

    decimal.parse converts to decimal.
    integer.parse converts to an integer.

    Its kinda hard for me to explain, would it not parse successfully if I tried to convert it to decimal and there were letters in the textbox?
    If you attempted to tryparse text -> decimal you'd get a result of "false" and out would have no value.

    Im still not clear on why it is used also, I mean when I remove tryparse from all of the buttons, I get a few problems - I get overflow errors when too many numbers are entered, and the program crashes if you press a maths function (+,/,-) when theres nothing in the text box.
    As would be expected. You've removed the inbuilt (and ineffective, unless you know it well) error handling.

    [quote]
    Is the output given from parsing a boolean? The only time the output is used is when converting between signed and unsigned numbers.

    I'm confused. Reword this :P I'll type in red whats happening in the relevant bit of code below.

    Code:
        Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click
    
    
            Dim txtValue As Decimal
    
            If Decimal.TryParse(txt.Text, txtValue) Then
    Ok, this line here is using tryparse, if the value in txt.text can't be converted to a decimal, then it returns false. You hjave no code to execute for "false" so it ends the if and exits the sub.
    
    If the result is true, as in the conversion was successful, then it moves on, the condition was satisfied as tryparse returned true.
               
    
                If Math.Sign(txtValue) = "-1" Then
    
                    txt.Text = Math.Abs(txtValue)
    
                Else
    
                    txt.Text = -txt.Text
    
                End If
    
            End If
    
        End Sub

  6. #6
    Cush's Avatar Elite User
    Reputation
    504
    Join Date
    May 2006
    Posts
    528
    Thanks G/R
    3/19
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I think I get it more :P

    For the last bit, I mean why would you use tryparse when you arent using the output? Like all of the other buttons have Decmial.Tryparse(txt.Text, Nothing), or does it covert txt.Text and keep it in there?

    Also, is this an 'effective' way to use tryparse, or is there a better/easier way to do the same thing?

  7. #7
    Cephalopod's Avatar Member
    Reputation
    5
    Join Date
    Sep 2008
    Posts
    85
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code is using the output provided by decimal.trypase, the output of decimal.tryparse is placed into the variable "txtValue"

    If you wrote that, I'd get rid of the txt infront of the variable name. txt is used to tell the reader the item is a textbox. I'd use name the variable "value" or "Value" or "_value" depending on which naming convention you want to use.

    I agree with Apoc, I wouldn't use tryparse, rather I'd setup my own try/catch and catch the specific error myself, or specific errors.

    Code:
    Dim value As Decimal = 0
    
    Try
        value = Decimal.parse(txt.text)
        'math.sign code here
    Catch error As System.InvalidCastException
        'error here
    End Try
    I'd place your math.sign code where it says to place it, and I assume the exception which is displayed is the invalidcastexception. If not here is a site which shows you how to tell which exception was thrown:
    Visual Basic .NET programming for Beginners - Try … Catch

    Shorter code (tryparse) doesn't always equal better code.

  8. #8
    Cush's Avatar Elite User
    Reputation
    504
    Join Date
    May 2006
    Posts
    528
    Thanks G/R
    3/19
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its txtValue because the value is taken from the text box :P

    What I meant was with the end of my last post, the output is 'Nothing', which of course means its going no-where, so why use tryparse in the first plac in that situation?

  9. #9
    Cephalopod's Avatar Member
    Reputation
    5
    Join Date
    Sep 2008
    Posts
    85
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean it outputs no value?

    If so, the conversion to a decimal is failing.

  10. #10
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just read the following: C# Tutorials - Basic Calculator In C# | DreamInCode.net

    It explains just about everything you'll need.

All times are GMT -5. The time now is 02:37 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search