×

VB.Net基本教程

VB.Net 环境设置VB.Net 程序结构VB.Net 基本语法VB.Net 数据类型VB.Net 变量VB.Net 常数和枚举VB.Net 修饰符VB.Net 声明VB.Net 指令VB.Net 运算符VB.Net 决策VB.Net 循环VB.Net 字符串VB.Net 日期和时间VB.Net 数组VB.Net 集合VB.Net 函数VB.Net Sub过程(子程序)VB.Net 类与对象VB.Net 异常处理VB.Net 文件处理VB.Net 基本控制VB.Net 对话框VB.Net 高级表单VB.Net 事件处理

VB.Net 高级教程

VB.Net 正则表达式VB.Net 数据库访问VB.Net Excel工作表VB.Net 发送邮件VB.Net XML处理VB.Net Web编程

VB.Net 有用的资源

VB.Net 有用资源

VB.Net 数据类型


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

VB.Net中提供的数据类型

VB.Net提供了多种数据类型。下表显示的所有数据类型可用的:

数据类型 存储分配 值范围
Boolean 取决于实施平台
Byte 1个字节 0到255(无符号)
Char 2个字节 0〜65535(无符号)
Date 8个字节 00:00:00(午夜),时间为0001年12月31日11时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至4294967295(无符号)
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提供以下内联类型转换函数:

SN 功能和说明
1

CBool(表达式)

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

2

CByte(表达式)

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

3

CChar(表达式)

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

4

CDate(表达式)

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

5

CDbl(表达式)

将表达式转换为双精度数据类型。

6

CDec(表达式)

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

7

CInT(表达式)

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

8

CLng函数(表达式)

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

9

CObj(表达式)

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

10

CSByte(表达式)

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

11

CShort(表达式)

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

12

CSng函数(表达式)

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

13

CStr的(表达式)

将表达式转换为字符串数据类型。

14

CUInt(表达式)

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

15

CULng(表达式)

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

16

CUShort(表达式)

将表达式转换为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

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)