CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Bug in .Net?

  1. #1
    Join Date
    Apr 2009
    Posts
    19

    Bug in .Net?

    For example the following statements will never return true, even when they should.

    Code:
    bool n;
    label1.Text = bool.TryParse(textBox1.Text, out n).ToString();
    I need to convert a SQL result to a bool I guess I can just go for it but if it fails there is no way to protect myself.

    Am I doing something wrong or is this a bug?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Bug in .Net?

    It wouldn't return true as it is being converted to a string. TryParse will return true on success and false on failure. What you want is this:

    Code:
    bool n;
    label1.Text = bool.Parse(textBox1.Text).ToString();
    At least, I think it is, kind of an odd example. If "Text" is any variant of "true" or "false", TryParse will return "true".

  3. #3
    Join Date
    Apr 2009
    Posts
    19

    Re: Bug in .Net?

    Instead of using a bool I decided to just return the guid becuase I will need it anyway. I am having a hard time figuring out how to test for no returned reaults though. So I changed to the following. Is this solution ok? Or is there a better solution? From what I've read on msdn I can't find a better solution.


    Before I insert an associate record I want to make sure the social security number isn't already in use.
    Code:
     
    public Guid Asociates_CheckForSSN(string p_ssn)
    {
    Dbcontext dbc = new Dbcontext();
    try
    {
    var assoc = dbc.Associates_CheckForSSN(p_ssn).Single();
    return assoc.AssociateID;
    }
    
    catch(InvalidOperationException)
    {
    return Guid.Empty;
    }
    }
    According to msdn that exception will be throw if no records are found.

    I will return the Guid of the Associate instead of returning bool.

    But is traping this error a good solution or is there some better way to test for a null result?

    Thanks.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Bug in .Net?

    Quote Originally Posted by Tealc View Post
    For example the following statements will never return true, even when they should.

    Code:
    bool n;
    label1.Text = bool.TryParse(textBox1.Text, out n).ToString();
    I need to convert a SQL result to a bool I guess I can just go for it but if it fails there is no way to protect myself.

    Am I doing something wrong or is this a bug?
    You want:


    Code:
    bool n;
    if (bool.TryParse(textBox1.Text, out n))
        label1.Text = n.ToString (); // Successfully parsed a bool from the text and stored it in 'n'
    else
        label1.Text = "Invalid"; // Failed to parse a bool. 'n' doesn't contain a useful value.
    Put whatever you want in the 'else'. This means that a valid bool could *not* be parsed from the string and stored in the variable 'n'.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Apr 2009
    Posts
    19

    Re: Bug in .Net?

    Well I tried returning a bool but it looks like I need more information then a simple yes or no so now I am using a Guid.

    I can still use the example though, but I don't see what the difference is between mine and yours.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Bug in .Net?

    What do you mean you don't see the difference? In your code, you take the value returned by TryParse, which indicates whether the parsing was successful or not. You're not using the parsed value. Read Mutant Fruit's example again.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Bug in .Net?

    The way you would use TryParse is something like this:

    Code:
    bool b;
    if ( bool.TryParse( someString, out b ) )
    {
        // the parse succeeded, now that parsed value is stored in 'b'.
    }
    TryParse returns true if the parse method succeeded. That does not mean that 'someString' parsed to 'true', it may very well parse to 'false', but it was parsed without error, so the TryParse method returns true and initializes the passed in value for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured