# Structure 陳述式Structure Statement

## 語法Syntax

[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ Partial ] _
Structure name [ ( Of typelist ) ]
[ Implements interfacenames ]
[ datamemberdeclarations ]
[ methodmemberdeclarations ]
End Structure


## 組件Parts

attributelist 選擇性。Optional. 請參閱屬性清單See Attribute List.
accessmodifier 選擇性。Optional. 可以是下列其中一項：Can be one of the following:

- 公用
- 保護

-

- 私用保護

Shadows 選擇性。Optional. 請參閱ShadowsSee Shadows.
Partial 選擇性。Optional. 表示結構的部分定義。Indicates a partial definition of the structure. 請參閱部分See Partial.
name 必要。Required. 這個結構的名稱。Name of this structure.
Of 選擇性。Optional. 指定這是泛型結構。Specifies that this is a generic structure.
typelist 如果您使用 of 關鍵字，則為必要項Required if you use the Of keyword. 此結構的類型參數清單。List of type parameters for this structure. 請參閱類型清單See Type List.
Implements 選擇性。Optional. 表示此結構會執行一或多個介面的成員。Indicates that this structure implements the members of one or more interfaces. 請參閱Implements 語句
interfacenames 如果您使用 Implements 語句，則為必要。Required if you use the Implements statement. 這個結構所實作為介面的名稱。The names of the interfaces this structure implements.
datamemberdeclarations 必要。Required. 零或多個 ConstDimEnumEvent 語句，宣告結構的資料成員Zero or more Const, Dim, Enum, or Event statements declaring data members of the structure.
methodmemberdeclarations 選擇性。Optional. 零或多個 FunctionOperatorPropertySub 程式的宣告，做為結構的方法成員Zero or more declarations of Function, Operator, Property, or Sub procedures, which serve as method members of the structure.
End Structure 必要。Required. 終止 Structure 定義。Terminates the Structure definition.

## 備註Remarks

Structure 語句會定義您可以自訂的複合實數值型別。The Structure statement defines a composite value type that you can customize. 結構」（structure）是舊版 Visual Basic 的使用者定義型別（UDT）的一般化。A structure is a generalization of the user-defined type (UDT) of previous versions of Visual Basic. 如需詳細資訊，請參閱結構For more information, see Structures.

## 規則Rules

• 嵌入.Nesting. 您可以在另一個中定義一個結構。You can define one structure within another. 外部結構稱為「包含結構」，而內部結構稱為「嵌套結構」。The outer structure is called the containing structure, and the inner structure is called a nested structure. 但是，您無法透過包含結構存取嵌套結構的成員。However, you cannot access a nested structure's members through the containing structure. 相反地，您必須宣告嵌套結構之資料類型的變數。Instead, you must declare a variable of the nested structure's data type.

• 成員宣告。Member Declaration. 您必須宣告結構的每個成員。You must declare every member of a structure. 結構成員無法受到保護Protected Friend，因為沒有任何可繼承自結構的。A structure member cannot be Protected or Protected Friend because nothing can inherit from a structure. 不過，結構本身可以 ProtectedProtected FriendThe structure itself, however, can be Protected or Protected Friend.

您可以在結構中宣告零個或多個非共用變數，或非共用、非自訂的事件。You can declare zero or more nonshared variables or nonshared, noncustom events in a structure. 您不能只有常數、屬性和程式，即使其中一些是非共用的。You cannot have only constants, properties, and procedures, even if some of them are nonshared.

• 初始.Initialization. 您無法初始化結構之任何非共用資料成員的值，做為其宣告的一部分。You cannot initialize the value of any nonshared data member of a structure as part of its declaration. 您必須在結構上使用參數化的函式來初始化這類資料成員，或是在建立結構的實例之後，將值指派給成員。You must either initialize such a data member by means of a parameterized constructor on the structure, or assign a value to the member after you have created an instance of the structure.

• 繼承Inheritance. 結構無法繼承自所有結構所繼承之 ValueType以外的任何類型。A structure cannot inherit from any type other than ValueType, from which all structures inherit. 特別是，一個結構無法繼承自另一個。In particular, one structure cannot inherit from another.

