VB.Net – 数据类型

VB.Net – 数据类型


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

VB.Net 中可用的数据类型

VB.Net 提供了广泛的数据类型。下表显示了所有可用的数据类型 –

Data Type 存储分配 取值范围
Boolean 取决于实施平台
Byte 1 字节 0 到 255(无符号)
Char 2 字节 0 到 65535(无符号)
Date 8 字节 0001 年 1 月 1 日 0:00:00(午夜)至 9999 年 12 月 31 日晚上 11:59:59
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(无符号)
User-Defined 取决于实施平台 结构的每个成员都有一个由其数据类型决定的范围,并且与其他成员的范围无关
UShort 2 字节 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)

将表达式转换为日期数据类型

5

CDbl(expression)

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

6

CDec(expression)

将表达式转换为十进制数据类型。

7

CInt(expression)

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

8

CLng(expression)

将表达式转换为 Long 数据类型。

9

CObj(expression)

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

10

CSByte(expression)

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

11

CShort(expression)

将表达式转换为 Short 数据类型。

12

CSng(expression)

将表达式转换为单一数据类型。

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

觉得文章有用?

点个广告表达一下你的爱意吧 !😁