|
-
November 30th, 2011, 08:10 AM
#1
C# Error
Hey, I'm using visual studio 2010 to code a C# program and it's thrown up this error
String was not recognized as a valid Boolean.
in relation to this code...
I can't figure out why, I've tried to error help but I can't make sense of it because I have two versions of the code and one words and the other doesn't.
The code is in relation to this code
Code:
if (theActivityBooking == null) //If new bookings are to be changed then display the below and change the below
theActivityBooking = new ActivityHoliday(cmbWkOfHol.Text, txtName.Text, txtCusAddress.Text, [B] Convert.ToString(txtChildren.Text)[B], cmbActivityType.Text, Convert.ToBoolean(comboUnder16.Text));
else
{ //if editing an existing booking
theActivityBooking.name = txtName.Text;
theActivityBooking.week = cmbWkOfHol.Text;
theActivityBooking.address = txtCusAddress.Text;
theActivityBooking.children = Convert.ToString(txtChildren.Text);
theActivityBooking.activitytype = cmbActivityType.Text;
theActivityBooking.CheckAge = ageconvert(comboUnder16.Text);
}
It's throwing up the error for this line in particular Convert.ToString(txtChildren.Text)
Thanks for any help you can provide
-
November 30th, 2011, 09:07 AM
#2
Re: C# Error
My main question is why you're converting the Text of a textbox to a string. It's already a string when you pull the Text. Can't really suggest much more than that as I don't know the expected data types of the parameters for the ActivityHoliday class.
Code:
if (Issue.Resolved)
{
ThreadTools.Click();
MarkThreadResolved();
}
-
November 30th, 2011, 09:31 AM
#3
Re: C# Error
The text going into the txtChildren text box is a number and when I enter it, it takes me straight to that line of code and throws up the error
-
November 30th, 2011, 12:38 PM
#4
Re: C# Error
Code:
Convert.ToBoolean(comboUnder16.Text)
Most likely.
If comboUnder16 is checkbox use comboUnder16.checked
-
November 30th, 2011, 03:30 PM
#5
Re: C# Error
Hi sorry for the confusion, I've just been looking at the code for hours and can't figure it out.
Combounder16 is a combo box, and txtChildren is a text box, the txtChildren inputs numbers and when I try to add the children to a list via the text box the error
the number of chidren being entered is declared as a string and the under16 is declared as a boolean, the error I now get is
: String was not recognized as a valid Boolean. Relating to that same line of code.
-
November 30th, 2011, 04:16 PM
#6
Re: C# Error
SOLVED IT! Thanks anyway.
Sometimes it's good to take a break and then come back to it without stressing!
-
November 30th, 2011, 05:24 PM
#7
Re: C# Error
It's always a good idea to share the solution, so that this thread can be helpful to other people having the same problem.
Anyway, the reason is that C# allows only certain strings to be converted to bool.
See the docs for the Boolean.Parse() method at MSDN.
Here's a quote of the example code output:
// 'True' --> True
// 'False' --> False
// 'true' --> True
// 'false' --> False
// ' true ' --> True
// Cannot parse '0'.
// Cannot parse '1'.
// Cannot parse '-1'.
Basically, it works only if it says "true" or "false" in some form (caps and white space are ignored).
Now, if you need to convert numbers to bool, you can either write a simple if-else routine yourself, or use the Convert.ToBoolean() static method (after parsing the string to a number, of course).
Return Value
Type: System.Boolean
true if value is not zero; otherwise, false.
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
|