Introduction

You may recall that when studying data types, we saw that each had a corresponding function used to convert a string value or an expression to that type. As a reminder, the general syntax of the conversion functions is:

ReturnType = FunctionName(Expression)

The Expression could be of any kind. For example, it could be a string or expression that would produce a value such as the result of a calculation. The conversion function would take such a value, string, or expression and attempt to convert it. If the conversion is successful, the function would return a new value that is of the type specified by the ReturnType in our syntax.

The conversion functions are as follows:

Function  
Name Return Type Description
CBool Boolean Converts an expression into a Boolean value
CByte Byte Converts an expression into Byte number
CDbl Double Converts an expression into a floating-point number with double precision
CDec Decimal Converts an expression into a decimal number
CInt Integer Converts an expression into an integer (natural) number
CLng Long Converts an expression into a long integer (a large natural) number
CObj Object Converts an expression into an Object type
CSByte SByte Converts an expression into a signed byte
CShort Short Converts an expression into a short integer
CSng Single Converts an expression into a floating-point number with single precision
CUInt UInt Converts an expression into an unsigned integer
CULng ULong Converts an expression into an unsigned long integer
CUShort UShort Converts an expression into an unsigned short integer

Type Conversion

Conversion functions allow you to convert a known value to a another type. Besides these functions, the Visual Basic language provides a function named CType. Its syntax is:

CType(expression, typename)

As you can see, the CType() function takes two arguments. The first argument is the expression or the value that you want to convert. An example could be name of a variable or a calculation:

CType(250.48 * 14.05, ...)

The second argument is the type of value you want to convert the first argument to. From what have learned so far, this second argument can be one of the data types we reviewed in Lesson 3. Here is an example:

CType(250.48 * 14.05, Single)

If you choose one of the Visual Basic language's data types, the expression produced by the first argument must be able to produce a value that is conform to the type of the second argument:

After the CType() function has performed its conversion, it returns a value that is the same category as the second argument. For example, you can call a CType() function that converts an expression to a long integer. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Long

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

The function can also return a different type, as long as its type can hold the value produced by the expression. Here are two examples:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As UInteger

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

Or

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Single

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

If you try storing the returned value into a variable that cannot hold it, you would receive an error:

Conversion

Visual Basic Built-In Functions: Conversions

Introduction

You may recall that when studying data types, we saw that each had a corresponding function used to convert a string value or an expression to that type. As a reminder, the general syntax of the conversion functions is:

ReturnType = FunctionName(Expression)

The Expression could be of any kind. For example, it could be a string or expression that would produce a value such as the result of a calculation. The conversion function would take such a value, string, or expression and attempt to convert it. If the conversion is successful, the function would return a new value that is of the type specified by the ReturnType in our syntax.

The conversion functions are as follows:

Function  
Name Return Type Description
CBool Boolean Converts an expression into a Boolean value
CByte Byte Converts an expression into Byte number
CDbl Double Converts an expression into a floating-point number with double precision
CDec Decimal Converts an expression into a decimal number
CInt Integer Converts an expression into an integer (natural) number
CLng Long Converts an expression into a long integer (a large natural) number
CObj Object Converts an expression into an Object type
CSByte SByte Converts an expression into a signed byte
CShort Short Converts an expression into a short integer
CSng Single Converts an expression into a floating-point number with single precision
CUInt UInt Converts an expression into an unsigned integer
CULng ULong Converts an expression into an unsigned long integer
CUShort UShort Converts an expression into an unsigned short integer

Type Conversion 

Conversion functions allow you to convert a known value to a another type. Besides these functions, the Visual Basic language provides a function named CType. Its syntax is:

CType(expression, typename)

As you can see, the CType() function takes two arguments. The first argument is the expression or the value that you want to convert. An example could be name of a variable or a calculation:

CType(250.48 * 14.05, ...)

The second argument is the type of value you want to convert the first argument to. From what have learned so far, this second argument can be one of the data types we reviewed in Lesson 3. Here is an example:

CType(250.48 * 14.05, Single)

If you choose one of the Visual Basic language's data types, the expression produced by the first argument must be able to produce a value that is conform to the type of the second argument:

