Val Function in VB.NET

How to use the Val function in VB.NET?

Explanation

Val Function

Val Function in Visual Basic.net 2008 is used to return the numbers contained in a string as a numeric value of appropriate type.
Syntax:

Function Val(ByVal Expression As String) As Double
or
Function Val(ByVal Expression As Object) As Double
or
Function Val(ByVal Expression As Char) As Integer
Example:

Module Module1
Sub Main()
Console.WriteLine("Value of 2 is: " & Val("1"))
Console.WriteLine("Value of 2,3 is: " & Val("2,3"))
Console.ReadLine()
End Sub
End Module
Result:

Value of 2 is: 1
Value of 2,3 is: 2

In the above example, all the number contained in string is returned, but if a number is after a non numeric character is not returned. Thus Val string function can be used.

Visual Basic Tutorial


Ask Questions

Ask Question