473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.
--
Dave Griffiths
Jul 2 '07 #1
10 26868
On Jul 2, 10:30 am, "Dave griffiths" <davegino...@nospam.hotmail.com>
wrote:
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.

--
Dave Griffiths
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

Jul 2 '07 #2
On Jul 2, 10:42 am, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On Jul 2, 10:30 am, "Dave griffiths" <davegino...@nospam.hotmail.com>
wrote:
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.
--
Dave Griffiths

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

Jul 2 '07 #3
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.

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005
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.

Jul 2 '07 #4
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....
-------------------------------------------------------------
txt3.text = cint(iif(txt2.text = "", 0, txt2.text)) + y
-------------------------------------------------------------
.... but at this point I would suggest that the proper way to do this would
be to create som sort of validation routine...

-------------------------------------------------------------
Public function ValidateForm(ErrorMessage as String) as Boolean
-------------------------------------------------------------

Jul 2 '07 #5
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
-------------------------------------------------------------

This is probably overkill for any small calculation but on larger dataentry
forms, something like this can save you a lot of trouble.

--
Rory

Jul 2 '07 #6
>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.
--
Dave Griffiths
Tim Patrick wrote:
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.

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005
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.
Jul 2 '07 #7
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.
--
Dave Griffiths
Dave griffiths wrote:
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.
Jul 2 '07 #8
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 :)

--
Rory
Jul 2 '07 #9
Dave griffiths wrote:
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.

--

(O)enone
Jul 2 '07 #10
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.

--
Dave Griffiths
Rory Becker wrote:
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 :)
Jul 2 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David P. Donahue | last post by:
I'm using C# to create an ASP .NET website backed by a MySQL database. One of the things I do often on the site is populate a DataList by binding it to a DataSet pulled from the database. However,...
9
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Is it possible to alter the string type? Basicly what i want to do is to create my own string type. lets call it sqlString, when i use this it will always use replace on the string...
2
by: zhengfish | last post by:
HI,all I have a class named CCC which have some members of std::string type. I declare a class pointer like this: CCC* ccc = new CCC( xml_input ); then I send the ccc to another module with...
3
by: Alex K. | last post by:
Hi all I need a string type with only three possible values: let's say "A", "B", "C". In other words, I need something like enum but of string type: enum MyStrings { sA = "A", sB = "B", sC...
4
by: thomas.pohl | last post by:
Hi, let's assume you want to nicely print the content of a list except for one (or some) individual item. You could do it like this: t = print("text: %s\nvalues: %i %i %i" % (t, t, t, t)) ...
1
by: Mike | last post by:
Hi, I'm passing a JSON-encoded string to json_decode() and am expecting its output to be an object type, but am getting a string type instead. How can I return an object? In the docs, the...
1
by: mathewgk80 | last post by:
Hi all, I would like to convert dataset type to string type... i am using asp.net,c#.net and sql server.. Please help me. Regards, Mathew
1
by: kenneth6 | last post by:
ctype.h: toupper(c), tolower(c): change case of char isalphanum(c), isalpha(c), isdigit(c), ispunct(c), isspace(c), iscntrl(c), islower(c), isupper(c): boolean checks on type of char i know...
1
by: kenneth6 | last post by:
I am writing sth about data in string type in console mode. now, I transform it to Windows Form Application, i know managed type should be used here instead of the unmanaged type. should I change...
6
by: SchoolOfLife | last post by:
Does an object in C++ of std::string type terminates with null character? The charater string literals in C++ (to maintain backward compatibility with C) end with a null character. Is this same with...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.