Conversion from string "" to type 'Integer' is not valid.

D

Dave griffiths

Hi all
Using VB2005 on Vista with a Norwegian locale setup.

The test program has 3 textboxes the sum held in txt3.

Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this

Public Class Main
Dim y As Integer
Dim x As Integer
Private Sub txt1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt1.LostFocus

If txt1.Text = "" Then
y = 0
Else
y = CInt(txt1.Text)
End If
End Sub

Private Sub txt2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.LostFocus

' This produces an error if txt2 is empty
x = CInt(txt2.Text)

txt3.Text = x + y
End Sub
End Class

Thanks in advance.
 
R

rowe_newsgroups

Hi all
Using VB2005 on Vista with a Norwegian locale setup.

The test program has 3 textboxes the sum held in txt3.

Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this

Public Class Main
Dim y As Integer
Dim x As Integer
Private Sub txt1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt1.LostFocus

If txt1.Text = "" Then
y = 0
Else
y = CInt(txt1.Text)
End If
End Sub

Private Sub txt2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.LostFocus

' This produces an error if txt2 is empty
x = CInt(txt2.Text)

txt3.Text = x + y
End Sub
End Class

Thanks in advance.

I would do this:

If IsNumeric(txt2.Text) Then
x = Integer.Parse(txt2.Text)
Else
x = 0
End If

Or Even this:

Dim x as Integer = 0
Integer.TryParse(txt2.Text, x)

Thanks,

Seth Rowe
 
R

rowe_newsgroups

I would do this:

If IsNumeric(txt2.Text) Then
x = Integer.Parse(txt2.Text)
Else
x = 0
End If

Or Even this:

Dim x as Integer = 0
Integer.TryParse(txt2.Text, x)

Thanks,

Seth Rowe

I forgot to say why :)

Using IsNumeric or TryCast will not only prevent a null string from
popping an exception, but will also stop non-integer values from
causing problems. For example, if the user enters text into the box
(you should probably disable this) or if they enter a value that is
out of range of an integer variable (though I don't think IsNumeric
will catch this, so you might go the tryparse way)

Thanks,

Seth Rowe
 
T

Tim Patrick

In the txt2_LostFocus event handler, you can change the assignment of "x" to:

x = CInt(Val(txt2.Text))

although some programmers consider Val to be an unsafe alternative to actual
formal checking of the data. Also, there are some minor situations where
Val will generate an error instead of returning zero for unrecogized input.
 
R

Rory Becker

Hi all Using VB2005 on Vista with a Norwegian locale setup.
The test program has 3 textboxes the sum held in txt3.

Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this

Public Class Main
Dim y As Integer
Dim x As Integer
Private Sub txt1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt1.LostFocus
If txt1.Text = "" Then
y = 0
Else
y = CInt(txt1.Text)
End If
End Sub
Private Sub txt2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.LostFocus

' This produces an error if txt2 is empty
x = CInt(txt2.Text)
txt3.Text = x + y
End Sub
End Class
Thanks in advance.



Well in a pinch you can use....
 
R

Rory Becker

x = CInt(txt2.Text)
txt3.Text = x + y

My first reply seems to have mysteriously disappeared. so I will try again :)

Essentially I agree with Seth.

I would however suggest that for anything that might eventually be a less
than trivial form, that you should consider the creation of a function to
validate the form itself before proceeding to perform calculations based
on the data in that form.

for example
-------------------------------------------------------------
Public Function isFormValid(ByRef ErrorMessage as String) as Boolean
If SomeBadCondition then
ErrorMessage = "Alert User of Problem"
Exit Sub
End If
If SomeOtherBadCondition then
ErrorMessage = "Alert User of Problem2"
Exit Sub
End If

Return True
End Function
-------------------------------------------------------------

Later on you can say...
-------------------------------------------------------------
Public Sub PerformCalculation
' check form first
Dim TheErrorMessage as String
If Not isFormValid(ThePotentialErrorMessage) then
MessageBox.Show(TheErrorMessage)
Exit Sub
End If
' Now perform calculation
....
....
....
End Sub
 
D

Dave griffiths

Hi Tim

I have learned to stay away from Val as I live and run my software in a
non english society.

From MS
The Val function recognizes only the period (.) as a valid decimal
separator. When different decimal separators are used, as in
international applications, use CDbl or CInt instead to convert a
string to a number.
 
D

Dave griffiths

Hi All

Thanks for so much input so quickly.

I agree with Rory that there needs to be some kind of form validation,
I was really asking the correct or should I say tidier way of
performing the conversion.

As the form uses extensive numeric textboxes I plan to create my own
class "numtext" with the information kindly supplied here to handle the
problem.
 
R

Rory Becker

As the form uses extensive numeric textboxes I plan to create my own
class "numtext" with the information kindly supplied here to handle
the problem.

If this is a small project then go for it... there's lots of good stuff to
be leared by doing it yourself. :)

Again though if this is for a large paid project, it might be worth looking
into potentially buying in this type of functionality. especially if you
see yourself having to deal with multiple different types of formatting.
numbers dates

I use DeveloperExpress' XtraEditors but Infragistics and many other companies
have reasonably comprehensive solutions to the DataEntry problem as a whole.

There are also many good freeware options. www.CodeProject.com should have
a few examples.

Again it may be overkill. I cannot possibly tell what your situation is Re
time or money, but where possible I prefer not to reinvent the wheel and
risk making a square one :)
 
O

\(O\)enone

Dave said:
From MS
The Val function recognizes only the period (.) as a valid decimal
separator. When different decimal separators are used, as in
international applications, use CDbl or CInt instead to convert a
string to a number.

Which sounds great, except that CDbl and CInt don't do the same thing as
Val. Val will read numeric data from the start of a non-numeric string and
return just the numeric part (so given the string "123ABC", Val will return
123.0 whereas CDbl and CInt will raise a type mistmatch; given "Hello World"
or "" , Val will return zero as opposed once again to a type mismatch).
Sometimes this functionality for Val is extremely useful -- although there
are other dangers (Val("1e3") returns 1000.0 rather than 1 as the "e" is
interpreted as the exponent operator).

We worked around this by writing a replacement function, taking the input
value, replacing all "thousands" separators (as defined by the locale) with
an empty string, and all "decimal" separators (again as defined by the
locale) with a period ("."). Then you can use Val() on it safely.

I think it's bizarre that Val works this way, but it does. Humpf.
 
D

Dave griffiths

Thanks Rory

This is a small "free" project for a small community, I think I will
benefit more by "rolling my own" solution as I know I need the practice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top