2025-12-19 02:45:31

Visual Basic 数据类型

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

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

数据类型存储分配(大小)值范围Boolean取决于平台True 或 FalseByte1 个字节0 ~ 255(无符号)Char2 个字节0 ~ 65535(无符号)Date8 个字节0001 年 1 月 1 日 00:00:00(午夜)至 9999 年 12 月 31 日 11:59:59 PMDecimal16个字节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+308Integer4个字节-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,768 至 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(无符号)用户自定义取决于平台结构中的每个成员都有一个由其数据类型决定的范围,而与其他成员的范围无关UShort2 个字节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 SubEnd Module运行一下

结果如下:

VB 类型转换函数VB 提供了以下类型转换函数:

编号方法描述1CBool(expression)将表达式转换为布尔数据类型。2CByte(expression)将表达式转换为 Byte 数据类型。3CChar(expression)将表达式转换为 Char 数据类型。4CDate(expression)将表达式转换为日期数据类型5CDbl(expression)将表达式转换为 Double 数据类型。6CDec(expression)将表达式转换为十进制数据类型。7CInt(expression)将表达式转换为 Integer 数据类型。8CLng(expression)将表达式转换为 Long 数据类型。9CObj(expression)将表达式转换为 Object 对象类型。10CSByte(expression)将表达式转换为 SByte 数据类型。11CShort(expression)将表达式转换为 Short 数据类型。12CSng(expression)将表达式转换为 Single 数据类型。13CStr(expression)将表达式转换为字符串数据类型。14CUInt(expression)将表达式转换为 UInt 数据类型。15CULng(expression)将表达式转换为 ULng 数据类型。16CUShort(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 SubEnd Module运行一下

结果如下:

True-1True2022/5/3112

广州嫁女儿礼金多少 广州结婚礼金
没有鼠标怎么上下滑动?键盘快捷键让你自如切换页面