473,666 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) 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.EventArg s) 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 26899
On Jul 2, 10:30 am, "Dave griffiths" <davegino...@no spam.hotmail.co m>
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.EventArg s) 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.EventArg s) 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(t xt2.Text)
Else
x = 0
End If

Or Even this:

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

Thanks,

Seth Rowe

Jul 2 '07 #2
On Jul 2, 10:42 am, rowe_newsgroups <rowe_em...@yah oo.comwrote:
On Jul 2, 10:30 am, "Dave griffiths" <davegino...@no spam.hotmail.co m>
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.EventArg s) 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.EventArg s) 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(t xt2.Text)
Else
x = 0
End If

Or Even this:

Dim x as Integer = 0
Integer.TryPars e(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.T ext))

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.EventArg s) 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.EventArg s) 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.EventArg s) 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.EventArg s) 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.t ext = "", 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(Er rorMessage 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(ByR ef ErrorMessage as String) as Boolean
If SomeBadConditio n then
ErrorMessage = "Alert User of Problem"
Exit Sub
End If
If SomeOtherBadCon dition then
ErrorMessage = "Alert User of Problem2"
Exit Sub
End If

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

Later on you can say...
-------------------------------------------------------------
Public Sub PerformCalculat ion
' check form first
Dim TheErrorMessage as String
If Not isFormValid(The PotentialErrorM essage) then
MessageBox.Show (TheErrorMessag e)
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.T ext))

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.EventArg s) 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.EventArg s) 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.EventArg s) 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.EventArg s) 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 DeveloperExpres s' 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

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

Similar topics

0
1402
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, I'm running into a bit of a problem with a specific field type in the database. It seems that fields of type TEXT (a kind of binary way of storing large amounts of text) generate the error "Unknown Target Conversion Type" when I try to bind...
9
12543
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 assigned... Instead of using:
2
1779
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 msg_queue. then this lead to a error on win32 platform, but it's works well on Redhat Linux 9.0. what's the reason?
3
1600
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 = "C"
4
1304
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)) If there was a conversion type which simply ignores the corresponding list item
1
8014
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 following returns an object: $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json));
1
1669
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
2789
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 this is for char type. If the data i am using is in string type, how can i use these functions or do the similar tasks with the string type data?
1
2547
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 the unmanaged type of string to char* for better data manipulation??
6
2486
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 an object of std::string type? Thank you...
0
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8352
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8780
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.