-
April 28th, 2009, 04:09 PM
#1
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?
-
April 28th, 2009, 04:22 PM
#2
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".
-
April 28th, 2009, 05:28 PM
#3
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.
-
April 28th, 2009, 05:52 PM
#4
Re: Bug in .Net?
Originally Posted by Tealc
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.
-
April 28th, 2009, 06:24 PM
#5
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.
-
April 29th, 2009, 01:25 AM
#6
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.
-
April 29th, 2009, 02:41 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|