HOW TO:重複使用工作元件

更新:2007 年 11 月

如果元件已存在,且已偵錯完畢並可正常運作,則最好是將它用於程式碼,而非開發另一個具有相同功能的元件。這類元件通常是公開為類別。若要重複使用它,可從該類別中建立物件。

範例

.NET Framework 提供許多可使用之元件的範例。其中一個元件是 System 命名空間中的 TimeZone 類別。TimeZone 提供成員,該成員可讓您擷取目前電腦系統時區的相關資訊。

Public Sub examineTimeZone()
    Dim tz As System.TimeZone = System.TimeZone.CurrentTimeZone
    Dim s As String = "Current time zone is "
    s &= CStr(tz.GetUtcOffset(Now).Hours) & " hours and "
    s &= CStr(tz.GetUtcOffset(Now).Minutes) & " minutes "
    s &= "different from UTC (coordinated universal time)"
    s &= vbCrLf & "and is currently "
    If tz.IsDaylightSavingTime(Now) = False Then s &= "not "
    s &= "on ""summer time""."
    MsgBox(s)
End Sub

第一個 Dim 陳述式 (Visual Basic) 宣告型別 TimeZone 的物件變數,並將 CurrentTimeZone 屬性所傳回的 TimeZone 物件指定給它。

請參閱

工作

HOW TO:建立物件

HOW TO:定義使用現有類別成員的類別

HOW TO:存取物件的共用和非共用成員

概念

物件和類別