‘ —————————- ‘ 属性定义(8个参数) ‘ —————————- Public Property Get Material() As String Material = pMaterial End Property Public Property Let Material(ByVal Value As String) pMaterial = Value End Property
Public Property Get Length() As Double Length = pLength End Property Public Property Let Length(ByVal Value As Double) pLength = Value End Property
Public Property Get Diameter() As Double Diameter = pDiameter End Property Public Property Let Diameter(ByVal Value As Double) pDiameter = Value End Property
Public Property Get Voltage() As Double Voltage = pVoltage End Property Public Property Let Voltage(ByVal Value As Double) pVoltage = Value End Property
Public Property Get Current() As Double Current = pCurrent End Property Public Property Let Current(ByVal Value As Double) pCurrent = Value End Property
Public Property Get Resistance() As Double Resistance = pResistance End Property Public Property Let Resistance(ByVal Value As Double) pResistance = Value End Property
Public Property Get Temperature() As Double Temperature = pTemperature End Property Public Property Let Temperature(ByVal Value As Double) pTemperature = Value End Property
Public Property Get IsInsulated() As Boolean IsInsulated = pIsInsulated End Property Public Property Let IsInsulated(ByVal Value As Boolean) pIsInsulated = Value End Property
‘ —————————- ‘ 方法1:计算功率 ‘ —————————- Public Function CalculatePower() As Double CalculatePower = pVoltage * pCurrent ‘ P = V * I RaiseEvent StatusChanged(“功率计算完成: ” & CalculatePower & “W”) End Function
‘ —————————- ‘ 方法2:检查安全性 ‘ —————————- Public Sub CheckSafety() Dim msg As String If pTemperature > 80 Then msg = “警告:温度过高(” & pTemperature & “°C)!” ElseIf pCurrent > 10 Then msg = “警告:电流超过安全阈值!” Else msg = “系统状态正常” End If RaiseEvent StatusChanged(msg) ‘ 触发事件 End Sub
‘ 二、调用
‘ 在窗体代码中: Dim WithEvents myWire As clsWire ‘ 必须使用 WithEvents 捕获事件
Private Sub Form_Load() Set myWire = New clsWire myWire.Voltage = 220 myWire.Current = 5 myWire.Temperature = 75
Sub Main()
Dim days As New DaysOfTheWeek()
For Each day As String In days
Console.Write(day & " ")
Next
' Output: Sun Mon Tue Wed Thu Fri Sat
Console.ReadKey()
End Sub
Private Class DaysOfTheWeek
Implements IEnumerable
Public days =
New String() {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
Public Iterator Function GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
' Yield each day of the week.
For i As Integer = 0 To days.Length - 1
Yield days(i)
Next
End Function
End Class
面向对象
面向对象时,类的声明示例:
Public Class className [Inherits bassClassName]
Public varName As Integer '資料成員
Public [ReadOnly/WriteOnly] Property propertyName() As Integer '屬性
Public Get
Return m_var
End Get
Public Set(ByVal value As Integer)
m_var=value
End Set
End Property
Public Sub subName(ByVal param1 As Integer) '方法
End Sub
Public Function funcName(ByVal param2 As Integer ) As Integer '函數
Return 101
End Function
[atrributive_list][accessibiliby][Shadows]Event eventName([Parameter]) [Implements interfaces.event] '事件
Public Sub new([Parameter]) '類的構造子(constructor),可以有多個(即可以重載)
End Sub
End Class
Interface interfaceName
Event eventName([param])
Property [ReadOnly/WriteOnly] propertyName As typeName
Function funcName([param]) As returnTypeName
End Interface
接口中这些成员都强制是Public可访问性。
实现一个接口,使用Implements关键字:
Public Function GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
Delegate Sub PlayHandler(ByVal sender As Object, ByVal e As System.EventArgs)
委托常用于事件处理(Event Handler)。.Net Framework的事件是类的属性(实际上是一个嵌套类),用于封装参数格式固定的委托;该委托的参数总是为(ByVal sender As Object, ByVal e as System.EventArgs),返回值为void(即Sub类型)。例如:
Public Class clsName
Public Event monitor As PlayHandler
End Class
Public Class Person
Public Property age As Integer
Public Property Name As String
End Class
Dim personList As New List(Of Person) From {
New Person With {.Name = "Qiang", .age = 24},
New Person With {.Name = "Wei", .age = 42}
}
匿名类型
例如:
Dim people = New With {.name="kyo", .age=22}
匿名函数或lambda表达式
即无名的函数或过程,作为表达式的值。可以写为一行或多行。例如:
Dim func1=Function(i As integer) i+10
Dim action = sub()
End Sub
Dim func2 = Function()
End Function
Imports System.Runtime.CompilerServices
Public Function funcName(ByVal ins As className) As String
'Do something
End Function
异常处理
Try
' write the condition here
Catch ex As Exception When condition
' Handle Exception when a specific condition is true
Catch ex As Exception
' Handle Exception
Finally
' Cleanup, close connections etc
' NB this code is always executed regardless of if an Exception was raised or not!
End Try
LINQ
From element [ As type ] In collection [, element2 [ As type2 ] In collection2 [, ... ] ] [Where 表达式]
与C家族语言统一使用 { 、 } 定义语句块不同,VB.NET每种语句块都有对应的开头与结尾。如 If ... Else ... End If , Class ... End Class 。
VB.NET定义变量使用 Dim 关键字,同时使用 Public 、 Private 或 Protected 表明作用域时可以省略 Dim 。
VB.NET表示变量类型的方式是在变量名后附加 As Type (Type为变量类型),而C家族语言是在变量名之前书写类型名,无论在定义变量时还是在函数与形参的声明时都是这样。
VB.NET中有没有返回值的特殊子过程 Sub 。VB.NET中没有 void 类型,C#中返回值为 void 的函数对应了VB.NET中的 Sub 。
VB.NET中有一种特殊的类型 Module ,相当于C#中的 static class 。
下面这两份功能一致的代码展现出了VB.NET与C#的很多不同之处:
Visual Basic:
Namespace N
'Comment
Module M
Public Function GetSum(arr() As Integer) As Integer
Dim Sum = 0 'Auto Type Infer
Dim i As Integer
For Each i In arr
Sum += i
Next
Return Sum
End Function
End Module
Class C
Private v As Integer
Public Function F() As Integer
Return v * v
End Function
Public Sub S(value As Integer)
Me.v = value
End Sub
Public Sub New() 'Constructor
Me.v = 0
End Sub
End Class
End Namespace
C#:
namespace N
{
//Comment
static class M
{
int GetSum(int[] arr)
{
var Sum = 0;
int i;
foreach(i in arr)
{
Sum += i;
}
return i;
}
};
class C
{
private:
int v;
public:
int F()
{
return v * v;
}
void S(int value)
{
this.v = value;
}
C() //Constructor
{
this.v = 0;
}
};
}