Val Function VB.NET

Val Function

Class
Microsoft.VisualBasic.Conversion

Syntax
Val(expression)

expression

Use: Required

Data Type: String or Char

Any string representation of a number

Return Value
A Double able to hold the number contained in expression

Description
Converts a string representation of a number into a Double

Rules at a Glance

  • The Val function starts reading the string with the leftmost character and stops at the first character that it does not recognize as being part of a valid number. For example, the statement:
  • iNumber = Val("1A1")

    returns 1.

  • &O and &H (the octal and hexadecimal prefixes) are recognized by the Val function.
  • Currency symbols, such as $ and £, and delimiters, such as commas, are not recognized as numbers by the Val function.
  • The Val function only recognizes the period (.) as a decimal delimiter.
  • Prior to processing expression, Val removes spaces, tabs, and line-feed characters.

Programming Tips and Gotchas
If you are developing an international application, you should use the more modern, internationally aware CDbl function to convert strings to numbers, since CDbl can recognize all decimal separators.

ValDec Function

Class
Microsoft.VisualBasic.Conversion

Syntax
ValDec(expression)

expression

Use: Required
Data Type: String or Char
Any string representation of a number

Return Value
A Decimal able to hold the number contained in expression

Description
Converts a string representation of a number into a Decimal

Rules at a Glance

  • The ValDec function starts reading the string with the leftmost character and stops at the first character that it does not recognize as being part of a valid number. For example, the statement:
  • iNumber = ValDec("1A1")

    returns 1.

  • &O and &H (the octal and hexadecimal prefixes) are recognized by the ValDec function.
  • Currency symbols, such as $ and £, and delimiters, such as commas, are not recognized as numbers by the ValDec function.
  • The ValDec function only recognizes the period (.) as a decimal delimiter.
  • Prior to processing expression, ValDec removes spaces, tabs, and line-feed characters.

Programming Tips and Gotchas
If you are developing an international application, you should use the CDec function to convert strings
to numbers, since CDec can recognize all decimal separators.

VB .NET/VB 6 Differences
The ValDec function is new to VB .NET.

VarType Function

Class
Microsoft.VisualBasic.Information

Syntax
VarType(varname)

varname

Use: Required

Data Type: Any The name of a variable

Return Value
A member of the VariantType enumeration indicating the variable type

Description
Determines the data type of a variable

Rules at a Glance

  • The possible values returned by the function include the following members of the VariantType enumeration.

TABLE

TABLETABLE

  • If varname is a dimensioned array, the VarType function returns VariantType.Array (8192), plus the value of the array's data type. For example, an array of strings returns 8192 + 8 = 8200. You can test for an array with a code fragment such as the following:
  • If VarType(myVar) And VariantType.Array Then

  • You can extract the data type of the array with the following code fragment:
  • vartype(myVar) and &HFFFFDFFF

  • All object variables, whether late-bound or early-bound, return VariantType.Object.
  • Data types that are members of the base class library but are not wrapped by VB data types

(i.e., UINT16, UINT32, etc.) return VariantType.UserDefinedType.

VB .NET/VB 6 Differences

  • In VB 6, passing a user-defined type as an argument to the VarType function generated an error. VB .NET allows you to pass a structure as an argument to the function.
  • in VB 6, the Vartype function indicates that the data type of an object is the data type of its default property. In VB .NET, all objects, including objects (like Collection objects) that have default properties, return VariantType.Object.

VbTypeName Function

Class
Microsoft. VisualBasic.Information

Syntax
VbTypeName(urtname)

Urtname

Use: Required

Data Type: String

The name of a CTS datatype

Return Value
A String containing the name of a VB .NET datatype

Description
Returns the name of the VB .NET datatype that corresponds to a particular Common Type System (CTS) datatype

Rules at a Glance

  • urtname must be the name of a valid CTS datatype, such as Int32, UInt32, String, or DateTime.
  • If urtname is not a valid CTS datatype, the function returns Nothing.
  • If urtname is a valid CTS datatype that does not directly correspond to a VB .NET datatype, the function returns Nothing.

Example

Programming Tips and Gotchas

  • To determine the VB .NET datatype of a particular variable, call the variable's GetType method to retrieve a Type object, then call the Type object's ToString method to retrieve its datatype name. This string can then be passed to the VbTypeName function. For example:

strType = VbTypeName(myVar.GetType( ).ToString)

  • If passed the name of a structure defined with the Structure construct or an instance of a class defined with the Class construct, the VbTypeName function returns Nothing.
  • The existence of the VbTypeName function clearly indicates that VB .NET datatypes are wrappers for some CTS datatypes.

VB .NET/VB 6 Differences
The VbTypeName function is new to VB .NET.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics