VB.NET 建構於 .NET Framework / .NET Core 之上,採用 **Common Language Runtime (CLR)**、強型別、物件導向、統一型別系統 (CTS)。VB6 代碼**無法直接編譯**,需人工重寫或使用升級精靈輔助 (僅能處理 30-50%)。
| VB6 / VBA | VB.NET | 說明 |
|---|---|---|
| `Sub Form_Load()` | `Sub New()` (建構函式) / `Form_Load` 事件 | 類別初始化統一用 `Sub New`,可多載、可帶參數 |
| `Property Let/Set/Get` | `Property ... Get / Set` (單一區塊) | 移除 `Let`/`Set` 關鍵字,統一用 `Value` 參數 |
| `Currency` | `Decimal` | `Currency` 移除;`Decimal` 為 128-bit 高精度型別,適合金融 |
| `Variant` | `Object` | `Object` 繼承自 `System.Object`,具備 `GetType()`、`ToString()` 等 |
| `Debug.Print` | `Debug.Write` / `Debug.WriteLine` | 輸出至「輸出」視窗,需 `Imports System.Diagnostics` |
| `Wend` | `End While` | `While...End While` 取代 `Do While...Loop` / `While...Wend` |
| `Integer` (16-bit) | `Short` (16-bit) | **位元寬度位移**:VB6 `Integer`=16-bit → VB.NET `Short`;VB6 `Long`=32-bit → VB.NET `Integer`;新增 `Long`=64-bit |
| `Long` (32-bit) | `Integer` (32-bit) | |
| (無) | `Long` (64-bit) | |
| `Set obj = Nothing` | `obj = Nothing` | 移除 `Set` 關鍵字,參考型別直接指派 |
| `Default Property` (隱含) | **移除** | 必須明確寫屬性名:`TextBox1.Text = "..."` 而非 `TextBox1 = "..."` |
| `On Error GoTo / Resume Next` | `Try...Catch...Finally` / `Throw` | 結構化例外處理,支援多 `Catch`、例外篩選器 `When` |
| `ReDim arr(10)` (隱含宣告) | `Dim arr(10) As Type` / `ReDim` 僅能改變大小 | 宣告與重設大小分離 |
| `Option Explicit` | `Option Strict On` / `Option Explicit On` | `Strict On` 禁止隱含縮小轉換 (如 `Double → Integer`),強制 `CInt`/`Convert.ToInt32` |
| VB6 | 位元 | VB.NET | 位元 | .NET 別名 |
|---|---|---|---|---|
| Integer | 16 | Short | 16 | System.Int16 |
| Long | 32 | Integer | 32 | System.Int32 |
| (無) | - | Long | 64 | System.Int64 |
| Byte | 8 | Byte | 8 | System.Byte |
API 呼叫注意:Windows API `HWND`、`LPARAM` 等在 64-bit 進程為 64-bit,需用 `IntPtr` 或 `Long` (VB.NET) 接收,不可用 `Integer`。
| 關鍵字 | 用途 | 範例 |
|---|---|---|
Inherits |
繼承基類 | `Inherits System.Windows.Forms.Form` |
MyBase |
存取基類成員 | `MyBase.Load(e)` |
Shared |
靜態成員 (類別層級) | `Public Shared ReadOnly PI As Double = 3.14159` |
Try / Catch / Finally / Throw |
結構化例外處理 | 見下方範例 |
ReadOnly / WriteOnly |
單向屬性 | `Public ReadOnly Property Name As String` |
Char |
單一 Unicode 字元 | `Dim c As Char = "A"c` |
Imports |
匯入命名空間 | `Imports System.IO` |
Namespace |
宣告命名空間 | `Namespace MyApp.Core` |
Overloads |
方法多載 | `Overloads Sub Print(s As String)` / `Overloads Sub Print(i As Integer)` |
Overrides |
覆寫虛擬方法 | `Overrides Function ToString() As String` |
Overridable |
允許被覆寫 (virtual) | `Overridable Sub Calculate()` |
MustOverride |
抽象方法 (MustOverride = abstract) | `MustOverride Sub Save()` |
Protected / Friend / Protected Friend |
存取修飾詞 | `Protected`=衍生類可見;`Friend`=組件內可見 |
' VB.NET 標準屬性模板
Private _name As String
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
' 自動實作屬性 (VB 14 / VS 2015+)
Public Property Age As Integer ' 編譯器自動產生後端欄位
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As SqlException When ex.Number = 2627 ' 例外篩選器 (VB 14+)
LogError("主鍵重複", ex)
Throw New BusinessException("資料已存在", ex)
Catch ex As Exception
LogError("資料庫錯誤", ex)
Throw
Finally
If conn.State = ConnectionState.Open Then conn.Close()
End Try
VB.NET 雖保留 Basic 風格關鍵字,但語意模型已全面對齊 C#/.NET 生態。掌握 **型別位元寬度變更、預設 ByVal、移除 Default Property/Set、結構化例外、命名空間/組件架構** 五大核心差異,即可避開 90% 遷移陷阱。