Conversions (data)
Reference Guide: (come back here whenever you need to reference Ctypes)
Here is a list of types of conversion types used in VB.net
- CObj - Converts to Object Type
- CChar - Converts to Char Type
- CDate - Converts to Date Type
- CString - Convert to String Type
- CDbl - Converts to Double Type
- CBool - Converts to Bool Type
- CByte - Converts to Byte Type
- CDec - Converts to Decimal Type
- CShort - Converts to Short Type
- CSng - Converts to Single Type
- CInt - Converts to Integer Type
- CLng - Converts to Long Type
Example of Data Conversions
As always, Open VB.net and follow along, if your interested in learning, then it's best to take the steps as you read them .
VB.net data allows two ways of converting.
Implicit
(Automatic Conversions)
Explicit
(Manual Conversions)
Implict Example:
Create a New Module (Module1.vb = Module1)
Add this to your Module
Code:
Imports System.Console
Module Module1
Sub Main()
Dim dl=121.3121 as Double
Dim i as Integer
i=dl
WriteLine("Integer value is" & i)
End Sub
End Module
What does this do?
When you run the code the result displayed is an integer value, in this case the value displayed is 121 instead of 121.3111
Explicit Conversions
Sometimes types cannot be implicitly converted, in which case you need to explicitly convert it
(this type of conversion can also be called a "cast")
CType function for conversion
whenever we are uncertain of the conversion type we can use CType function.
Here is an example:
Code:
Imports System.Console
Module Module1
Sub Main()
Dim dl As Double
dl = 121.3111
Dim i As Integer
i = CType(dl, i)
WriteLine("Integer value is" & i)
End Sub
End Module
You can obviously do this outside of a module (in the form, just remove the module , and add it to the part of the form you need it to trigger on)
Next Tut will be File Extensions and Proper Conversions