only integer

  • Thread starter Thread starter Maarten
  • Start date Start date
M

Maarten

hi all,

how can i retrieve the type if a value inserted in an inputbox.

what i want to do is make shure people only insert an integer type, if they
don't they get an error message.


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

inti = InputBox("Value", "Read frequenty")

End Sub



what should i add to this code that when the variable inti is'nt a number,
they get a massagbox "only numbers" .



kind regards Maarten
 
Maarten said:
how can i retrieve the type if a value inserted in an inputbox.

what i want to do is make shure people only insert an integer type, if
they don't they get an error message.


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

inti = InputBox("Value", "Read frequenty")

\\\
inti = ...
Try
i = CInt(inti)
Catch
MsgBox("The number entered was not an integer.")
End Try
///

..NET 2.0 will have a 'Int32.TryParse' method.
 
hi,
thanks for the reply, but...

i tried yourre code, but it won't give the messagebox if i enter a non
integer

kind regards
 
I should, what value did you enter? Post your new code?

Chris

Maarten said:
hi,
thanks for the reply, but...

i tried yourre code, but it won't give the messagebox if i enter a non
integer

kind regards
 
You could use the following function. It is only good for checking for
non-negative numbers.
if bNumbersOnly = True, the routine will return a -1 if the string is
not all numbers.
if bNumbersOnly = False, the routine will return the integer value of
all of the numbers in a string and ignore all other characters. $1,234
would return 1234. "There are 12 entries" would return 12. "Found 1 in
2000" would return 12000. So be careful out there! :)

'---------- Get the numbers from a string ----------
Public Function AtoI(ByVal s As String, ByVal bNumbersOnly As
Boolean) As Integer
Dim nLen As Integer ' Length of the
string
Dim i As Integer ' Loop counter
Dim nDigit As Integer ' Each digit
Dim nTot As Integer

nTot = 0 ' Starting value
nLen = s.Length ' Number of
characters
For i = 0 To nLen - 1 ' Check all
characters
nDigit = Asc(s.Substring(i, 1)) - 48 ' Get the current
digit
If nDigit >= 0 And nDigit <= 9 Then
nTot = nTot * 10 + nDigit ' Calculate digit
Else
If bNumbersOnly Then ' Only allow
numerics
nTot = -1 ' Flag as an error
Exit For ' Force routine to
end
End If
End If
Next i
Return (nTot) ' Return Value
End Function
 
Sorry about the indentations. Apparently postings knock out leading
tabs in the code.
 
this is the code,



Dim i As Integer

Dim Tmrsp as long

Tmrsp = InputBox("Value", "Read frequenty")

Try

i = CInt(inti)

MsgBox(Tmrsp)

Catch

MsgBox("The number entered was not an integer.")

End Try

End Sub


what does the catch statement do?

thanks Maarten

Chris said:
I should, what value did you enter? Post your new code?

Chris
 
thanks for the code, but i have a problem with the function

i've never workes with functions, and i don't know how to send the text
trough the function, and then retreave it.

sry for this beginners question, but i'm realy intrested to know how it
works and how to make them.

kind regards Maarten
 
You could try using regex for this kind of test.

Dim s as string = InputBox("Value", "Read frequenty<sic>")

Dim rNumber As New System.Text.RegularExpressions.Regex("^\d*$")
If rNumber.IsMatch(s) Then
msgbox("ok")
Else
msgbox("not a whole number")
End If

HTH,
Greg
 
See below...
this is the code,



Dim i As Integer

Dim Tmrsp as long

Tmrsp = InputBox("Value", "Read frequenty")

Tmrsp is now a long with the value typed in inputbox.
Please notice that you do nothing else with this variable then print a
messagebox with its value. What this example originally was trying to
show you is (if I am not mistaken) that, by using CInt and a string as
its parameter, this function would fail and would jump to the Catch
section showing the messagebox (here is determined that the string
entered was not an number). Still I find Double.TryParse(...) easier.
And if you don't mind using the vb classes:
Microsoft.VisualBasic.Information.IsNumeric("a string")
 
Well you are trying to Cint(inti) but you never set it to a value. Try
this:
Dim i As Integer

Dim Tmrsp as long

Tmrsp = InputBox("Value", "Read frequenty")

Try

i = CInt(Tmrsp)

MsgBox(Tmrsp)

Catch

MsgBox("The number entered was not an integer.")

End Try

End Sub


