数据类型是指用于声明不同类型的变量或函数的扩展系统。变量的类型决定了它在存储器中占用多少空间以及如何解释存储的位模式。
VB.Net数据类型
VB.Net提供了广泛的数据类型。下表显示了所有VB.Net可用的数据类型:
数据类型 | 存储分配(大小) | 值范围 |
---|---|---|
Boolean |
取决于实施平台 | True 或False |
Byte |
1个字节 | 0 ~ 255 (无符号) |
Char |
2个字节 | 0 ~ 65535 (无符号) |
Date |
8个字节 | 0001年1月1日00:00:00(午夜)至9999年12月31日11:59:59 PM |
Decimal |
16个字节 | 0 ~ +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9...E+28) 没有小数点; 0 ~ +/-7.9228162514264337593543950335 ,在小数点右边有28 位数字值 |
Double |
8个字节 | 负值范围:-1.79769313486231570E + 308 至-4.94065645841246544E-324 ;正值范围:4.94065645841246544E-324 至1.79769313486231570E+308 |
Integer |
4个字节 | -2,147,483,648 至2,147,483,647 (有符号) |
Long |
8个字节 | -9,223,372,036,854,775,808 至9,223,372,036,854,775,807 (带符号) |
Object |
在32位平台上有4 个字节,在64位平台上有8 个字节 |
任何类型都可以存储在Object 类型的变量中 |
SByte |
1个字节 | -128 至127 (有符号) |
Short |
2个字节 | -32,768 至32,767 (有符号) |
Single |
4个字节 | -3.4028235E + 38 到-1.401298E-45 为负值;1.401298E-45 至3.4028235E + 38 为正值 |
String |
取决于实施平台 | 0 到约20 亿个Unicode 字符 |
UInteger |
4个字节 | 0 到4,294,967,295 (无符号) |
ULong |
8个字节 | 0 到18,446,744,073,709,551,615 (无符号) |
用户自定义 | 取决于实施平台 | 结构中的每个成员都有一个由其数据类型决定的范围,而与其他成员的范围无关 |
UShort |
2个字节 | 0 ~ 65,535 (无符号) |
示例
以下示例演示了一些类型的使用,创建一个项目:DataTypes,并创建一个VB文件:DataTypes.vb,代码如下所示 –
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
执行上面示例代码,得到以下结果 –
VB.Net类型转换函数
VB.Net提供了以下的一致类型转换函数:
编号 | 方法 | 描述 |
---|---|---|
1 | CBool(expression) |
将表达式转换为布尔数据类型。 |
2 | CByte(expression) |
将表达式转换为Byte 数据类型。 |
3 | CChar(expression) |
将表达式转换为Char 数据类型。 |
4 | CDate(expression) |
将表达式转换为日期数据类型 |
5 | CDbl(expression) |
将表达式转换为Double 数据类型。 |
6 | CDec(expression) |
将表达式转换为十进制数据类型。 |
7 | CInt(expression) |
将表达式转换为Integer 数据类型。 |
8 | CLng(expression) |
将表达式转换为Long 数据类型。 |
9 | CObj(expression) |
将表达式转换为对象类型。 |
10 | CSByte(expression) |
将表达式转换为SByte 数据类型。 |
11 | CShort(expression) |
将表达式转换为Short 数据类型。 |
12 | CSng(expression) |
将表达式转换为Single 数据类型。 |
13 | CStr(expression) |
将表达式转换为字符串数据类型。 |
14 | CUInt(expression) |
将表达式转换为UInt 数据类型。 |
15 | CULng(expression) |
将表达式转换为ULng 数据类型。 |
16 | CUShort(expression) |
将表达式转换为UShort 数据类型。 |
示例:
以下示例演示了上述一部分函数的使用:
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
执行上面示例代码,得到以下结果 –
True -1 True 2017/11/6 1 2