cv.cm

編碼 · 程式語言

Visual Basic .NET 入門教學

閱讀約 10 分鐘

簡答

Visual Basic .NET(VB.NET)是 .NET 平台上的物件導向、靜態型別語言,關鍵字像英文,例如 Dim、If ... Then ... End If 與 For ... Next。它不分大小寫,且使用與 C# 相同的 .NET 函式庫。用 dotnet new console -lang VB 建立專案,再以 dotnet run 執行。VBA 是 Microsoft Office 內建的巨集語言,是語法相近的較早期同源語言。

Visual Basic .NET 是什麼、誰在使用

VB.NET 是傳統 Visual Basic(VB6)在 .NET 上的後繼者。它與 C# 編譯成相同的中介語言,因此兩種語言可以共用函式庫,甚至混用在同一個方案中。它主要用於 Windows 桌面應用程式(Windows Forms、WPF)、企業內部工具、ASP.NET 網頁應用程式,以及既有 VB 程式碼的維護。

Microsoft 持續在現代 .NET 上支援 Visual Basic,但已表示不打算再為這門語言加入重大的新功能;新的 .NET 功能通常會先出現在 C# 中。對於沒有既有 VB 程式碼的新專案,C# 是較常見的選擇,不過 VB.NET 對於學習程式設計與商業軟體來說,仍然是易讀的語言。

安裝並執行 Hello, world

安裝 .NET SDK(Windows、macOS 或 Linux)。Windows 上的 Visual Studio 也包含 Visual Basic 範本與視覺化的表單設計工具。

dotnet new console -lang VB -o HelloVB
cd HelloVB
dotnet run
# Hello, world!
' Program.vb
Imports System

Module Program
    Sub Main(args As String())
        Console.WriteLine("Hello, world!")
    End Sub
End Module

Module 存放共用的程序,Sub Main 是進入點。註解以單引號 ' 開頭。每個陳述式通常在行尾結束;沒有分號或大括號。

變數與基本型別

用 Dim name As Type 宣告變數。在 Option Infer On(預設值)下,指派值時可以省略型別。請在每個檔案的最上方加上 Option Strict On(或在專案中設定),讓有風險的隱含轉換變成編譯錯誤。

型別範例說明
IntegerDim n As Integer = 4232 位元;Long 是 64 位元
DoubleDim x As Double = 3.1464 位元浮點數
DecimalDim price As Decimal = 19.99D精確的十進位數,適合處理金額
StringDim s As String = "hello"用 & 連接字串
CharDim c As Char = "a"c單一字元,後綴為 c
BooleanDim ok As Boolean = TrueTrue / False
DateDim d As Date = #2026-01-15#日期與時間值
Option Strict On
Module Types
    Sub Main()
        Dim name As String = "Ana"
        Dim age = 30                  ' inferred as Integer
        Console.WriteLine(name & " is " & age.ToString())
        Console.WriteLine($"Next year {name} is {age + 1}")
        Console.WriteLine(7 / 2)      ' 3.5 (always Double)
        Console.WriteLine(7 \ 2)      ' 3 (integer division)
        Console.WriteLine(7 Mod 2)    ' 1
    End Sub
End Module

流程控制

Dim score = 85
If score >= 90 Then
    Console.WriteLine("A")
ElseIf score >= 80 Then
    Console.WriteLine("B")
Else
    Console.WriteLine("C")
End If

Select Case score
    Case 90 To 100
        Console.WriteLine("excellent")
    Case 80 To 89
        Console.WriteLine("good")
    Case Else
        Console.WriteLine("keep going")
End Select

For i = 1 To 3
    Console.WriteLine($"{i} squared is {i * i}")
Next

Dim n = 3
Do While n > 0
    n -= 1
Loop

這些陳述式要寫在 Sub 內。For i = 1 To 3 包含兩端;加上 Step -1 可以倒數。For Each item In collection 會走訪任何集合。使用 Exit For 或 Exit Do 可提早離開迴圈。撰寫條件時,建議用 AndAlso 與 OrElse:一旦結果確定就會停止求值,而 And 與 Or 則永遠兩邊都會求值。

Sub、函式與 lambda

Sub 不回傳值;Function 則會。參數預設以 ByVal(複本)傳遞;ByRef 則讓程序能更改呼叫者的變數。參數可以是帶預設值的 Optional。

Function Add(a As Integer, b As Integer) As Integer
    Return a + b
End Function

Sub Greet(Optional name As String = "world")
    Console.WriteLine($"Hello, {name}!")
End Sub

Sub Increment(ByRef value As Integer)
    value += 1
End Sub

' Inside Sub Main:
Dim x = 5
Increment(x)                                  ' x is now 6
Greet()                                       ' Hello, world!
Dim square = Function(n As Integer) n * n     ' lambda
Console.WriteLine(square(Add(1, 2)))          ' 9

陣列、List 與 Dictionary

Dim nums() As Integer = {3, 1, 2}
Dim five(4) As Integer            ' 5 elements, indexes 0 to 4
Array.Sort(nums)
Console.WriteLine(String.Join(", ", nums))   ' 1, 2, 3

Dim names As New List(Of String) From {"Ana", "Ben"}
names.Add("Cleo")
Console.WriteLine(names.Count)               ' 3

