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

Textbox.text, Input to Integer

P: n/a
I've been reading and looking on the internet - but I just can't fin
this. Perhaps I'm approaching this the wrong way - code-wise

I'm attempting to learn how "things" work in VBNET by creating
small app - 3 textbox objects. I want the user to input data int
textbox1 and textbox2 - and then click on a button - which will tak
the values to textbox1 and textbox2 and multiply them together. Th
result - displays in textbox 3

I think I'm running into problems where I'm attempting to convert th
textbox string (I guess it considers it a string by default) - int
an integer - so that I can take the 2 integers from textbox1 and
and multiply them together. Is the code below wrong? How do I ge
the data that the user inputs into the textbox - into a variable tha
is an integer

dim one as intege
one = val(textbox1.text

Does anyone see what I'm trying to do? Can someone show me a cod
sample of what needs to be done. I would think that this would no
be too difficult

Nov 20 '05 #1
Share this Question
Share on Google+
38 Replies


P: n/a
See Integer.Parse(string), or Double.Parse(string). All the numeric types
have a Parse method that parses a string and returns a number (if possible).

"Keith" <an*******@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
I've been reading and looking on the internet - but I just can't find
this. Perhaps I'm approaching this the wrong way - code-wise.

I'm attempting to learn how "things" work in VBNET by creating a
small app - 3 textbox objects. I want the user to input data into
textbox1 and textbox2 - and then click on a button - which will take
the values to textbox1 and textbox2 and multiply them together. The
result - displays in textbox 3.

I think I'm running into problems where I'm attempting to convert the
textbox string (I guess it considers it a string by default) - into
an integer - so that I can take the 2 integers from textbox1 and 2
and multiply them together. Is the code below wrong? How do I get
the data that the user inputs into the textbox - into a variable that
is an integer?

dim one as integer
one = val(textbox1.text)

Does anyone see what I'm trying to do? Can someone show me a code
sample of what needs to be done. I would think that this would not
be too difficult.

Nov 20 '05 #2

P: n/a
You could try

dim one as integer = 0
dim two as integer = 0
dim total as integer = 0
try
one = cint(textbox1.text)
two = cint(textbox1.text)
total = one * two
catch
'error doing calc or converting to int
end try

"Keith" <an*******@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
I've been reading and looking on the internet - but I just can't find
this. Perhaps I'm approaching this the wrong way - code-wise.

I'm attempting to learn how "things" work in VBNET by creating a
small app - 3 textbox objects. I want the user to input data into
textbox1 and textbox2 - and then click on a button - which will take
the values to textbox1 and textbox2 and multiply them together. The
result - displays in textbox 3.

I think I'm running into problems where I'm attempting to convert the
textbox string (I guess it considers it a string by default) - into
an integer - so that I can take the 2 integers from textbox1 and 2
and multiply them together. Is the code below wrong? How do I get
the data that the user inputs into the textbox - into a variable that
is an integer?

dim one as integer
one = val(textbox1.text)

Does anyone see what I'm trying to do? Can someone show me a code
sample of what needs to be done. I would think that this would not
be too difficult.

Nov 20 '05 #3

P: n/a
Hi Keith,

I would do it something like this.
(I am an expert in making typos, so watch for that)

\\\
If IsNumeric(textbox1.txt) andalso Isnumeric(textbox2.txt) then
textbox3.txt = Cint(textbox1.txt) * Cint(textbox2.txt)
else
messagebox.show("The values are not numeric")
end if
///

I hope this helps?
Cor
Nov 20 '05 #4

P: n/a
Hi Keith,

Only to help, using a "try catch end try" for this kind of operatins is
not a good way to do.

The try .... cost a lot of time when there is an exception, while the only
error can be that the values are not numeric.

Use those exceptions catching for things you cannot see before (mostly
datahandling ).

Just an adittion, not ment as critique, so keep on helping.

Cor
Nov 20 '05 #5

P: n/a
"Cor Ligthert" <no**********@planet.nl> wrote in news:OJzQULmPEHA.1392
@TK2MSFTNGP09.phx.gbl:
The try .... cost a lot of time when there is an exception, while the only
error can be that the values are not numeric.


Unless you are in a loop the time overhead is insignificant. Getting results
from a user input and even triggering one catch is literally no overhead.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 20 '05 #6

P: n/a
Hi Kudzu,
The try .... cost a lot of time when there is an exception, while the only error can be that the values are not numeric.
Unless you are in a loop the time overhead is insignificant. Getting

results from a user input and even triggering one catch is literally no overhead.


I do not agree, I said only use catch if there is no better alternative. And
mostly is it, when you make your programs well. Except if there is something
that you have no influence on.

In this sample by instance it will stop the program every time the user
enters an nonnumeric character, while there is a direct test for that.

Cor
Nov 20 '05 #7

P: n/a
"Cor Ligthert" <no**********@planet.nl> wrote in
news:OP**************@TK2MSFTNGP09.phx.gbl:
> The try .... cost a lot of time when there is an exception, while the
> error can be that the values are not numeric.


Unless you are in a loop the time overhead is insignificant. Getting

results
from a user input and even triggering one catch is literally no
overhead.


I do not agree, I said only use catch if there is no better alternative.


No - you said "The try .... cost a lot of time when there is an exception"
which is the part I replied to.

If there is a non catch way to do it, of course us it. But the statement you
posted regarding performance is too general and there are a lot of learning
developers here. Thats why I clarified it.

I didnt say catch was a better choice in this situation.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Nov 20 '05 #8

P: n/a
Hi Kudzu,

Same idea and what is a lot of time, I see people here talking about things
which takes thousands of nanoseconds, and this can take parts of seconds
when it goes wrong, therefore.

However I am glad we agree.

Cor
Nov 20 '05 #9

P: n/a
"Cor Ligthert" <no**********@planet.nl> wrote in news:#FEgoanPEHA.3708
@TK2MSFTNGP10.phx.gbl:
Same idea and what is a lot of time, I see people here talking about things
which takes thousands of nanoseconds, and this can take parts of seconds
when it goes wrong, therefore.


It takes milliseconds at most. In a loop yes - but compared against the
drawing of the text in the text box and the user typing, we are talking ants
on the Titanic. ;)
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 20 '05 #10

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
I would do it something like this.
(I am an expert in making typos, so watch for that)