After the CType() function has performed its conversion, it returns a value that is the same category as the second argument. For example, you can call a CType() function that converts an expression to a long integer. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Long

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

The function can also return a different type, as long as its type can hold the value produced by the expression. Here are two examples:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As UInteger

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

Or

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Single

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

If you try storing the returned value into a variable that cannot hold it, you would receive an error:

Conversion

 

Visual Basic Built-In Functions: Int/Fix

Description

If you have a decimal number but are interested only in the integral part, to assist you with retrieving that part, the Visual Basic language provides the Int() and the Fix() functions. Their syntaxes are:

Public Shared Function Int( _
    ByVal Number As { Double | Integer | Long | 
		      Object | Short | Single | Decimal }) _
    As { Double | Integer | Long | Object | Short | Single | Decimal }
Public Shared Function Fix( _
    ByVal Number As { Double | Integer | Long | 
		      Object | Short | Single | Decimal }) _
    As { Double | Integer | Long | Object | Short | Single | Decimal }

Each function must take one argument. The value of the argument must be number-based. This means it can be an integer or a floating-point number. If the value of the argument is integer-based, the function returns the (whole) number. Here is an example

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Integer

        Number = 286345
        Console.WriteLine(Int(Number))

        Return 0
    End Function

End Module

This would produce:

Int

If the value of the argument is a decimal number, the function returns only the integral part. Here is an example

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As UInteger

        Number = 7942.225 * 202.46
        Console.WriteLine(Int(Number))

        Return 0
    End Function

End Module

This would produce:

Int

 

This function always returns the integral part only, even if you ask it to return a floating-point-based value. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Single

        Number = 286345.9924
        Console.WriteLine(Int(Number))

        Return 0
    End Function

End Module

This would produce:

Int

  

Introduction

So far, after performing a calculation, we were presenting the result "as is". To appropriately display a value, the Visual Basic language provides a function named Format. This function can be used for different types of values The most basic technique consists of passing it an expression that holds the value to display. The syntax of this function is:

Public Shared Function Format( _
   ByVal Expression As Object, _
   Optional ByVal Style As String = "" _
) As String

As mentioned above, this function can be used in various scenarios. The simplest way to use this function is to pass it a number, a string, or a date/time). The function would then produce that number.

Besides the Format() function, the Visual Basic language provides some additional functions we will review below.

Formatting a Number

So far, to display a number, we simply passed it to the Console.WriteLine() function or to another procedure. In some cases, you may want the number to display in a particular format. To control how the number should display, you can pass the second argument of the Format() function. This argument would be passed as a string.

To produce the number in a general format, you can pass the second argument as "g", "G", "f", or "F" .

To display the number with a decimal separator, pass the second argument as "n", "N", or "Standard". Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Double

        Number = 20502.48

        Console.WriteLine("Number: " & Format(Number, "STANDARD"))
        Return 0
    End Function

End Module

This would produce:

Format

An alternative to get this format is to call a function named FormatNumber. Its syntax is:

Function FormatNumber(
   ByVal Expression As Object,
   Optional ByVal NumDigitsAfterDecimal As Integer = -1,
   Optional ByVal IncludeLeadingDigit As TriState = TriState.UseDefault,
   Optional ByVal UseParensForNegativeNumbers As TriState = TriState.UseDefault,
   Optional ByVal GroupDigits As TriState = TriState.UseDefault
) As String

Only the first argument is required and it represents the value to display. If you pass only this argument, you get the same format as the Format() function called with the Standard option. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Double

        Number = 20502.48

        Console.WriteLine("Number: " & Format(Number, "STANDARD"))
        Console.WriteLine("Number: " & FormatNumber(Number))
        Return 0
    End Function

End Module

This would produce the same result as above.

If you call the Format() function with the Standard option, it would consider only the number of digits on the right side of the decimal separator. If you want to display more digits than the number actually has, call the FormatNumber() function and pass a second argument with the desired number. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Double

        Number = 20502.48

        Console.WriteLine("Number: " & Format(Number, "STANDARD") & vbCrLf & _
               "Number: " & FormatNumber(Number, 4))
        Return 0
    End Function

