Textbox.text, Input to Integer

G

Guest

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
 
R

Russell Jones

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).
 
K

Keith Wilson

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
 
C

Cor Ligthert

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
 
C

Cor Ligthert

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
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
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/
 
C

Cor Ligthert

Hi Kudzu,
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
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
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/
 
C

Cor Ligthert

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
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
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/
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
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.
 
H

Herfried K. Wagner [MVP]

* "Russell Jones said:
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.
 
H

Herfried K. Wagner [MVP]

* "Chad Z. Hower aka Kudzu said:
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.
 
C

Cor Ligthert

Hi Herfried,

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

Cor
 
H

Herfried K. Wagner [MVP]

Hi Cor,

* "Cor Ligthert said:
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
///
 
C

Cor Ligthert

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
 
C

Chad Z. Hower aka Kudzu

(e-mail address removed) (Herfried K. Wagner [MVP]) wrote in
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/
 
H

Herfried K. Wagner [MVP]

Hi Cor,

* "Cor Ligthert said:
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!
///
 
H

Herfried K. Wagner [MVP]

* "Chad Z. Hower aka Kudzu said:
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.
 
C

Cor Ligthert

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
 

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