By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,165 Members | 1,502 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,165 IT Pros & Developers. It's quick & easy.

need help resolving runtime error

P: n/a
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.
Feb 7 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Feb 7 '06 #2

P: n/a
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.

Feb 7 '06 #3

P: n/a
What do you mean that I have already assigned text boxes to variables? How
do I turn on the strict option?
"Andrew Morton" <ak*@in-press.co.uk.invalid> wrote in message
news:%2****************@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

Feb 7 '06 #4

P: n/a
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]" <Ke**********@discussions.microsoft.com> wrote in message
news:00**********************************@microsof t.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.

Feb 7 '06 #5

P: n/a
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?
"Charlie Brookhart" <ch****************@hotmail.com> wrote in message
news:e_******************************@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" <ak*@in-press.co.uk.invalid> wrote in message
news:%2****************@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


Feb 7 '06 #6

P: n/a
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" <ch****************@hotmail.com> wrote in message
news:rr********************@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]" <Ke**********@discussions.microsoft.com> wrote in
message
news:00**********************************@microsof t.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.
>
>
>


Feb 7 '06 #7

P: n/a
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.

"Jim Wooley" <jw******@bellsouth.net> wrote in message
news:uM**************@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" <ch****************@hotmail.com> wrote in message
news:rr********************@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]" <Ke**********@discussions.microsoft.com> wrote in
message
news:00**********************************@microsof t.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.
>
>
>



Feb 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.