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

### 若要宣告結構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.