結構 (Visual Basic)Structures (Visual Basic)

結構」(structure)是舊版 Visual Basic 支援的使用者定義型別(UDT)的一般化。A structure is a generalization of the user-defined type (UDT) supported by previous versions of Visual Basic. 除了欄位之外,結構也可以公開屬性、方法和事件。In addition to fields, structures can expose properties, methods, and events. 結構可以執行一或多個介面,而且您可以為每個欄位宣告個別的存取層級。A structure can implement one or more interfaces, and you can declare individual access levels for each field.

您可以結合不同類型的資料項目來建立結構。You can combine data items of different types to create a structure. 結構會將一或多個專案與彼此以及結構本身產生關聯。A structure associates one or more elements with each other and with the structure itself. 當您宣告結構時,它會成為複合資料型別,而您可以宣告該類型的變數。When you declare a structure, it becomes a composite data type, and you can declare variables of that type.

當您想要讓單一變數保存數個相關的資訊時,結構會很有用。Structures are useful when you want a single variable to hold several related pieces of information. 例如,您可能想要將員工的名稱、電話分機和薪資保持在一起。For example, you might want to keep an employee's name, telephone extension, and salary together. 您可以使用數個變數來取得這項資訊,或者您可以定義結構並將它用於單一員工變數。You could use several variables for this information, or you could define a structure and use it for a single employee variable. 當您有許多員工,因此有許多不同的變數實例時,結構的優勢就會變得很明顯。The advantage of the structure becomes apparent when you have many employees and therefore many instances of the variable.

本節內容In This Section

如何:宣告結構How to: Declare a Structure
顯示如何宣告結構及其元素。Shows how to declare a structure and its elements.

結構變數Structure Variables
涵蓋將結構指派給變數,並存取其元素。Covers assigning a structure to a variable and accessing its elements.

結構和其他程式設計項目Structures and Other Programming Elements
摘要說明結構與陣列、物件、程式等的互動方式。Summarizes how structures interact with arrays, objects, procedures, and each other.

結構和類別Structures and Classes
描述結構與類別之間的相似性和差異。Describes the similarities and differences between structures and classes.

資料類型Data Types
介紹 Visual Basic 的資料類型,並說明如何使用它們。Introduces the Visual Basic data types and describes how to use them.

資料類型Data Types
列出 Visual Basic 所提供的基本資料類型。Lists the elementary data types supplied by Visual Basic.