Dim ages As New Dictionary(Of String, Integer)
ages("Ana") = 30
ages("Ben") = 17
For Each pair In ages
    Console.WriteLine($"{pair.Key}: {pair.Value}")
Next

陣列與 list 以括號從 0 開始索引:nums(0)。泛型型別使用 Of:List(Of String) 是 C# 的 List<string> 在 VB 中的寫法。請注意,Dim five(4) 宣告的是上界而不是長度,所以這個陣列有五個元素。

招牌特色:類別、屬性與 LINQ 查詢

VB.NET 完全是物件導向的:類別具有屬性、方法、建構函式(Sub New)、繼承(Inherits)、介面(Implements)與事件(Event、RaiseEvent、Handles)。LINQ 查詢可以用易讀的查詢語法撰寫。

Public Class Person
    Public Property Name As String
    Public Property Age As Integer

    Public Sub New(name As String, age As Integer)
        Me.Name = name
        Me.Age = age
    End Sub
End Class

' Inside Sub Main:
Dim people As New List(Of Person) From {
    New Person("Ana", 30),
    New Person("Ben", 17)
}
Dim adults = From p In people
             Where p.Age >= 18
             Select p.Name
For Each adultName In adults
    Console.WriteLine(adultName)   ' Ana
Next

由於 VB 不分大小寫,Name 與 name 是同一個識別字;在建構函式內,Me.Name 指的是屬性,name 指的是參數。其他 VB 特有的便利功能還有 With ... End With 區塊、XML 字面值,以及處理常見工作的 My 命名空間。

錯誤處理

VB.NET 使用結構化例外處理:Try、Catch、Finally 與 Throw。Catch 可以包含 When 篩選條件。VB6 與 VBA 較舊的 On Error GoTo 風格仍可編譯,但不應用於新的 .NET 程式碼。

Function ParseAge(s As String) As Integer
    Dim n As Integer
    If Not Integer.TryParse(s, n) Then
        Throw New FormatException($"Not a number: {s}")
    End If
    If n < 0 Then Throw New ArgumentOutOfRangeException(NameOf(s))
    Return n
End Function

' Inside Sub Main:
Try
    Console.WriteLine(ParseAge("abc"))
Catch ex As FormatException
    Console.WriteLine("Error: " & ex.Message)   ' Error: Not a number: abc
Finally
    Console.WriteLine("done")
End Try

完整的小程式:字詞頻率

Imports System
Imports System.Collections.Generic
Imports System.Linq

Module WordCount
    Sub Main()
        Dim text = "the cat and the hat and the bat"
        Dim counts As New Dictionary(Of String, Integer)

        For Each word In text.Split(" "c)
            If counts.ContainsKey(word) Then
                counts(word) += 1
            Else
                counts(word) = 1
            End If
        Next

        Dim top = counts.OrderByDescending(Function(kv) kv.Value).Take(2)
        For Each kv In top
            Console.WriteLine($"{kv.Key} {kv.Value}")
        Next
    End Sub
End Module
' the 3
' and 2

VBA:Microsoft Office 內的 Visual Basic

Visual Basic for Applications(VBA)是內建於 Excel、Word 與 Access 等 Microsoft Office 應用程式的巨集語言。它以傳統 Visual Basic 為基礎,而不是 .NET:它在 Office 應用程式內執行,使用 Office 物件模型(活頁簿、儲存格範圍、文件),並在 VBA 編輯器中編輯(Windows 上按 Alt+F11)。Dim、If、For 與 Sub 的基本語法相同,因此懂 VB.NET 對學 VBA 很有幫助。

' VBA in Excel: write the squares of 1 to 5 into column A
Sub FillSquares()
    Dim i As Long
    For i = 1 To 5
        Cells(i, 1).Value = i * i
    Next i
    MsgBox "Done"
End Sub

接下來可以學什麼

  • Microsoft Learn:Visual Basic 文件,包括入門指南與語言參考。
  • 學習 .NET 基底類別庫(集合、檔案、LINQ);它與 C# 共用。
  • 若要開發桌面應用程式,可以試試 Visual Studio 中的 Windows Forms 或 WPF 範本。
  • 若要自動化 Office,請閱讀 Microsoft Learn 上你所使用應用程式的 VBA 參考。

常見問題

Visual Basic .NET 已經過時了嗎?

沒有。它在目前的 .NET 版本與 Visual Studio 中都受到支援,但 Microsoft 不打算加入重大的新語言功能,所以大多數新的 .NET 專案會選擇 C#。

VB.NET 與 VBA 有什麼差別?

VB.NET 是完整的 .NET 語言,用來建構獨立的應用程式。VBA 是以傳統 Visual Basic 為基礎的巨集語言,在 Excel 等 Office 應用程式內執行;兩者語法相似,但執行環境與函式庫不同。

該學 VB.NET 還是 C#?

如果你要維護 VB 程式碼,或想要非常易讀的語法,VB.NET 就可以。對於新專案、就業機會與最新的 .NET 功能,C# 通常是更好的投資,而你學到的函式庫知識可以在兩者之間通用。

Visual Basic 分大小寫嗎?

不分。Dim Total、total 與 TOTAL 都指向同一個變數,編輯器通常會自動幫你統一大小寫。

全部編碼教學