如何:宣告結構 (Visual Basic)How to: Declare a Structure (Visual Basic)

您可以使用Structure 語句開始結構宣告,然後使用 End Structure 語句來結束它。You begin a structure declaration with the Structure Statement, and you end it with the End Structure statement. 在這兩個語句之間,您必須宣告至少一個元素Between these two statements you must declare at least one element. 元素可以是任何資料類型,但至少必須有一個非共用變數或非共用的 noncustom 事件。The elements can be of any data type, but at least one must be either a nonshared variable or a nonshared, noncustom event.

您無法初始化結構宣告中的任何結構元素。You cannot initialize any of the structure elements in the structure declaration. 當您將變數宣告為結構類型時,您可以透過變數來存取它們,藉以指派值給專案。When you declare a variable to be of a structure type, you assign values to the elements by accessing them through the variable.

如需結構和類別之間差異的討論,請參閱結構和類別For a discussion of the differences between structures and classes, see Structures and Classes.

基於示範目的,請考慮您想要追蹤員工姓名、電話分機和薪資的情況。For demonstration purposes, consider a situation where you want to keep track of an employee's name, telephone extension, and salary. 結構可讓您在單一變數中執行此動作。A structure allows you to do this in a single variable.

若要宣告結構To declare a structure

  1. 建立結構的開始和結束語句。Create the beginning and ending statements for the structure.

    您可以使用PublicProtectedFriendPrivate關鍵字來指定結構的存取層級,也可以讓它預設為 PublicYou can specify the access level of a structure using the Public, Protected, Friend, or Private keyword, or you can let it default to Public.

    Private Structure employee  
    End Structure  
    
  2. 將元素加入至結構的主體。Add elements to the body of the structure.

    結構必須至少有一個元素。A structure must have at least one element. 您必須宣告每個元素,並指定它的存取層級。You must declare every element and specify an access level for it. 如果您使用Dim 語句,但沒有任何關鍵字,協助工具預設為 PublicIf you use the Dim Statement without any keywords, the accessibility defaults to Public.

    Private Structure employee  
        Public givenName As String  
        Public familyName As String  
        Public phoneExtension As Long  
        Private salary As Decimal  
        Public Sub giveRaise(raise As Double)  
            salary *= raise  
        End Sub  
        Public Event salaryReviewTime()  
    End Structure  
    

    上述範例中的 salary 欄位是 Private,這表示它無法從結構外部存取,即使是從包含類別也一樣。The salary field in the preceding example is Private, which means it is inaccessible outside the structure, even from the containing class. 不過,giveRaise 程式是 Public的,因此可以從結構外部呼叫。However, the giveRaise procedure is Public, so it can be called from outside the structure. 同樣地,您也可以從結構外部引發 salaryReviewTime 事件。Similarly, you can raise the salaryReviewTime event from outside the structure.

    除了變數、Sub 程式和事件以外,您也可以在結構中定義常數、Function 程式和屬性。In addition to variables, Sub procedures, and events, you can also define constants, Function procedures, and properties in a structure. 您最多可以指定一個屬性做為預設屬性,前提是它至少接受一個引數。You can designate at most one property as the default property, provided it takes at least one argument. 您可以使用共用Sub 程式來處理事件。You can handle an event with a SharedSub procedure. 如需詳細資訊,請參閱如何:在 Visual Basic 中宣告及呼叫預設屬性For more information, see How to: Declare and Call a Default Property in Visual Basic.

請參閱See also