\\\
If IsNumeric(textbox1.txt) andalso Isnumeric(textbox2.txt) then
textbox3.txt = Cint(textbox1.txt) * Cint(textbox2.txt)


'IsNumeric(...) = True' doesn't imply that 'CInt' will be successful.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #11

P: n/a
* "Russell Jones" <ar**@nospam.northstate.net> scripsit:
See Integer.Parse(string), or Double.Parse(string). All the numeric types
have a Parse method that parses a string and returns a number (if possible).


Full ACK. I prefer 'Double.TryParse' because it doesn't throw an
exception when conversion is not successful. There will be 'TryParse'
methods for some other datatypes in .NET 2.0 too.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12

P: n/a
* "Chad Z. Hower aka Kudzu" <cp**@hower.org> scripsit:
The try .... cost a lot of time when there is an exception, while the only
error can be that the values are not numeric.


Unless you are in a loop the time overhead is insignificant. Getting results
from a user input and even triggering one catch is literally no overhead.


Depending on the machine it can take up to some seconds until the
exception is catched, so this may reduce user experience.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #13

P: n/a
Hi Herfried,

Are you sure?
Can you give a sample because that is intresting.
(In the context from my sample of course)

Cor

'IsNumeric(...) = True' doesn't imply that 'CInt' will be successful.

Nov 20 '05 #14

P: n/a
Hi Cor,

* "Cor Ligthert" <no**********@planet.nl> scripsit:
Can you give a sample because that is intresting.
(In the context from my sample of course)


\\\
Dim s As String = "12345344234234234234234234234"
If IsNumeric(s) Then ' True.
MsgBox(CStr(CInt(s))) ' Overflow exception.
End If
///
'IsNumeric(...) = True' doesn't imply that 'CInt' will be successful.


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #15

P: n/a
Hi Herfried,

I forgot to tell, I have limited the textbox length in the designer, do you
not do that with this kinds of problems.

:-)))

(I never did, i was typing it in the message, however in a real case that
should be done of course).

Cor
\\\
Dim s As String = "12345344234234234234234234234"
If IsNumeric(s) Then ' True.
MsgBox(CStr(CInt(s))) ' Overflow exception.
End If
///
'IsNumeric(...) = True' doesn't imply that 'CInt' will be successful.

Nov 20 '05 #16

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:2h************@uni-berlin.de:
Unless you are in a loop the time overhead is insignificant. Getting
results from a user input and even triggering one catch is literally no
overhead.


Depending on the machine it can take up to some seconds until the
exception is catched, so this may reduce user experience.


Any machine that takes a few seconds to handle an exception and route it to a
catch has much bigger problems. Mainly staying alive, as its from the 1960's
era.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 20 '05 #17

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:2h************@uni-berlin.de:
Unless you are in a loop the time overhead is insignificant. Getting
results from a user input and even triggering one catch is literally no
overhead.