The try...catch handles an exception. An exception will be thrown by the
Cint() funciton if it can not convert the Tmrsp variable to a integer. Read
up in the help file on it.

Chris
 
you can try this,

if not isnumeric(InputBox("Value", "Read frequenty")) then
msgbox "only numbers please"
endif
 
Does the OP want any number or only an "integer" (aka whole number) ???

Greg
 
Maarten said:
Dim i As Integer

Dim Tmrsp as long

Tmrsp = InputBox("Value", "Read frequenty")

Try

i = CInt(inti)


What's 'inti'?

Notice that 'InputBox' returns a string, so there is an implicit type
conversion done in the code above without 'Option Strict On'.

\\\
Dim s As String = InputBox("Value", Read fequenty")
Dim i As Integer
Try
i = CInt(s)
...
Catch
MsgBox("You didn't enter an integer!")
End Try
///

Add these lines on top of your source file:

\\\
Option Explicit On
Option Strict On
///
 
@Herfried K. Wagner, about the "Option Explicit On" and "Option Strict
On" settings: Isn't it more efficient to do this at project level
instead of source file level?
 
\\\
inti = ...
Try
i = CInt(inti)
Catch
MsgBox("The number entered was not an integer.")
End Try
///

Best practises suggest that you really should not be using exceptions
to control program logic. There are basically 3 types of exceptions
that an application can have:

1. Anticipated errors
2. Unanticipated errors
3. Business rule violations

( http://msdn.microsoft.com/library/d...-us/dv_vstechart/html/vbnet_errorhandling.asp )

Error handling should ordinarily be reserved for (2.) unanticipated
errors, while the other two should be coded (or refactored) to test
for proper conditions.

From the Best Practises page:
( http://msdn.microsoft.com/library/d...l/cpconbestpracticesforhandlingexceptions.asp )

"The method you choose depends on how often you expect the event to occur. If the event
is truly exceptional and is an error (such as an unexpected end-of-file), using exception
handling is better because less code is executed in the normal case. If the event happens
routinely, using the programmatic method to check for errors is better. "

Also:
( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt05.asp )

"While exception handling is recommended to create robust, maintainable code, there is an
associated performance cost. Throwing and catching exceptions is expensive. For this reason,
you should use exceptions only in exceptional circumstances and not to control regular logic
flow. A good rule of thumb is that the exceptional path should be taken less than one
time in a thousand."

HTH
LFS
 
Michel van den Berg said:
@Herfried K. Wagner, about the "Option Explicit On" and "Option Strict On"
settings: Isn't it more efficient to do this at project level instead of
source file level?

Yes, that's easier, but if you are working with a large project that was
developed with 'Option * Off' then it's easier to turn it on on file-by-file
basis.
 
Larry,

Larry Serflaten said:
Best practises suggest that you really should not be using exceptions
to control program logic.

Well, I know that, but what are the alternatives in this particular case, as
long as there is no 'Int32.TryParse'? OK, maybe we could utilize
'Double.TryParse' or regular expressions + range check, but this is IMO far
too complicated to implement and should be possible out of the box (which is
the case for .NET 2.0).
 
Herfried K. Wagner said:
Well, I know that, but what are the alternatives in this particular case, as
long as there is no 'Int32.TryParse'? OK, maybe we could utilize
'Double.TryParse' or regular expressions + range check, but this is IMO far
too complicated to implement and should be possible out of the box (which is
the case for .NET 2.0).

Too complicated? Since when has writting user interface code been a breeze ???
<g>

For one such alternative:

Dim user As String
Do
user = InputBox("Enter a number")
If user.Length = 0 OrElse user Like "*[!0-9]*" Then
MsgBox("A numeric value is required to continue.")
Else
Console.WriteLine(user)
Exit Do
End If
Loop


A more robust solution would be:

Dim user As String
Dim value As Integer
Do
user = InputBox("Enter a number")
If user.Length = 0 OrElse user Like "*[!0-9]*" Then
MsgBox("A numeric value is required to continue.")
Else
If user.Length < 11 _
AndAlso CLng(user) <= Integer.MaxValue _
AndAlso CLng(user) >= Integer.MinValue Then
value = CInt(user)
Exit Do
Else
MsgBox("Please enter a whole number between " _
& Integer.MinValue & " and " & Integer.MaxValue)
End If
End If
Loop
Console.WriteLine(value)


LFS
 

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

Back
Top