data types refer to an extensive system used for declaring variables or functions of different types. the type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
data types available in vb.net
vb.net provides a wide range of data types. the following table shows all the data types available −
data type | storage allocation | value range |
---|---|---|
boolean | depends on implementing platform | true or false |
byte | 1 byte | 0 through 255 (unsigned) |
char | 2 bytes | 0 through 65535 (unsigned) |
date | 8 bytes | 0:00:00 (midnight) on january 1, 0001 through 11:59:59 pm on december 31, 9999 |
decimal | 16 bytes | 0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9...e+28) with no decimal point; 0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal |
double | 8 bytes |
-1.79769313486231570e+308 through -4.94065645841246544e-324, for negative values 4.94065645841246544e-324 through 1.79769313486231570e+308, for positive values |
integer | 4 bytes | -2,147,483,648 through 2,147,483,647 (signed) |
long | 8 bytes | -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807(signed) |
object |
4 bytes on 32-bit platform 8 bytes on 64-bit platform |
any type can be stored in a variable of type object |
sbyte | 1 byte | -128 through 127 (signed) |
short | 2 bytes | -32,768 through 32,767 (signed) |
single | 4 bytes |
-3.4028235e+38 through -1.401298e-45 for negative values; 1.401298e-45 through 3.4028235e+38 for positive values |
string | depends on implementing platform | 0 to approximately 2 billion unicode characters |
uinteger | 4 bytes | 0 through 4,294,967,295 (unsigned) |
ulong | 8 bytes | 0 through 18,446,744,073,709,551,615 (unsigned) |
user-defined | depends on implementing platform | each member of the structure has a range determined by its data type and independent of the ranges of the other members |
ushort | 2 bytes | 0 through 65,535 (unsigned) |
example
the following example demonstrates use of some of the types −
module datatypes sub main() dim b as byte dim n as integer dim si as single dim d as double dim da as date dim c as char dim s as string dim bl as boolean b = 1 n = 1234567 si = 0.12345678901234566 d = 0.12345678901234566 da = today c = "u"c s = "me" if scriptengine = "vb" then bl = true else bl = false end if if bl then 'the oath taking console.write(c & " and," & s & vbcrlf) console.writeline("declaring on the day of: {0}", da) console.writeline("we will learn vb.net seriously") console.writeline("lets see what happens to the floating point variables:") console.writeline("the single: {0}, the double: {1}", si, d) end if console.readkey() end sub end module
when the above code is compiled and executed, it produces the following result −
u and, me declaring on the day of: 12/4/2012 12:00:00 pm we will learn vb.net seriously lets see what happens to the floating point variables: the single:0.1234568, the double: 0.123456789012346
the type conversion functions in vb.net
vb.net provides the following in-line type conversion functions −
sr.no. | functions & description |
---|---|
1 |
cbool(expression) converts the expression to boolean data type. |
2 |
cbyte(expression) converts the expression to byte data type. |
3 |
cchar(expression) converts the expression to char data type. |
4 |
cdate(expression) converts the expression to date data type |
5 |
cdbl(expression) converts the expression to double data type. |
6 |
cdec(expression) converts the expression to decimal data type. |
7 |
cint(expression) converts the expression to integer data type. |
8 |
clng(expression) converts the expression to long data type. |
9 |
cobj(expression) converts the expression to object type. |
10 |
csbyte(expression) converts the expression to sbyte data type. |
11 |
cshort(expression) converts the expression to short data type. |
12 |
csng(expression) converts the expression to single data type. |
13 |
cstr(expression) converts the expression to string data type. |
14 |
cuint(expression) converts the expression to uint data type. |
15 |
culng(expression) converts the expression to ulng data type. |
16 |
cushort(expression) converts the expression to ushort data type. |
example
the following example demonstrates some of these functions −
module datatypes sub main() dim n as integer dim da as date dim bl as boolean = true n = 1234567 da = today console.writeline(bl) console.writeline(csbyte(bl)) console.writeline(cstr(bl)) console.writeline(cstr(da)) console.writeline(cchar(cchar(cstr(n)))) console.writeline(cchar(cstr(da))) console.readkey() end sub end module
when the above code is compiled and executed, it produces the following result −
true -1 true 12/4/2012 1 1