|
Data Type Conversion, File Extensions
Converting between Data types
In Visual Basic, data can be converted in two ways: implicitly,
which means the conversion is performed automatically, and explicitly,
which means you must perform the conversion.
Implict Conversions
Let's understand implicit conversions in code. The example below declares two
variable, one of type double and the other integer. The double data type is assigned
a value and is converted to integer type. When you run the code the result displayed
is an integer value, in this case the value displayed is 132 instead of 132.31223.
Imports System.Console
Module Module1
Sub Main()
Dim d=132.31223 as Double
Dim i as Integer
i=d
WriteLine("Integer value is" & i)
End Sub
End Module
|
Explicit Conversions
When types cannot be implicitly converted you should convert them explicitly. This
conversion is also called as cast. Explicit conversions
are accomplished using CType function.
CType function for conversion
If we are not sure of the name of a particular conversion function then we can use
the CType function. The above example with a CType
function looks like this:
Imports System.Console
Module Module1
Sub Main()
Dim d As Double
d = 132.31223
Dim i As Integer
i = CType(d, i)
'two arguments, type we are converting from, to type desired
WriteLine("Integer value is" & i)
End Sub
End Module
|
Below is the list of conversion functions which we can use in VB .NET.
CBool - use this function to convert to Bool data type
CByte - use this function to convert to Byte data type
CChar - use this function to convert to Char data type
CDate - use this function to convert to Date type
CDbl - use this function to convert to Double data type
CDec - use this function to convert to Decimal data type
CInt - use this function to convert to Integer data type
CLng - use this function to convert to Long data type
CObj - use this function to convert to Object type
CShort - use this function to convert to Short data type
CSng - use this function to convert to Single data type
CString - use this function to convert to String data type
Attributes
Attributes are those that lets us specify information about the items we are
using in VB .NET. Attributes are enclosed in angle brackets(< >) and are used
when VB .NET needs to know more beyond the standard syntax.
File Extensions in VB .NET
The files and their extensions which are created as part of the Windows Application
Project and their meaning are summarized below:
.vbproj->A Visual Basic project
Form1.vb->A form's code
AssemblyInfo.VB->Information about an assembly, includes
version information
.vbproj.user->Stores project user options
.sln->Solution file which stores solution's configuration
.suo-> Stores Solution user options
Form1.resx.NET->XML based resource template
bin directory->Directory for binary executables
obj directory->Directory for debugging binaries
Language Terminology
Briefly on some terminology when working with the language:
Module: Used to hold code
Variable: A named memory location of a specific data
type used to hold some value
Procedure: A callable series of statements which may
or may not return a value
Sub-Procedure: A procedure with no return value
Function: A procedure with return value
Methods: A procedure built into the class
Constructor: Special method used to initialize and customize
the object. It has the same name as the class
Class: An OOP class which contains data and code
Object: An instance of a class
Arrays: Programming constructs that lets us access data
by numeric index
Attributes: They are the items that specify information
about other items being used in VB. NET
|