Implicit & explicit type Conversion in VB.net
By: Issac
Implicit conversion is done automatically by .net compiler lets see a example of how it is done
Dim iDbl As Double
Dim iInt As Integer
iDbl = 1.13
MsgBox("The value of iDbl is " iDbl)
iInt = iDbl
MsgBox("The value of iInt is " iInt)
- Declare a Double datatype variable iDble
- Declare an Integer datatype variable iInt
- Assign a decimal value to iDbl
- Display the value of iDbl
- Assign the value of iDbl to iInt
- Display the value of iInt
The first messagebox displays the value of iDbl as 1.13, the second messegebox display the value of iInt is 1, and iInt is displayed as 1 because the value is narrowed to 1 to fit in an Integer variable.Here the Compiler made the conversion for us. These types of conversions are called Implicit Conversion.
For this we need to keep the Option strict switch in OFF mode
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim d As Double
Dim i As Integer
d = 1.13
MsgBox("The value of d is " & d)
iInt = iDbl
'after conversion
MsgBox("The value of i is " & i)
End Sub
End Class
Explicit Type Conversions
While coding sometimes we may have to perform conversions which cannot be done by the compiler such conversions are called as Explicit conversions.An explicit conversion uses a type conversion keyword. With these conversion keywords we can perform the Explicit Conversion.
Archived Comments
1. Billyweimb
View Tutorial By: Billyweimb at 2017-07-20 13:55:48
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net