|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
need help resolving runtime errorthe user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't understand how to fix the problem. 'declaring variables Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) Dim totalPints As Double = Convert.ToDouble(txtPints.Text) Dim totalGallons As Double = Convert.ToDouble(txtGallons.Text) 'assigning text box values to variables totalLiters = txtLiters.Text totalPints = txtPints.Text totalGallons = txtGallons.Text 'performing calculations txtPints.Text = totalLiters * 2.1133 txtGallons.Text = totalLiters * 0.26 Runtime error message: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. Charlie Brookhart wrote:
> I have a program (posted below) that is supposed to take liters, <snip>> which is the user input, and convert it to pints and gallons. The > pints and gallons are displayed in a read only textbox. I don't > understand how to fix the problem. > > 'declaring variables > > Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) > > 'assigning text box values to variables Um, you've already done that...If you use "option strict on", it will show you what the problem is. Andrew What do you mean that I have already assigned text boxes to variables? How
do I turn on the strict option? Show quoteHide quote "Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message news:%23dnGDcALGHA.1124@TK2MSFTNGP10.phx.gbl... > Charlie Brookhart wrote: > > I have a program (posted below) that is supposed to take liters, > > which is the user input, and convert it to pints and gallons. The > > pints and gallons are displayed in a read only textbox. I don't > > understand how to fix the problem. > > > > 'declaring variables > > > > Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) > > > <snip> > > 'assigning text box values to variables > > Um, you've already done that... > > If you use "option strict on", it will show you what the problem is. > > Andrew > > I found the option strict statement on the MSDN library CD. That creates an
error message as well and actually prevents the building of the program, "statement is not inside a valid method. So, I either get runtime errors or build errors. I still do not have a working program at this point. I'm entering a number in a textbox. Why doesn't VB recognize that a number has been entered as opposed to something like abcd? Show quoteHide quote "Charlie Brookhart" <charliebrookhar***@hotmail.com> wrote in message news:e_WdnUVzdupuQXXenZ2dnUVZ_t-dnZ2d@adelphia.com... > What do you mean that I have already assigned text boxes to variables? How > do I turn on the strict option? > > > "Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message > news:%23dnGDcALGHA.1124@TK2MSFTNGP10.phx.gbl... > > Charlie Brookhart wrote: > > > I have a program (posted below) that is supposed to take liters, > > > which is the user input, and convert it to pints and gallons. The > > > pints and gallons are displayed in a read only textbox. I don't > > > understand how to fix the problem. > > > > > > 'declaring variables > > > > > > Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) > > > > > <snip> > > > 'assigning text box values to variables > > > > Um, you've already done that... > > > > If you use "option strict on", it will show you what the problem is. > > > > Andrew > > > > > > Charlie,
You are not checking to see if the text in the textbox is actually a number. You should enclose where you convert the text in the textboxes into a number in a try catch block. If you have vb 2005 use double.tryparse instead. Ken ------------------------ Show quoteHide quote "Charlie Brookhart" wrote: > I have a program (posted below) that is supposed to take liters, which is > the user input, and convert it to pints and gallons. The pints and gallons > are displayed in a read only textbox. I don't understand how to fix the > problem. > > 'declaring variables > > Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) > > Dim totalPints As Double = Convert.ToDouble(txtPints.Text) > > Dim totalGallons As Double = Convert.ToDouble(txtGallons.Text) > > > > 'assigning text box values to variables > > totalLiters = txtLiters.Text > > totalPints = txtPints.Text > > totalGallons = txtGallons.Text > > 'performing calculations > > txtPints.Text = totalLiters * 2.1133 > > txtGallons.Text = totalLiters * 0.26 > > Runtime error message: > > An unhandled exception of type 'System.FormatException' occurred in > mscorlib.dll > > Additional information: Input string was not in a correct format. > > > I am working with VB 2003. I simply don't understand why I am getting a
runtime error and how to fix it. Show quoteHide quote "Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in message news:0076350D-C920-4E9F-ACEB-8E5FBB6BE1ED@microsoft.com... > Charlie, > > You are not checking to see if the text in the textbox is > actually a number. You should enclose where you convert the text in the > textboxes into a number in a try catch block. If you have vb 2005 use > double.tryparse instead. > > Ken > ------------------------ > > "Charlie Brookhart" wrote: > > > I have a program (posted below) that is supposed to take liters, which is > > the user input, and convert it to pints and gallons. The pints and gallons > > are displayed in a read only textbox. I don't understand how to fix the > > problem. > > > > 'declaring variables > > > > Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) > > > > Dim totalPints As Double = Convert.ToDouble(txtPints.Text) > > > > Dim totalGallons As Double = Convert.ToDouble(txtGallons.Text) > > > > > > > > 'assigning text box values to variables > > > > totalLiters = txtLiters.Text > > > > totalPints = txtPints.Text > > > > totalGallons = txtGallons.Text > > > > 'performing calculations > > > > txtPints.Text = totalLiters * 2.1133 > > > > txtGallons.Text = totalLiters * 0.26 > > > > Runtime error message: > > > > An unhandled exception of type 'System.FormatException' occurred in > > mscorlib.dll > > > > Additional information: Input string was not in a correct format. > > > > > > First rule: Always check the validity of your user's input. In this case,
you are not checking to see if the user entered a number or not. By using Convert.ToDouble, you will get a run-time exception if you pass in something ..Net doesn't recognize as a number. You need to check to see if it is a number before trying to force a conversion on it. Failing that, wrap each Convert.ToDouble (or Double.Parse) statement in a Try..Catch..End Try block. (This is actually with .Net 1.x does under the covers with the VB library's IsNumeric function, it just throws away the result and passes back whether it was able to convert it or not.) Second rule: Everything entered in a textbox on the UI is a string. TextBox.Text does not accept a number. You must tell the textbox how you want the number to appear as a string. For example, Textbox1.text=MyNumber.ToString("c2") will present the number formatted with the current regional setting's currency format and 2 decimal places. Textbox1.text=MyNumber throws an exception. Also Textbox1=MyNumber will throw a number as you can not assign a number to a textbox as they are not the same type of object. I recommend stepping back from the liters to pints issue and looking at the basic data type issues. The reason for the Option Strict recommendation was to force you to identify that you can't assign a number to a textbox.text property. You are hitting the same error. By turning Option Strict On, you catch the exception at compile time rather than waiting for it to crash your application when an end user is consuming the application. Option Strict is a good thing. Jim Wooley Show quoteHide quote "Charlie Brookhart" <charliebrookhar***@hotmail.com> wrote in message news:rrudndsL1dsfQHXeRVn-og@adelphia.com... >I am working with VB 2003. I simply don't understand why I am getting a > runtime error and how to fix it. > > "Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in > message > news:0076350D-C920-4E9F-ACEB-8E5FBB6BE1ED@microsoft.com... >> Charlie, >> >> You are not checking to see if the text in the textbox is >> actually a number. You should enclose where you convert the text in the >> textboxes into a number in a try catch block. If you have vb 2005 use >> double.tryparse instead. >> >> Ken >> ------------------------ >> >> "Charlie Brookhart" wrote: >> >> > I have a program (posted below) that is supposed to take liters, which > is >> > the user input, and convert it to pints and gallons. The pints and > gallons >> > are displayed in a read only textbox. I don't understand how to fix the >> > problem. >> > >> > 'declaring variables >> > >> > Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) >> > >> > Dim totalPints As Double = Convert.ToDouble(txtPints.Text) >> > >> > Dim totalGallons As Double = Convert.ToDouble(txtGallons.Text) >> > >> > >> > >> > 'assigning text box values to variables >> > >> > totalLiters = txtLiters.Text >> > >> > totalPints = txtPints.Text >> > >> > totalGallons = txtGallons.Text >> > >> > 'performing calculations >> > >> > txtPints.Text = totalLiters * 2.1133 >> > >> > txtGallons.Text = totalLiters * 0.26 >> > >> > Runtime error message: >> > >> > An unhandled exception of type 'System.FormatException' occurred in >> > mscorlib.dll >> > >> > Additional information: Input string was not in a correct format. >> > >> > >> > > > The IsNumeric function would work in combination with an if then else
statement, would it not? If the user enters something other than a number, an message box would display. Being that I don't have any programming experience at all, and that I am having to learn VB from a book, it looks like I am going to have a lot of reading to in order to understand what goes into verrifying user input and converting strings into something other than text. Thank you to all who have responded to my post, and thanks for your help. Show quoteHide quote "Jim Wooley" <jwool***@bellsouth.net> wrote in message news:uMQUHEDLGHA.2492@TK2MSFTNGP10.phx.gbl... > First rule: Always check the validity of your user's input. In this case, > you are not checking to see if the user entered a number or not. By using > Convert.ToDouble, you will get a run-time exception if you pass in something > .Net doesn't recognize as a number. You need to check to see if it is a > number before trying to force a conversion on it. Failing that, wrap each > Convert.ToDouble (or Double.Parse) statement in a Try..Catch..End Try block. > (This is actually with .Net 1.x does under the covers with the VB library's > IsNumeric function, it just throws away the result and passes back whether > it was able to convert it or not.) > > Second rule: Everything entered in a textbox on the UI is a string. > TextBox.Text does not accept a number. You must tell the textbox how you > want the number to appear as a string. For example, > Textbox1.text=MyNumber.ToString("c2") will present the number formatted with > the current regional setting's currency format and 2 decimal places. > Textbox1.text=MyNumber throws an exception. Also Textbox1=MyNumber will > throw a number as you can not assign a number to a textbox as they are not > the same type of object. > > I recommend stepping back from the liters to pints issue and looking at the > basic data type issues. The reason for the Option Strict recommendation was > to force you to identify that you can't assign a number to a textbox.text > property. You are hitting the same error. By turning Option Strict On, you > catch the exception at compile time rather than waiting for it to crash your > application when an end user is consuming the application. Option Strict is > a good thing. > > Jim Wooley > > > "Charlie Brookhart" <charliebrookhar***@hotmail.com> wrote in message > news:rrudndsL1dsfQHXeRVn-og@adelphia.com... > >I am working with VB 2003. I simply don't understand why I am getting a > > runtime error and how to fix it. > > > > "Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in > > message > > news:0076350D-C920-4E9F-ACEB-8E5FBB6BE1ED@microsoft.com... > >> Charlie, > >> > >> You are not checking to see if the text in the textbox is > >> actually a number. You should enclose where you convert the text in the > >> textboxes into a number in a try catch block. If you have vb 2005 use > >> double.tryparse instead. > >> > >> Ken > >> ------------------------ > >> > >> "Charlie Brookhart" wrote: > >> > >> > I have a program (posted below) that is supposed to take liters, which > > is > >> > the user input, and convert it to pints and gallons. The pints and > > gallons > >> > are displayed in a read only textbox. I don't understand how to fix the > >> > problem. > >> > > >> > 'declaring variables > >> > > >> > Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) > >> > > >> > Dim totalPints As Double = Convert.ToDouble(txtPints.Text) > >> > > >> > Dim totalGallons As Double = Convert.ToDouble(txtGallons.Text) > >> > > >> > > >> > > >> > 'assigning text box values to variables > >> > > >> > totalLiters = txtLiters.Text > >> > > >> > totalPints = txtPints.Text > >> > > >> > totalGallons = txtGallons.Text > >> > > >> > 'performing calculations > >> > > >> > txtPints.Text = totalLiters * 2.1133 > >> > > >> > txtGallons.Text = totalLiters * 0.26 > >> > > >> > Runtime error message: > >> > > >> > An unhandled exception of type 'System.FormatException' occurred in > >> > mscorlib.dll > >> > > >> > Additional information: Input string was not in a correct format. > >> > > >> > > >> > > > > > > >
EXE vs DLL Creation
Draw backs of Serialized Objects Best Practices with In Memory Data Method description thingy... Device Driver in VB 2005 Copying files across network Converting a project from 2003 to 2005 differences Books On VB.NET 2005 Peculiar inconsistency in autogenerated event handler code String vs. Stringbuilder speed parsing questio |
|||||||||||||||||||||||