End Module

This would display:

Format Number

   

In the same way, if you want the number to display with less numbers on the right side of the decimal separator, specify that number.

As a third alternative, you can call the Format() function. In reality, the second argument is used to format the number with many more options. To represent the integral part of a number, you use the # sign. To specify the number of digits to display on the right side of the decimal separator, type a period on the right side of # followed by the number of 0s representing each decimal place. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Double

        Number = 20502.48

        Console.WriteLine("Number: " & Format(Number, "#.00000"))
        Return 0
    End Function

End Module

The five 0s on the right side of the period indicate that you want to display 5 digits on the right side of the period. This would produce:

Format

You can enter as many # signs as you want; it wouldn't change anything. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Double

        Number = 20502.48

        Console.WriteLine("Number: " & Format(Number, "##########.00000"))
        Return 0
    End Function

End Module

This would produce the same result as above. To specify that you want to display the decimal separator, include its character between the # signs. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Double

        Number = 20502.48

        Console.WriteLine("Number: " & Format(Number, "###,#######.00000"))
        Return 0
    End Function

End Module

This would produce:

Format

You can include any other character or symbol you want in the string to be part of the result, but you should include such a character only at the beginning or the end of the string, otherwise the interpreter might give you an unexpected result.

                   
Home

Introduction to Visual Basic Built-In Functions

Overview of Built-In Procedures

Introduction

A procedure is referred to as "built-in" if it shipped with its programming language. To make your job a little easier, the Visual Basic language is equipped with many procedures that you can use right away in your program. Based on this, before creating your own procedure, first check whether the functionality you are looking for is already implementing in one of the available procedures because those that ship with the Visual Basic language are highly reliable and should be preferred.

Before using a built-in procedure, you must of course be familiar with it. This comes either by consulting the documentation or by experience. This means that you must know its name, its argument(s), its return value, and its role. The Visual Basic programming language provides one of the richest libraries you will ever see. In fact, it is the richest of the .NET-based languages, giving you access to functions that are not directly available to other languages such as C# or C++/CLI. Because there so many of those functions, we will review only the most usually used. Eventually, when necessary, in other lessons, we may introduce new ones.

Conversion Functions

You may recall that when studying data types, we saw that each had a corresponding function used to convert a string value or an expression to that type. As a reminder, the general syntax of the conversion functions is:

ReturnType = FunctionName(Expression)

The Expression could be of any kind. For example, it could be a string or expression that would produce a value such as the result of a calculation. The conversion function would take such a value, string, or expression and attempt to convert it. If the conversion is successful, the function would return a new value that is of the type specified by the ReturnType in our syntax.

The conversion functions are as follows:

Function  
Name Return Type Description
CBool Boolean Converts an expression into a Boolean value
CByte Byte Converts an expression into Byte number
CDbl Double Converts an expression into a floating-point number with double precision
CDec Decimal Converts an expression into a decimal number
CInt Integer Converts an expression into an integer (natural) number
CLng Long Converts an expression into a long integer (a large natural) number
CObj Object Converts an expression into an Object type
CSByte SByte Converts an expression into a signed byte
CShort Short Converts an expression into a short integer
CSng Single Converts an expression into a floating-point number with single precision
CUInt UInt Converts an expression into an unsigned integer
CULng ULong Converts an expression into an unsigned long integer
CUShort UShort Converts an expression into an unsigned short integer

These functions allow you to convert a known value to a another type. Besides these functions, the Visual Basic language provides a function named CType. Its syntax is:

CType(expression, typename)

As you can see, the CType() function takes two arguments. The first argument is the expression or the value that you want to convert. An example could be name of a variable or a calculation:

CType(250.48 * 14.05, ...)

The second argument is the type of value you want to convert the first argument to. From what have learned so far, this second argument can be one of the data types we reviewed in Lesson 3. Here is an example:

CType(250.48 * 14.05, Single)

If you choose one of the Visual Basic language's data types, the expression produced by the first argument must be able to produce a value that is conform to the type of the second argument: