VB.Net - 数据类型

数据类型是指用于声明不同类型的变量或函数的扩展系统.变量的类型决定了它在存储中占用的空间大小以及如何解释存储的位模式.

VB.Net中可用的数据类型

VB.Net提供了广泛的数据类型.下表显示了所有可用的数据类型和减号;

数据类型存储分配值范围
Boolean取决于实施平台错误
Byte1字节0到255(无符号)
Char2字节0到65535(无符号)
Date8字节0:00 :00年1月1日00:00(午夜),9999年12月31日晚上11:59:59
Decimal16字节0至+/- 79,228,162,514,264,337,593,543,950,335(+/- 7.9 ... E + 28),无小数点; 0到+/- 7.9228162514264337593543950335,小数点右边有28个位置
Double8字节

-1.79769313486231570E + 308至-4.94065645841246544E-324,负值

4.94065645841246544E-324至1.79769313486231570E + 308,正值

Integer4个字节-2,147,483,648到2,147,483,647(签名)
Long8字节-9,223,372,036,854,775,808到9,223,372,036,854,775,807(签名)
Object

32位平台上的4个字节

64位平台上的8个字节

任何类型都可以存储在Object类型的变量中
SByte1字节-128到127(签名)
Short2个字节-32,7 68到32,767(签名)
Single4个字节

-3.4028235E + 38到-1.401298E-45为负值;

1.401298E-45到3.4028235E + 38为正值

String取决于实施平台0到大约20亿个Unicode字符
UInteger4个字节0到4,294,967,295(无符号)
ULong8字节0到18,446,744,073,709,551,615(未签名)
User-Defined取决于实现平台结构的每个成员都有一个由其数据类型和独立性决定的范围其他成员的范围
UShort2字节0到65,535(未签名)

示例

以下示例演示了一些类型的使用 :

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

编译并执行上述代码时,会产生以下结果 :

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

VB.Net中的类型转换函数

VB.Net提供以下内联类型转换函数 :

Sr.No.功能&描述
1

CBool(expression)

将表达式转换为布尔数据类型.

2

CByte(expression)

将表达式转换为字节数据类型.

3

CChar(expression)

转换Char数据类型的表达式.

4

CDate(expression)

将表达式转换为Date数据类型

5

CDbl(expression)

将表达式转换为Double数据类型.

6

CDec(expression)

将表达式转换为Decimal数据类型.

7

CInt(expression)

将表达式转换为整数数据类型.

8

CLng(expression)

转换表达式到长数据类型.

9

CObj(expression)

将表达式转换为对象类型.

10

CSByte(expression)

将表达式转换为SByte数据类型.

11

CShort(expression)

将表达式转换为短数据类型.

12

CSng(expression)

转换表达式为单数据类型.

13

CStr(expression)

将表达式转换为String数据类型.

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
12/4/2012
1
1