Visual Basic 中的資料類型Data Types in Visual Basic

程式設計元素的「資料類型」是指其可以保留何種資料,以及儲存該資料的方式。The data type of a programming element refers to what kind of data it can hold and how it stores that data. 資料類型會應用於所有能儲存在電腦記憶體中的值,或是所有參與運算式評估的值。Data types apply to all values that can be stored in computer memory or participate in the evaluation of an expression. 每個變數、常值、常數、列舉、屬性、程序參數、程序引數和程序傳回值都具有資料類型。Every variable, literal, constant, enumeration, property, procedure parameter, procedure argument, and procedure return value has a data type.

已宣告的資料類型Declared Data Types

您可以使用宣告陳述式來定義程式設計元素,並以 As 子句來指定其資料類型。You define a programming element with a declaration statement, and you specify its data type with the As clause. 下表顯示用來宣告各種元素的陳述式。The following table shows the statements you use to declare various elements.

程式設計元素Programming element 資料類型宣告Data type declaration
變數Variable Dim 陳述式In a Dim Statement

Dim amount As DoubleDim amount As Double

Static yourName As StringStatic yourName As String

Public billsPaid As Decimal = 0Public billsPaid As Decimal = 0
LiteralLiteral 藉由常值類型字元;請參閱類型字元中的<常值類型字元>With a literal type character; see "Literal Type Characters" in Type Characters

Dim searchChar As Char = "." CDim searchChar As Char = "." C
常數Constant Const 陳述式In a Const Statement

Const modulus As Single = 4.17825FConst modulus As Single = 4.17825F
列舉Enumeration Enum 陳述式In an Enum Statement

Public Enum colorsPublic Enum colors
屬性Property Property 陳述式In a Property Statement

Property region() As StringProperty region() As String
程序參數Procedure parameter Sub 陳述式Function 陳述式Operator 陳述式In a Sub Statement, Function Statement, or Operator Statement

Sub addSale(ByVal amount As Double)Sub addSale(ByVal amount As Double)
程序引數Procedure argument 在呼叫程式碼中;每個引數都是已宣告的程式設計元素,或是包含已宣告元素的運算式In the calling code; each argument is a programming element that has already been declared, or an expression containing declared elements

subString = Left( inputString , 5 )subString = Left( inputString , 5 )
程序傳回值Procedure return value Function 陳述式Operator 陳述式In a Function Statement or Operator Statement

Function convert(ByVal b As Byte) As StringFunction convert(ByVal b As Byte) As String

如需 Visual Basic 資料類型清單,請參閱資料類型For a list of Visual Basic data types, see Data Types.

另請參閱See also