Abs Function

   
Abs Function

Class

System.Math

Syntax

 Math.Abs(   value   ) 
value (required; any valid numeric expression)

A number whose absolute value is to be returned

Return Value

The absolute value of value . The data type is the same as that of the argument passed to the function.

Description

Returns the absolute value of value . If value is an uninitialized variable, the return value is 0

Rules at a Glance

  • Only numeric values can be passed to the Abs function.

  • This is a Shared member of the Math class, so it can be used without creating any objects.

Example

In this example, the LineLength function is used to determine the length of a line on the screen. If the line runs from left to right, X1 is less than X2 , and the expression X2 - X1 returns the length of the line. If, however, the line runs from right to left, X1 is greater than X2 , and a negative line length is returned. As you know, in most circumstances it does not matter which way a line is pointing; all you want to know is how long it is. Using the Abs function allows you to return the same figure whether the underlying figure is negative or positive:

 Function LineLength(X2 as Integer) as Integer     Dim X1 As Integer     X1 = 100     LineLength = Math.Abs(X2 - X1) End Function 

Programming Tips and Gotchas

Because the Abs function can only accept numeric values, you may want to check the value you pass to Abs using the IsNumeric function to avoid generating an error. This is illustrated in the following code snippet:

 If IsNumeric(sExtent) Then    Math.Abs(sExtent)    ... End If 

VB.NET/VB 6 Differences

In VB 6, Abs is an intrinsic VB function. In the .NET platform, it is a member of the Math class in the System namespace, and so it is not part of the VB.NET language.

See Also

Sign Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net