当前位置: 首页 > 教程 > VB.Net >

VB.Net数据类型

精华
小牛编辑
125浏览
2023-03-14

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

VB.Net数据类型

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

数据类型 存储分配(大小) 值范围
Boolean 取决于实施平台 TrueFalse
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-3241.79769313486231570E+308
Integer 4个字节 -2,147,483,6482,147,483,647(有符号)
Long 8个字节 -9,223,372,036,854,775,8089,223,372,036,854,775,807(带符号)
Object 在32位平台上有4个字节,在64位平台上有8个字节 任何类型都可以存储在Object类型的变量中
SByte 1个字节 -128127(有符号)
Short 2个字节 -32,76832,767(有符号)
Single 4个字节 -3.4028235E + 38-1.401298E-45为负值;1.401298E-453.4028235E + 38为正值
String 取决于实施平台 0到约20亿个Unicode字符
UInteger 4个字节 04,294,967,295(无符号)
ULong 8个字节 018,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