Depending on the machine it can take up to some seconds until the
exception is catched, so this may reduce user experience.


Any machine that takes a few seconds to handle an exception and route it to a
catch has much bigger problems. Mainly staying alive, as its from the 1960's
era.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 20 '05 #18

P: n/a
* "Chad Z. Hower aka Kudzu" <cp**@hower.org> scripsit:
Unless you are in a loop the time overhead is insignificant. Getting
results from a user input and even triggering one catch is literally no
overhead.


Depending on the machine it can take up to some seconds until the
exception is catched, so this may reduce user experience.


Any machine that takes a few seconds to handle an exception and route it to a
catch has much bigger problems. Mainly staying alive, as its from the 1960's
era.


Definitely no. On my PII 350 it sometimes takes 2 to 5 seconds.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #19

P: n/a
Hi Cor,

* "Cor Ligthert" <no**********@planet.nl> scripsit:
I forgot to tell, I have limited the textbox length in the designer, do you
not do that with this kinds of problems.


\\\
Dim s As String = "2E100"
MsgBox(IsNumeric(s)) ' True.
MsgBox(CInt(s)) ' Crash!
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #20

P: n/a
* "Chad Z. Hower aka Kudzu" <cp**@hower.org> scripsit:
Unless you are in a loop the time overhead is insignificant. Getting
results from a user input and even triggering one catch is literally no
overhead.


Depending on the machine it can take up to some seconds until the
exception is catched, so this may reduce user experience.


Any machine that takes a few seconds to handle an exception and route it to a
catch has much bigger problems. Mainly staying alive, as its from the 1960's
era.


Definitely no. On my PII 350 it sometimes takes 2 to 5 seconds.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #21

P: n/a
Hi Cor,

* "Cor Ligthert" <no**********@planet.nl> scripsit:
I forgot to tell, I have limited the textbox length in the designer, do you
not do that with this kinds of problems.


\\\
Dim s As String = "2E100"
MsgBox(IsNumeric(s)) ' True.
MsgBox(CInt(s)) ' Crash!
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #22

P: n/a
Hi Herfried,

I think in this case it is than allowed to enclose this messages including
the isnumeric in a try and catch block, because of the fact that the
keyboard device can give unpredictable results. I did not know that it was
accepting this kind of numerics

:-)

Cor
Nov 20 '05 #23

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
I think in this case it is than allowed to enclose this messages including
the isnumeric in a try and catch block, because of the fact that the
keyboard device can give unpredictable results. I did not know that it was
accepting this kind of numerics


So, I would not use 'IsNumeric' in this situation ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #24

P: n/a
> So, I would not use 'IsNumeric' in this situation ;-).

What than, in the other thread the OP shows he wants that keyboard keypress
event with that

TextBox1.SelectionStart = TextBox1.Text.Length and that with a -1 when the
backspace is pushed, however I think in another situation this is very good.

Try
if isnumeric
bla

Catch
is a very big exception so no problem

End Try

However show me your (simple) alternative

Cor
Nov 20 '05 #25

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
So, I would not use 'IsNumeric' in this situation ;-).
What than, in the other thread the OP shows he wants that keyboard keypress
event with that

TextBox1.SelectionStart = TextBox1.Text.Length and that with a -1 when the
backspace is pushed, however I think in another situation this is very good.

Try
if isnumeric
bla


So, why do you need 'IsNumeric' at all?
Catch
is a very big exception so no problem

End Try

However show me your (simple) alternative


IMO, that's a very user-friendly way:

\\\
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim EventSource As TextBox = DirectCast(sender, TextBox)
Try
Dim i As Integer = Integer.Parse(EventSource.Text)
If Me.ErrorProvider1.GetError(EventSource).Length > 0 Then
Me.ErrorProvider1.SetError(EventSource, "")
End If
Catch ex As Exception
Me.ErrorProvider1.SetError(EventSource, "Value must be an integer. (" & ex.Message & ")")
End Try
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #26

P: n/a
Hi Herfried,

Look what you wrote yourself (and me as well) to Kudzu,

By using the IsNumeric all errors will be trapped by the Isnumeric, which
cost no time, however when there is an exceptional error (an overflow) it
will be catched by the Catch.

I think that is the right approach,

Cor
So, I would not use 'IsNumeric' in this situation ;-).

Nov 20 '05 #27

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:Oc*************@TK2MSFTNGP11.phx.gbl:
Definitely no. On my PII 350 it sometimes takes 2 to 5 seconds.


