跳轉到內容

Visual Basic .NET/變數

來自華夏公益教科書,開放的書籍,構建開放的世界

在程式設計中,變數只是儲存資料的空間。變數有名稱和資料型別。在 Visual Basic .NET 中,變數使用 Dim(Dimension 的縮寫)語句宣告。以下是語法

 Dim varName As varType

varName 是變數的名稱。

varType 是變數的資料型別。型別包括字串、整數、雙精度浮點數、布林值等。

例如,要宣告一個名為 MyInt 的整數,請使用

 Dim MyInt As Integer


預設情況下,變數的大小寫不區分,因此如果像這樣宣告,myint 會被 IDE 自動轉換為 MyInt

另一方面,要讓程式忽略字串值的大小寫,應新增 Option Compare Text

Option Compare Text    ' By commenting this line the result becomes False
Module Module1
    Sub Main()
        Dim string1 As String = "a"
        Dim string2 As String = "A"
        MsgBox(string1 = string2)
    End Sub
End Module

資料型別

[編輯 | 編輯原始碼]

資料型別定義變數可以儲存的資料型別。一些變數儲存數字,另一些儲存名稱。內建的 VB.NET 類型別名及其等效的 .NET Framework 型別如下

VB 別名 .NET 型別 大小 範圍
SByte System.SByte 8 位(1 位元組) -128 到 127
Byte System.Byte 8 位(1 位元組) 0 到 255
Short System.Int16 16 位(2 位元組) -32,768 到 32,767
UShort System.UInt16 16 位(2 位元組) 0 到 65,535
Integer System.Int32 32 位(4 位元組) -2,147,483,648 到 2,147,483,647
UInteger System.UInt32 32 位(4 位元組) 0 到 4,294,967,295
Long System.Int64 64 位(8 位元組) -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
ULong System.UInt64 64 位(8 位元組) 0 到 18,446,744,073,709,551,615

浮點數

[編輯 | 編輯原始碼]
VB 別名 .NET 型別 大小 精度 範圍
Single System.Single 32 位(4 位元組) 7 位 1.5 x 10-45 到 3.4 x 1038
Double System.Double 64 位(8 位元組) 15-16 位 5.0 x 10-324 到 1.7 x 10308
Decimal System.Decimal 128 位(16 位元組) 28-29 位小數 1.0 x 10-28 到 7.9 x 1028

其他預定義型別

[編輯 | 編輯原始碼]
VB 別名 .NET 型別 大小(位) 範圍
Char System.Char 16 位(2 位元組) 範圍為 0 到 65,535 的一個 Unicode 符號。
Boolean System.Boolean 32 位(4 位元組) True 或 False
Object System.Object 32/64 位(4/8 位元組) 平臺相關(對物件的引用)。
Date System.DateTime 64 位(8 位元組) 公元 0001 年 1 月 1 日 上午 12:00:00 到公元 9999 年 12 月 31 日 下午 11:59:59
String System.String 80 + [16 * Length] 位(10 + [2 * Length] 位元組) 最大長度為 2,147,483,647 個字元的 Unicode 字串。

使用變數

[編輯 | 編輯原始碼]

值是變數中包含的資料。要將值賦給已宣告的變數,請使用等號。

字面量的字尾

[編輯 | 編輯原始碼]

整數字面量,例如 42 和 1000,預設型別為 Integer。字串和字元字面量,例如 "Hello World""À",預設型別為 String。要指定字面量的型別,請使用字尾。字尾附加在字面量之後,以 <literal><suffix> 的方式,字面量和字尾之間沒有任何空格。

對於字串和字元變數,請使用雙引號括住值

 strMyVariable = "The String"
 chrMyVariable = "À"

對於日期變數,請使用井號/磅號括住值,格式為 #<month>/<day>/<year> <hour>:<minute>:<second> <AM|PM>#

 dtMyVariable = #7/4/1776 12:01:50 PM#

對於所有其他變數,請刪除引號和井號/磅號

 bytMyVariable = 1
 sbytMyVariable = -2
 shrtMyVariable = 10S
 ushrtMyVariable = 10US
 intMyVariable = 100
 uintMyVariable = 100UI
 lngMyVariable = 1000L
 ulngMyVariable = 1000UL
 sngMyVariable = 1.234F
 dblMyVariable = 1.567R
 decMyVariable = 1234567.89D
 boolMyVariable = True
 objMyObject = New Object

初始值

[編輯 | 編輯原始碼]

要將變數賦予另一個變數的值,只需用儲存所需資料的變數的名稱替換等號右邊的值。

您還可以在宣告中將值賦予變數。

 Dim myVariable As String = "StringValue"

重要: Visual Basic 始終將右邊變數的值賦予左邊變數。左邊變數取右邊變數的值。右邊變數不會改變。

常量就像不會改變的變數。它們代替了您不想反覆鍵入的值。常量使用關鍵字“Const”宣告。它們的值在宣告中定義 - 它們也使用資料型別。以下是語法

 Const cnstMyConstant As String = "The very long string"

以下是一個示例

 Const cnstPi As Single = 3.14159265F

當您需要反覆鍵入相同的數字/字串等時,常量非常有用。例如,要從弧度轉換為角度,您可以鍵入 180/Pi 常量

 Const cnstRadToDeg As Single = 57,29579143

然後像這樣使用它

 Degrees = Radians / cnstRadToDeg

此常量在 Sin、Cos、Tan、Arctan 等函式中很有用。

華夏公益教科書