Check Integer Value on Asmx - asp.net

i have sample web method on my services,
<WebMethod()> _
Public Function AddThis(ByVal x As Integer, ByVal y As Integer) As Integer
Dim mySum As Integer
If Not IsNumeric(x) Then
Return 0
End If
mySum = x + y
Return mySum
End Function
when i debug it, suddnely i made mistake about x or y value , and it give me an error :
System.ArgumentException: Cannot convert gf to System.Int32.
Parameter name: type ---> System.FormatException: Input string was not in a correct format.
i try to check the value :
If Not IsNumeric(x) Then
Return 0
End If
but keep getting the error, is it possible to check the value first before run the services?

At the point you check "IsNumeric(x)", X will always be numeric.
You expect an Integer as parameters, so the error will occur the moment you call the function AddThis.
You will have to check X and Y before calling this function. Or if that is not possible, use something like this:
Public Function AddThis(ByVal x As String, ByVal y As String) As Integer
Dim mySum As Integer
If Not IsNumeric(x) Or Not IsNumeric(y) Then
Return 0
End If
mySum = Integer.Parse(x) + Integer.Parse(y)
Return mySum
End Function

Related

Tracing cases of failure got

I'm trying to populate an integer variable from a character variable. If there is any error found I want to show the error message and trace all the possible cases for failure got.
//Defining variable
Define variable char_value as character no-undo initial "kk".
Define variable int_value as integer no-undo.
define variable ix as integer no-undo.
Assign int_value = integer(char_value) no-error.
IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES > 0 THEN
DO:
MESSAGE ERROR-STATUS:NUM-MESSAGES
" errors occurred during conversion." SKIP
"Do you want to view them?"
VIEW-AS ALERT-BOX QUESTION BUTTONS YES-NO
UPDATE view-errs AS LOGICAL.
IF view-errs THEN
DO ix = 1 TO ERROR-STATUS:NUM-MESSAGES:
MESSAGE ERROR-
STATUS:GET-NUMBER(ix)
ERROR-STATUS:GET-
MESSAGE(ix).
END.
END.
There are two conditions which I want to know.
What char value I gave so that no. Of error returns will be more than 1.
How can I trace all the possible cases for failure got.
The built-in conversion routine does not do what you want it to do. So you will need to parse your input prior to attempting to convert it. Something like this:
function isDigit returns logical ( input d as character ):
if length( d ) = 1 then
return ( index( "0123456789", d ) > 0 ).
else
return no.
end.
procedure checkInteger:
define input parameter integerString as character no-undo.
define output parameter errorList as character no-undo.
define output parameter ok as logical no-undo.
define variable i as integer no-undo.
define variable n as integer no-undo.
define variable c as character no-undo.
ok = yes.
n = length( integerString ).
do i = 1 to n:
c = substring( integerString, i, 1 ).
if i = 1 and c = "-" then next.
if isDigit( c ) = no then
do:
ok = no.
errorList = errorList + substitute( "The character '&1' at offset &2 is not a valid integer value~n", c, i ).
end.
end.
errorList = trim( errorList, "~n" ). // remove the trailing newline (if any)
return.
end.
define variable ok as logical no-undo.
define variable errorList as character no-undo.
run checkInteger( "12x34y56z789", output errorList, output ok ).
if ok = yes then
message "string is a properly formed integer, go ahead and convert it".
else
message
"string was not correctly formed, do not try to convert it" skip
errorList
view-as alert-box information
.
Note #1 If the input contains unprintable characters the errorList string will display it literally and it will look kind of funny. You could, of course, encode them to be more readable. Doing so is left as an exercise. Or another question.
Note #2 This code makes no attempt to check that the string value will fit into an integer or an int64. That is also left as an exercise.
While you can make your parsing as complex as you like, I would just keep it simple and ensure the user is provided enough information, which in this case is the complete input value:
def var cc as char initial "kk".
def var ii as int.
ii = integer( cc ).
catch e as progress.lang.error:
message quoter( cc, "'" ) e:getMessage(1) view-as alert-box.
end catch.

Select number from a string classic asp