您不能在結構定義中使用Inherits 語句，甚至可以指定 ValueTypeYou cannot use the Inherits Statement in a structure definition, even to specify ValueType.

• 實作.Implementation. 如果結構使用Implements 語句，您就必須執行您在 interfacenames中指定的每個介面所定義的每個成員。If the structure uses the Implements Statement, you must implement every member defined by every interface you specify in interfacenames.

• Default 屬性。Default Property. 結構最多可以使用預設的修飾詞，指定一個屬性做為其預設屬性A structure can specify at most one property as its default property, using the Default modifier. 如需詳細資訊，請參閱DefaultFor more information, see Default.

## 行為Behavior

• 存取層級。Access Level. 在結構中，您可以使用自己的存取層級來宣告每個成員。Within a structure, you can declare each member with its own access level. 所有結構成員都預設為公用存取。All structure members default to Public access. 請注意，如果結構本身有限制較多的存取層級，這會自動限制其成員的存取權，即使您使用存取修飾詞來調整其存取層級也一樣。Note that if the structure itself has a more restricted access level, this automatically restricts access to its members, even if you adjust their access levels with the access modifiers.

• 範圍.Scope. 結構會在其包含的命名空間、類別、結構或模組的範圍內。A structure is in scope throughout its containing namespace, class, structure, or module.

每個結構成員的範圍都是整個結構。The scope of every structure member is the entire structure.

• 期.Lifetime. 結構本身不會有存留期。A structure does not itself have a lifetime. 相反地，該結構的每個實例都有與其他所有實例無關的存留期。Rather, each instance of that structure has a lifetime independent of all other instances.

實例的存留期會在新的 Operator子句建立時開始。The lifetime of an instance begins when it is created by a New Operator clause. 它會在保留該變數的存留期結束時結束。It ends when the lifetime of the variable that holds it ends.

您無法擴充結構實例的存留期。You cannot extend the lifetime of a structure instance. 對靜態結構功能的近似值是由模組提供。An approximation to static structure functionality is provided by a module. 如需詳細資訊，請參閱Module 語句For more information, see Module Statement.

• 加.Qualification. 結構外的程式碼必須使用該結構的名稱來限定成員名稱。Code outside a structure must qualify a member's name with the name of that structure.

如果嵌套結構內的程式碼會對程式設計項目進行不合格的參考，Visual Basic 會先在嵌套結構中搜尋該專案，然後在其包含結構中，依此類推，直到最外層的包含專案。If code inside a nested structure makes an unqualified reference to a programming element, Visual Basic searches for the element first in the nested structure, then in its containing structure, and so on out to the outermost containing element. 如需詳細資訊，請參閱 References to Declared ElementsFor more information, see References to Declared Elements.

• 記憶體耗用量。Memory Consumption. 如同所有複合資料型別，您無法安全地計算結構的總記憶體耗用量，方法是將其成員的名義儲存體配置相加。As with all composite data types, you cannot safely calculate the total memory consumption of a structure by adding together the nominal storage allocations of its members. 此外，您無法安全地假設記憶體中的儲存順序與您的宣告順序相同。Furthermore, you cannot safely assume that the order of storage in memory is the same as your order of declaration. 如果您需要控制結構的儲存配置，您可以將 StructLayoutAttribute 屬性套用至 Structure 語句。If you need to control the storage layout of a structure, you can apply the StructLayoutAttribute attribute to the Structure statement.

## 範例Example

Public Structure employee
' Public members, accessible from throughout declaration region.
Public firstName As String
Public middleName As String
Public lastName As String
' Friend members, accessible from anywhere within the same assembly.
Friend employeeNumber As Integer
Friend workPhone As Long
' Private members, accessible only from within the structure itself.
Private homePhone As Long
Private level As Integer
Private salary As Double
Private bonus As Double
' Procedure member, which can access structure's private members.
Friend Sub CalculateBonus(ByVal rate As Single)
bonus = salary * CDbl(rate)
End Sub
' Property member to return employee's eligibility.
Friend ReadOnly Property Eligible() As Boolean
Get
Return level >= 25
End Get
End Property
' Event member, raised when business phone number has changed.
Public Event ChangedWorkPhone(ByVal newPhone As Long)
End Structure