..


Sponsored Links

Calculate whether a numeric value is between two values ​​in VBScript

Article written by Luca Ruggiero

A few days ago I found to work in and use the SQL BETWEEN to verify that a search parameter was between two values, and I realized it might be useful in some cases even within a function like that of programming code, and not just in a search string on a database.

At the end I wrote a function that performs the same task using VBScript.

After this introduction, "all in one breath," I make a premise: I chose to set this article in client-side VBScript to give everyone a chance to test it immediately, but the same code can also be used in ASP than in Visual Basic, or even following the same logic that I have followed, it becomes very easy to write one in PHP, Java, Javascript or other which, like VBScript, do not have this feature natively between the default.

Let's see the code of the custom function Between






 Function Between (MyValue, StartValue, EndValue)



    



 If IsEmpty (MyValue) Then



        



 Between 0 =



    



 ElseIf IsEmpty (StartValue) Then



        



 Between 0 =



    



 ElseIf IsEmpty (EndValue) Then



        



 Between 0 =



    



 ElseIf IsNumeric (MyValue) = False Then



        



 Between 0 =



    



 ElseIf IsNumeric (StartValue) = False Then



        



 Between 0 =



    



 ElseIf IsNumeric (EndValue) = False Then



        



 Between 0 =



    



 Else



        



 If MyValue> = StartValue And MyValue <= EndValue Then



            



 Between 1 =



        



 Else



            



 Between 0 =



        



 End If



    



 End If







 End Function



The function takes three parameters: the first is the number to evaluate whether including or not including the starting value and the target value, the second and the third parameter, however, are precisely the numbers of departure and destination.

Then, using the function as follows trivially

 



 document.write Between (1, 0, 10)

 
we check that 1 is between 0 and 10, and of course it is, then the result will be "1" (true).

In this case, however,

 



 Between document.write (25, 0, 10)

 
receive "0" (false) because 23 is not between 0 and 10.

We continue to examine the function. We perform a series of conditional tests to verify that the parameters are not left empty and which are numerical.

To accept or not to filter the numerical parameters of type string can be used within the custom function, the function default conversion "CInt".

For reasons of space I have set a series of conditions for each individual field, but you can use a slim single condition, separating the individual checks with the operator "Or".

Now let's see a practical use of this function being tested






 Dim number, start, end







 number = 1







 start = 0







 end = 10







 If Between (number, start, end) = 1 Then



    



 Else



    



 End If



Specifically, the number to evaluate the value of departure and destinations of the variables with a meaningful name, at which point I make a good condition using our built-in function as a conditional filter.

In the same category ...
E-Learning
ASP Zero (Ebook) ASP Zero (Ebook)
Learning Microsoft ASP and VBScript from scratch. At only 29 €.
Visual Basic 6 (Course) Visual Basic 6 (Course)
Make Desktop Applications with VB6. From 39 €.
Sponsored Links