If just the CATCH takes that long - someting is SERIOUSLY wrong with your
installation.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 20 '05 #28

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:Oc*************@TK2MSFTNGP11.phx.gbl:
Definitely no. On my PII 350 it sometimes takes 2 to 5 seconds.


Just to eb sure - we are talking abotu this:

try
throw some excetpion
catch
code here
end try

THIS takes 2-5 seconds on your machine? Does it only take that long in th
debugger for you? If so your VS is hosed - you really need a reinstall.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Nov 20 '05 #29

P: n/a
Hi Kudzu,

That is my expirience as well in debugging mode
Definitely no. On my PII 350 it sometimes takes 2 to 5 seconds.


If just the CATCH takes that long - someting is SERIOUSLY wrong with your
installation.


Cor

Nov 20 '05 #30

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
Look what you wrote yourself (and me as well) to Kudzu,
I know. It was only a note, not a recommendation not to use
'Try...Catch' at all.
By using the IsNumeric all errors will be trapped by the Isnumeric, which
cost no time, however when there is an exceptional error (an overflow) it
will be catched by the Catch.
I doubt that the performance of 'IsNumeric' is very good. 'IsNumeric'
is very general, it will work with hexadecimal and octal numbers and
numbers in various other styles.
I think that is the right approach,


'Integer.TryParse' is the right approach, but that's .NET 2.0.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #31

P: n/a
>
I doubt that the performance of 'IsNumeric' is very good. 'IsNumeric'
is very general, it will work with hexadecimal and octal numbers and
numbers in various other styles.


IsNumeric does not take 2 to 3 seconds probably something as 2 to 3 pico
seconds and in that we go to the part where Kudzu and I do very much agree.

Cor
Nov 20 '05 #32

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
That is my expirience as well in debugging mode


OK.
Definitely no. On my PII 350 it sometimes takes 2 to 5 seconds.


If just the CATCH takes that long - someting is SERIOUSLY wrong with your
installation.


Kuzdu is right, the problem is not as big in a release version.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #33

P: n/a
* "Chad Z. Hower aka Kudzu" <cp**@hower.org> scripsit:
Definitely no. On my PII 350 it sometimes takes 2 to 5 seconds.


Just to eb sure - we are talking abotu this:

try
throw some excetpion
catch
code here
end try

THIS takes 2-5 seconds on your machine? Does it only take that long in th
debugger for you? If so your VS is hosed - you really need a reinstall.


No, it's my old machine. And yes, it's the debug version.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #34

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in news:
#a**************@TK2MSFTNGP10.phx.gbl:
* "Cor Ligthert" <no**********@planet.nl> scripsit:
That is my expirience as well in debugging mode
OK.


Do you guys have any virus scanners or other running? Can you send me a
simple app to demonstrate this? One that even logs its own time and displays
it I think is best so we can accurately compare times.
Kuzdu is right, the problem is not as big in a release version.


In release it should be negligible - in the milliseconds at most.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 20 '05 #35

P: n/a
PS - not that I want to drag Delphi in - but Delphi for the .NET framework I
can tell you for sure does not stop for several seconds on try..catch, and
I've never had my VS do it either. Thats why Id like to try a project thats
slow on your machine.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 20 '05 #36

P: n/a
* "Chad Z. Hower aka Kudzu" <cp**@hower.org> scripsit:
PS - not that I want to drag Delphi in - but Delphi for the .NET framework I
can tell you for sure does not stop for several seconds on try..catch, and
I've never had my VS do it either. Thats why Id like to try a project thats
slow on your machine.


Mhm... I don't have such a project on my machine at the moment because
the "old" machine doesn't currently have VS.NET installed :-(.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #37

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:en*************@tk2msftngp13.phx.gbl:
Mhm... I don't have such a project on my machine at the moment because
the "old" machine doesn't currently have VS.NET installed :-(.


And it only takes abnormally long on your old machine?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 20 '05 #38

P: n/a
Chad,

* "Chad Z. Hower aka Kudzu" <cp**@hower.org> scripsit:
Mhm... I don't have such a project on my machine at the moment because
the "old" machine doesn't currently have VS.NET installed :-(.


And it only takes abnormally long on your old machine?


Yes. I mixed up my debugging experience on the old machine with the
release behavior (I hardly ever run my applications in release mode).

I remember that catching exceptions took very long when accessing a
prostgresql database running in cygwin, but I assume that this had other
reasons.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #39

This discussion thread is closed

Replies have been disabled for this discussion.