Loading...
Loading...
Compare original and translation side by side
' Always enable strict type checking
Option Strict On
Option Explicit On
Option Infer On
' Modern class definition
Public Class Customer
Public Property Id As Integer
Public Property Name As String
Public Property Email As String
' Constructor
Public Sub New(id As Integer, name As String, email As String)
Me.Id = id
Me.Name = name
Me.Email = email
End Sub
End Class
' Modern async method
Public Async Function GetCustomerAsync(id As Integer) As Task(Of Customer)
Dim result = Await database.QueryAsync(Of Customer)(
"SELECT * FROM Customers WHERE Id = @id",
New With {.id = id}
)
Return result.FirstOrDefault()
End Function
' LINQ query
Dim activeCustomers = From c In customers
Where c.IsActive AndAlso c.Balance > 0
Order By c.Name
Select c' 始终启用严格类型检查
Option Strict On
Option Explicit On
Option Infer On
' 现代类定义
Public Class Customer
Public Property Id As Integer
Public Property Name As String
Public Property Email As String
' 构造函数
Public Sub New(id As Integer, name As String, email As String)
Me.Id = id
Me.Name = name
Me.Email = email
End Sub
End Class
' 现代异步方法
Public Async Function GetCustomerAsync(id As Integer) As Task(Of Customer)
Dim result = Await database.QueryAsync(Of Customer)(
"SELECT * FROM Customers WHERE Id = @id",
New With {.id = id}
)
Return result.FirstOrDefault()
End Function
' LINQ查询
Dim activeCustomers = From c In customers
Where c.IsActive AndAlso c.Balance > 0
Order By c.Name
Select c' ALWAYS at top of every file
Option Strict On
Option Explicit On
Option Infer On
' Why:
' - Option Strict: Prevents implicit narrowing conversions
' - Option Explicit: Requires variable declaration
' - Option Infer: Enables type inference (but still type-safe)' 务必放在每个文件的顶部
Option Strict On
Option Explicit On
Option Infer On
' 原因:
' - Option Strict:防止隐式窄化转换
' - Option Explicit:要求变量必须声明
' - Option Infer:启用类型推断(但仍保持类型安全)' ✅ Good: Explicit when clarity helps
Dim customerName As String = GetCustomerName(id)
Dim count As Integer = GetCount()
' ✅ Good: Inference when type is obvious
Dim customer = New Customer(1, "John", "john@example.com")
Dim items = New List(Of String) From {"a", "b", "c"}
' ❌ Bad: Dim without type and no inference
Dim data = GetData() ' What type is data?
' ✅ Better: Be explicit
Dim data As DataTable = GetData()' ✅ 推荐:需要清晰性时使用显式类型
Dim customerName As String = GetCustomerName(id)
Dim count As Integer = GetCount()
' ✅ 推荐:类型明显时使用推断
Dim customer = New Customer(1, "John", "john@example.com")
Dim items = New List(Of String) From {"a", "b", "c"}
' ❌ 不推荐:未指定类型且无法推断的Dim声明
Dim data = GetData() ' data的类型是什么?
' ✅ 更好的方式:显式声明
Dim data As DataTable = GetData()' Modern nullable syntax (VB 15.5+)
Dim middleName As String? = Nothing
Dim age As Integer? = Nothing
' Null-conditional operator
Dim length As Integer? = middleName?.Length
' Null-coalescing operator
Dim displayName As String = middleName If Nothing
' Check for null
If age IsNot Nothing Then
Console.WriteLine($"Age: {age.Value}")
End If
' GetValueOrDefault
Dim years As Integer = age.GetValueOrDefault(0)' 现代可空语法(VB 15.5+)
Dim middleName As String? = Nothing
Dim age As Integer? = Nothing
' 空条件运算符
Dim length As Integer? = middleName?.Length
' 空合并运算符
Dim displayName As String = middleName If Nothing
' 检查空值
If age IsNot Nothing Then
Console.WriteLine($"Age: {age.Value}")
End If
' 获取默认值
Dim years As Integer = age.GetValueOrDefault(0)' ✅ Modern (VB 14+)
Dim message = $"Customer {customer.Name} has balance {customer.Balance:C}"
' ❌ Old style (avoid)
Dim message = String.Format("Customer {0} has balance {1:C}", customer.Name, customer.Balance)
' ❌ Concatenation (avoid for complex strings)
Dim message = "Customer " & customer.Name & " has balance " & customer.Balance.ToString("C")' ✅ 现代写法(VB 14+)
Dim message = $"Customer {customer.Name} has balance {customer.Balance:C}"
' ❌ 旧写法(避免使用)
Dim message = String.Format("Customer {0} has balance {1:C}", customer.Name, customer.Balance)
' ❌ 字符串拼接(复杂字符串时避免使用)
Dim message = "Customer " & customer.Name & " has balance " & customer.Balance.ToString("C")' Single-line lambda
Dim isAdult = Function(age As Integer) age >= 18
' Multi-line lambda
Dim calculateTax = Function(amount As Decimal) As Decimal
Dim rate = If(amount > 10000, 0.2D, 0.15D)
Return amount * rate
End Function
' Lambda in LINQ
Dim adults = customers.Where(Function(c) c.Age >= 18).ToList()
' Action lambda (Sub)
items.ForEach(Sub(item) Console.WriteLine(item))' 单行Lambda
Dim isAdult = Function(age As Integer) age >= 18
' 多行Lambda
Dim calculateTax = Function(amount As Decimal) As Decimal
Dim rate = If(amount > 10000, 0.2D, 0.15D)
Return amount * rate
End Function
' LINQ中的Lambda
Dim adults = customers.Where(Function(c) c.Age >= 18).ToList()
' Action类型Lambda(Sub)
items.ForEach(Sub(item) Console.WriteLine(item))' List initialization
Dim numbers = New List(Of Integer) From {1, 2, 3, 4, 5}
' Dictionary initialization
Dim config = New Dictionary(Of String, String) From {
{"ConnectionString", "Server=..."},
{"Timeout", "30"},
{"MaxRetries", "3"}
}
' Array initialization
Dim names As String() = {"Alice", "Bob", "Charlie"}' 列表初始化
Dim numbers = New List(Of Integer) From {1, 2, 3, 4, 5}
' 字典初始化
Dim config = New Dictionary(Of String, String) From {
{"ConnectionString", "Server=..."},
{"Timeout", "30"},
{"MaxRetries", "3"}
}
' 数组初始化
Dim names As String() = {"Alice", "Bob", "Charlie"}' Named tuples (VB 15.3+)
Function GetCustomerInfo(id As Integer) As (Name As String, Email As String)
Return ("John Doe", "john@example.com")
End Function
' Usage
Dim info = GetCustomerInfo(1)
Console.WriteLine($"{info.Name}: {info.Email}")
' Deconstruction
Dim (customerName, customerEmail) = GetCustomerInfo(1)' 命名元组(VB 15.3+)
Function GetCustomerInfo(id As Integer) As (Name As String, Email As String)
Return ("John Doe", "john@example.com")
End Function
' 使用方式
Dim info = GetCustomerInfo(1)
Console.WriteLine($"{info.Name}: {info.Email}")
' 解构
Dim (customerName, customerEmail) = GetCustomerInfo(1)' Query syntax (more readable for complex queries)
Dim query = From c In customers
Where c.IsActive
Order By c.Name
Select New With {
.Name = c.Name,
.Email = c.Email
}
' Method syntax (more flexible)
Dim query = customers _
.Where(Function(c) c.IsActive) _
.OrderBy(Function(c) c.Name) _
.Select(Function(c) New With {
.Name = c.Name,
.Email = c.Email
})' 查询语法(复杂查询时可读性更高)
Dim query = From c In customers
Where c.IsActive
Order By c.Name
Select New With {
.Name = c.Name,
.Email = c.Email
}
' 方法语法(灵活性更高)
Dim query = customers _
.Where(Function(c) c.IsActive) _
.OrderBy(Function(c) c.Name) _
.Select(Function(c) New With {
.Name = c.Name,
.Email = c.Email
})' Filtering
Dim activeCustomers = customers.Where(Function(c) c.IsActive).ToList()
' Projection
Dim names = customers.Select(Function(c) c.Name).ToList()
' Ordering
Dim sorted = customers.OrderBy(Function(c) c.Name).ThenBy(Function(c) c.Id)
' Grouping
Dim grouped = From c In customers
Group c By c.City Into Group
Select City, Customers = Group
' Aggregation
Dim total = orders.Sum(Function(o) o.Amount)
Dim average = orders.Average(Function(o) o.Amount)
Dim count = customers.Count(Function(c) c.IsActive)
' First/Single
Dim first = customers.FirstOrDefault(Function(c) c.Id = 1)
Dim single = customers.SingleOrDefault(Function(c) c.Email = email)
' Any/All
Dim hasActive = customers.Any(Function(c) c.IsActive)
Dim allActive = customers.All(Function(c) c.IsActive)' 过滤
Dim activeCustomers = customers.Where(Function(c) c.IsActive).ToList()
' 投影
Dim names = customers.Select(Function(c) c.Name).ToList()
' 排序
Dim sorted = customers.OrderBy(Function(c) c.Name).ThenBy(Function(c) c.Id)
' 分组
Dim grouped = From c In customers
Group c By c.City Into Group
Select City, Customers = Group
' 聚合
Dim total = orders.Sum(Function(o) o.Amount)
Dim average = orders.Average(Function(o) o.Amount)
Dim count = customers.Count(Function(c) c.IsActive)
' 获取首个/单个元素
Dim first = customers.FirstOrDefault(Function(c) c.Id = 1)
Dim single = customers.SingleOrDefault(Function(c) c.Email = email)
' 存在性检查
Dim hasActive = customers.Any(Function(c) c.IsActive)
Dim allActive = customers.All(Function(c) c.IsActive)' Return Task(Of T) for async methods with return value
Public Async Function GetDataAsync() As Task(Of String)
Dim result = Await httpClient.GetStringAsync(url)
Return result
End Function
' Return Task for async methods without return value
Public Async Function SaveDataAsync(data As String) As Task
Await File.WriteAllTextAsync(filePath, data)
End Function
' Async Sub only for event handlers
Private Async Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
Await ProcessDataAsync()
End Sub' 有返回值的异步方法返回Task(Of T)
Public Async Function GetDataAsync() As Task(Of String)
Dim result = Await httpClient.GetStringAsync(url)
Return result
End Function
' 无返回值的异步方法返回Task
Public Async Function SaveDataAsync(data As String) As Task
Await File.WriteAllTextAsync(filePath, data)
End Function
' Async Sub仅用于事件处理程序
Private Async Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
Await ProcessDataAsync()
End Sub' ✅ Good: Await all the way
Public Async Function ProcessOrderAsync(orderId As Integer) As Task(Of Boolean)
Dim order = Await GetOrderAsync(orderId)
Await ValidateOrderAsync(order)
Await SaveOrderAsync(order)
Return True
End Function
' ❌ Bad: Blocking on async (deadlock risk)
Public Function ProcessOrder(orderId As Integer) As Boolean
Dim order = GetOrderAsync(orderId).Result ' Can deadlock!
Return True
End Function
' ✅ Good: ConfigureAwait(False) in libraries
Public Async Function GetDataAsync() As Task(Of String)
Dim result = Await httpClient.GetStringAsync(url).ConfigureAwait(False)
Return result
End Function
' Parallel async operations
Dim tasks = New List(Of Task(Of Customer)) From {
GetCustomerAsync(1),
GetCustomerAsync(2),
GetCustomerAsync(3)
}
Dim customers = Await Task.WhenAll(tasks)' ✅ 推荐:全程使用Await
Public Async Function ProcessOrderAsync(orderId As Integer) As Task(Of Boolean)
Dim order = Await GetOrderAsync(orderId)
Await ValidateOrderAsync(order)
Await SaveOrderAsync(order)
Return True
End Function
' ❌ 不推荐:阻塞异步操作(存在死锁风险)
Public Function ProcessOrder(orderId As Integer) As Boolean
Dim order = GetOrderAsync(orderId).Result ' 可能导致死锁!
Return True
End Function
' ✅ 推荐:类库中使用ConfigureAwait(False)
Public Async Function GetDataAsync() As Task(Of String)
Dim result = Await httpClient.GetStringAsync(url).ConfigureAwait(False)
Return result
End Function
' 并行异步操作
Dim tasks = New List(Of Task(Of Customer)) From {
GetCustomerAsync(1),
GetCustomerAsync(2),
GetCustomerAsync(3)
}
Dim customers = Await Task.WhenAll(tasks)' Modern structured exception handling
Try
Dim result = Await ProcessDataAsync()
Return result
Catch ex As ArgumentNullException
' Handle specific exception
logger.LogError(ex, "Null argument in ProcessData")
Throw
Catch ex As HttpRequestException
' Handle another specific exception
logger.LogError(ex, "HTTP request failed")
Return Nothing
Catch ex As Exception
' Catch-all (use sparingly)
logger.LogError(ex, "Unexpected error")
Throw
Finally
' Cleanup (always runs)
connection?.Dispose()
End Try' 现代结构化异常处理
Try
Dim result = Await ProcessDataAsync()
Return result
Catch ex As ArgumentNullException
' 处理特定异常
logger.LogError(ex, "ProcessData中传入空参数")
Throw
Catch ex As HttpRequestException
' 处理另一种特定异常
logger.LogError(ex, "HTTP请求失败")
Return Nothing
Catch ex As Exception
' 捕获所有异常(谨慎使用)
logger.LogError(ex, "发生意外错误")
Throw
Finally
' 清理操作(始终会执行)
connection?.Dispose()
End Try' VB 14+ exception filters
Try
ProcessData()
Catch ex As IOException When ex.Message.Contains("file not found")
' Handle specific IO error
CreateDefaultFile()
Catch ex As IOException When ex.Message.Contains("access denied")
' Handle different IO error
RequestPermissions()
End Try' VB 14+异常筛选器
Try
ProcessData()
Catch ex As IOException When ex.Message.Contains("file not found")
' 处理特定IO错误
CreateDefaultFile()
Catch ex As IOException When ex.Message.Contains("access denied")
' 处理另一种IO错误
RequestPermissions()
End Try' Define custom exception
Public Class CustomerNotFoundException
Inherits Exception
Public Property CustomerId As Integer
Public Sub New(customerId As Integer)
MyBase.New($"Customer {customerId} not found")
Me.CustomerId = customerId
End Sub
Public Sub New(customerId As Integer, innerException As Exception)
MyBase.New($"Customer {customerId} not found", innerException)
Me.CustomerId = customerId
End Sub
End Class
' Usage
If customer Is Nothing Then
Throw New CustomerNotFoundException(customerId)
End If' 定义自定义异常
Public Class CustomerNotFoundException
Inherits Exception
Public Property CustomerId As Integer
Public Sub New(customerId As Integer)
MyBase.New($"Customer {customerId} not found")
Me.CustomerId = customerId
End Sub
Public Sub New(customerId As Integer, innerException As Exception)
MyBase.New($"Customer {customerId} not found", innerException)
Me.CustomerId = customerId
End Sub
End Class
' 使用方式
If customer Is Nothing Then
Throw New CustomerNotFoundException(customerId)
End If' Simple auto property
Public Property Name As String
' With default value
Public Property IsActive As Boolean = True
' Read-only auto property (VB 14+)
Public ReadOnly Property Id As Integer
' Can only be set in constructor
Public Sub New(id As Integer)
Me.Id = id
End Sub' 简单自动属性
Public Property Name As String
' 带默认值的自动属性
Public Property IsActive As Boolean = True
' 只读自动属性(VB 14+)
Public ReadOnly Property Id As Integer
' 只能在构造函数中设置
Public Sub New(id As Integer)
Me.Id = id
End SubPublic Class Customer
Public Property FirstName As String
Public Property LastName As String
' Computed property
Public ReadOnly Property FullName As String
Get
Return $"{FirstName} {LastName}"
End Get
End Property
' Property with validation
Private _age As Integer
Public Property Age As Integer
Get
Return _age
End Get
Set(value As Integer)
If value < 0 Or value > 150 Then
Throw New ArgumentOutOfRangeException(NameOf(Age))
End If
_age = value
End Set
End Property
End ClassPublic Class Customer
Public Property FirstName As String
Public Property LastName As String
' 计算属性
Public ReadOnly Property FullName As String
Get
Return $"{FirstName} {LastName}"
End Get
End Property
' 带验证的属性
Private _age As Integer
Public Property Age As Integer
Get
Return _age
End Get
Set(value As Integer)
If value < 0 Or value > 150 Then
Throw New ArgumentOutOfRangeException(NameOf(Age))
End If
_age = value
End Set
End Property
End ClassPublic Interface IRepository(Of T)
Function GetByIdAsync(id As Integer) As Task(Of T)
Function GetAllAsync() As Task(Of IEnumerable(Of T))
Function AddAsync(entity As T) As Task
Function UpdateAsync(entity As T) As Task
Function DeleteAsync(id As Integer) As Task
End Interface
' Implementation
Public Class CustomerRepository
Implements IRepository(Of Customer)
Public Async Function GetByIdAsync(id As Integer) As Task(Of Customer) _
Implements IRepository(Of Customer).GetByIdAsync
Return Await dbContext.Customers.FindAsync(id)
End Function
' ... other implementations
End ClassPublic Interface IRepository(Of T)
Function GetByIdAsync(id As Integer) As Task(Of T)
Function GetAllAsync() As Task(Of IEnumerable(Of T))
Function AddAsync(entity As T) As Task
Function UpdateAsync(entity As T) As Task
Function DeleteAsync(id As Integer) As Task
End Interface
' 实现类
Public Class CustomerRepository
Implements IRepository(Of Customer)
Public Async Function GetByIdAsync(id As Integer) As Task(Of Customer) _
Implements IRepository(Of Customer).GetByIdAsync
Return Await dbContext.Customers.FindAsync(id)
End Function
' ... 其他实现
End Class' Base class
Public MustInherit Class BaseEntity
Public Property Id As Integer
Public Property CreatedAt As DateTime
Public Property UpdatedAt As DateTime
Public MustOverride Sub Validate()
End Class
' Derived class
Public Class Customer
Inherits BaseEntity
Public Property Name As String
Public Property Email As String
Public Overrides Sub Validate()
If String.IsNullOrWhiteSpace(Name) Then
Throw New ValidationException("Name is required")
End If
If Not Email.Contains("@") Then
Throw New ValidationException("Invalid email format")
End If
End Sub
End Class' 基类
Public MustInherit Class BaseEntity
Public Property Id As Integer
Public Property CreatedAt As DateTime
Public Property UpdatedAt As DateTime
Public MustOverride Sub Validate()
End Class
' 派生类
Public Class Customer
Inherits BaseEntity
Public Property Name As String
Public Property Email As String
Public Overrides Sub Validate()
If String.IsNullOrWhiteSpace(Name) Then
Throw New ValidationException("名称为必填项")
End If
If Not Email.Contains("@") Then
Throw New ValidationException("邮箱格式无效")
End If
End Sub
End Class' Always use Option Strict On
Option Strict On
Option Explicit On
Option Infer On
' Use modern syntax (LINQ, async/await, string interpolation)
Dim activeCustomers = Await customers.Where(Function(c) c.IsActive).ToListAsync()
Dim message = $"Found {activeCustomers.Count} active customers"
' Use meaningful names
Dim customerEmailAddress As String
Dim isCustomerActive As Boolean
' Use async/await for I/O operations
Public Async Function LoadDataAsync() As Task(Of Data)
' Use IDisposable pattern
Using connection = New SqlConnection(connectionString)
' Use connection
End Using
' Use XML comments for public APIs
''' <summary>
''' Gets a customer by their unique identifier.
''' </summary>
''' <param name="customerId">The customer ID to search for.</param>
''' <returns>The customer if found, Nothing otherwise.</returns>
Public Async Function GetCustomerAsync(customerId As Integer) As Task(Of Customer)' 始终启用Option Strict On
Option Strict On
Option Explicit On
Option Infer On
' 使用现代语法(LINQ、async/await、字符串插值)
Dim activeCustomers = Await customers.Where(Function(c) c.IsActive).ToListAsync()
Dim message = $"找到 {activeCustomers.Count} 个活跃客户"
' 使用有意义的命名
Dim customerEmailAddress As String
Dim isCustomerActive As Boolean
' I/O操作使用async/await
Public Async Function LoadDataAsync() As Task(Of Data)
' 使用IDisposable模式
Using connection = New SqlConnection(connectionString)
' 使用连接
End Using
' 公共API使用XML注释
''' <summary>
''' 根据唯一标识符获取客户。
''' </summary>
''' <param name="customerId">要搜索的客户ID。</param>
''' <returns>找到则返回客户,否则返回Nothing。</returns>
Public Async Function GetCustomerAsync(customerId As Integer) As Task(Of Customer)' Don't use On Error (use Try-Catch)
On Error Resume Next ' Legacy VB6 style - AVOID
' Don't use late binding
Dim obj As Object = CreateObject("Excel.Application") ' Use typed references
' Don't block on async
Dim result = GetDataAsync().Result ' Can deadlock
' Don't use underscores in new code (legacy convention)
Private m_customerName As String ' Use camelCase: _customerName
' Don't concatenate strings in loops
Dim result As String = ""
For Each item In items
result &= item ' Use StringBuilder or String.Join
Next
' Don't use Hungarian notation
Dim strName As String ' Just use: Dim name As String
Dim intCount As Integer ' Just use: Dim count As Integer' 不要使用On Error(改用Try-Catch)
On Error Resume Next ' VB6遗留风格 - 避免使用
' 不要使用后期绑定
Dim obj As Object = CreateObject("Excel.Application") ' 使用类型化引用
' 不要阻塞异步操作
Dim result = GetDataAsync().Result ' 可能导致死锁
' 新代码中不要使用下划线(遗留约定)
Private m_customerName As String ' 使用小驼峰:_customerName
' 不要在循环中拼接字符串
Dim result As String = ""
For Each item In items
result &= item ' 使用StringBuilder或String.Join
Next
' 不要使用匈牙利命名法
Dim strName As String ' 直接使用:Dim name As String
Dim intCount As Integer ' 直接使用:Dim count As Integer