目录

VB.Net - 数据类型

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

VB.Net中提供的数据类型

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

数据类型 存储分配 价值范围
BooleanDepends on implementing platform False
Byte 1个字节 0 through 255 (unsigned)
Char 2个字节 0 through 65535 (unsigned)
Date 8个字节 0999:00(午夜),时间是1991年1月1日到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 through 2,147,483,647 (signed)
Long 8个字节 -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807(signed)
Object

32位平台上的4个字节

64位平台上的8个字节

任何类型都可以存储在Object类型的变量中
SByte 1个字节 -128 through 127 (signed)
Short 2个字节 -32,768 through 32,767 (signed)
Single 4字节

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

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

StringDepends on implementing platform 0到大约20亿个Unicode字符
UInteger 4字节 0 through 4,294,967,295 (unsigned)
ULong 8个字节 0 through 18,446,744,073,709,551,615 (unsigned)
User-DefinedDepends on implementing platform 结构的每个成员具有由其数据类型确定的范围,并且独立于其他成员的范围
UShort 2个字节 0 through 65,535 (unsigned)

例子 (Example)

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

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)

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

3

CChar(expression)

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

4

CDate(expression)

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

5

CDbl(expression)

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

6

CDec(expression)

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

7

CInt(expression)

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

8

CLng(expression)

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

9

CObj(expression)

将表达式转换为Object类型。

10

CSByte(expression)

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

11

CShort(expression)

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

12

CSng(expression)

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

13

CStr(expression)

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

14

CUInt(expression)

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

15

CULng(expression)

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

16

CUShort(expression)

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

例子 (Example)

以下示例演示了其中一些功能 -

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
VB.Net - 变量.下一篇>
↑回到顶部↑
WIKI教程 @2018