I need to get a 4/5 digit number which always comes directly after a #
For example it'll be "Item Title (#1234)" however it's not always in the same place or at the end.
Not sure how I go about doing that
You need to manually parse the string.
I've made a quick function for this:
'//return the number after delimeter string, if exists (first occurance only)
'//in case no number exists, returns Empty value
Function FindNumberAfter(rawValue, delimeterString)
Dim index, x, curChar
Dim sBuffer
FindNumberAfter = vbEmpty
index = InStr(rawValue, delimeterString)
If index>0 Then
For x=index+Len(delimeterString) To Len(rawValue)
curChar = Mid(rawValue, x, 1)
If IsNumeric(curChar) Then
sBuffer = sBuffer & curChar
Else
Exit For
End If
Next
If Len(sBuffer)>0 Then FindNumberAfter = CLng(sBuffer)
End If
End Function
Usage in your case:
Response.Write(FindNumberAfter("Item Title (#1234)", "#"))

function variable does not live outside a for loop

I have a generic function in julia that the aim is to say if a member of a vector of
a given dimension is negative or not. After a few variations I have:
function any(vec)
dim = size(vec)
for i in 1:dim[2]
fflag = vec[1,i] < 0
println("Inside any, fflag = ", fflag)
if fflag == true
result = 0
println("blabla ", result)
break
else
result =1
println("blabla ", result)
continue
end
end
println("hey, what is result? ")
println(result)
return result
end
If I run a test I found the following result:
Inside any, fflag = false
blabla 1
Inside any, fflag = false
blabla 1
Inside any, fflag = false
blabla 1
hey, what is result?
result not defined
at In[7]:57
I don't know why the compiler says me that 'result' is not defined. I know the variable exist but why does not live outside the for loop?
The documentation on variable scoping clearly states that a for loop defines a new scope. This means result is going out of scope when execution leaves the for loop. Hence it is undefined when you call println(result)
Defining result in advance of the for loop should give the behaviour you are expecting:
function any(vec)
dim = size(vec)
result = -1
for i in 1:dim[2]
...
Or if you do not wish to assign a default value, and are sure the for loop will set its value, you can do:
function any(vec)
dim = size(vec)
local result
for i in 1:dim[2]
...
In the first example, if the for loop does not set a value, result will be -1.
In the the second example, not setting a value in the for loop will leave result undefined.

Array.IndexOf always returns -1

This is my code.
Public Sub SomeFucntion(ByVal test As Short)
SomeOtherFucntion(MyArray)
If Array.IndexOf(MyArray, test) <> -1
//....
End If
End Sub
test return value 10.
But i am getting IndexOf value as -1.
Inside QuickWatch in VS 2005 when i put the value as 10 instead of test i am getting the correct index.
My array is single dimensional array which now has 2,5 and 10. Since its got 10 it should ideally return 2 as the index.
The problem might be caused by the fact that your array contains integers, but the value you are looking for is a short. Consider the following example:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, value)) ' Prints -1
If the array contains integers, you need to convert your short into an integer first, for example, by using CInt:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' Prints 0
Edit: Note that the declared type has nothing to do with this. Let's declare the array as Object, since that's what you mentioned in your comment (note that the following example requires Option Strict Off, which is bad):
Dim myArray As Object = New Integer() {5}
Dim value As Object = 5S ' Short literal
Console.WriteLine(Array.IndexOf(myArray, value)) ' still returns -1
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' returns 0
Note: You can make that conversion implicit by declaring your function as Public Sub SomeFunction(ByVal test As Integer).
Dim test As Short = 5
Dim MyArray() As Short = {1, 3, 4, 5, 7, 3}
If Array.IndexOf(MyArray, test) <> -1 Then
MessageBox.Show("Index Found.")
End If
The above works. since test is declared as short, make sure MyArray is also declared as short.

ASP.net - Does a return statement in function stop the function?

Given the function
'Returns true if no cell is > 1
Function isSolutionValid() As Boolean
Dim rLoop As Integer
Dim cLoop As Integer
For cLoop = 0 To canvasCols - 1
For rLoop = 0 To canvasRows - 1
If canvas(cLoop, rLoop) > 1 Then
Return False
End If
Next
Next
Return True
End Function
When 'return false' is fired, I'm assuming that the function is stopped at that point and there's no need for exit fors and things like that?
That is correct.
See Function Statement.
Returns control to the code that
called a Function, Sub, Get, Set, or
Operator procedure.

Resources