結構和其他程式設計項目 (Visual Basic)Structures and Other Programming Elements (Visual Basic)

您可以搭配使用結構與陣列、物件和程式,以及彼此結合。You can use structures in conjunction with arrays, objects, and procedures, as well as with each other. 互動會使用與這些元素個別使用相同的語法。The interactions use the same syntax as these elements use individually.

注意

您無法初始化結構宣告中的任何結構元素。You cannot initialize any of the structure elements in the structure declaration. 您只能將值指派給已宣告為結構類型之變數的元素。You can assign values only to elements of a variable that has been declared to be of a structure type.

結構和陣列Structures and Arrays

結構可以包含陣列做為它的一個或多個元素。A structure can contain an array as one or more of its elements. 下列範例將說明這點。The following example illustrates this.

Public Structure systemInfo  
    Public cPU As String  
    Public memory As Long  
    Public diskDrives() As String  
    Public purchaseDate As Date  
End Structure   

在結構中存取陣列的值,與存取物件上屬性的方式相同。You access the values of an array within a structure the same way you access a property on an object. 下列範例將說明這點。The following example illustrates this.

Dim mySystem As systemInfo  
ReDim mySystem.diskDrives(3)  
mySystem.diskDrives(0) = "1.44 MB"  

您也可以宣告結構的陣列。You can also declare an array of structures. 下列範例將說明這點。The following example illustrates this.

Dim allSystems(100) As systemInfo  

您會遵循相同的規則來存取此資料結構的元件。You follow the same rules to access the components of this data architecture. 下列範例將說明這點。The following example illustrates this.

ReDim allSystems(5).diskDrives(3)  
allSystems(5).CPU = "386SX"  
allSystems(5).diskDrives(2) = "100M SCSI"  

結構和物件Structures and Objects

結構可以包含物件做為其一個或多個元素。A structure can contain an object as one or more of its elements. 下列範例將說明這點。The following example illustrates this.

Protected Structure userInput  
    Public userName As String  
    Public inputForm As System.Windows.Forms.Form  
    Public userFileNumber As Integer  
End Structure  

您應該在這類宣告中使用特定的物件類別,而不是 ObjectYou should use a specific object class in such a declaration, rather than Object.

結構和程式Structures and Procedures

您可以將結構當做程式引數來傳遞。You can pass a structure as a procedure argument. 下列範例將說明這點。The following example illustrates this.

Public currentCPUName As String = "700MHz Pentium compatible"  
Public currentMemorySize As Long = 256  
Public Sub fillSystem(ByRef someSystem As systemInfo)  
    someSystem.cPU = currentCPUName  
    someSystem.memory = currentMemorySize  
    someSystem.purchaseDate = Now  
End Sub  

上述範例會以傳址方式傳遞結構,讓程式可以修改其專案,讓變更在呼叫程式碼中生效。The preceding example passes the structure by reference, which allows the procedure to modify its elements so that the changes take effect in the calling code. 如果您想要針對這類修改來保護結構,請以傳值方式傳遞。If you want to protect a structure against such modification, pass it by value.

您也可以從 Function 程式傳回結構。You can also return a structure from a Function procedure. 下列範例將說明這點。The following example illustrates this.

Dim allSystems(100) As systemInfo  
Function findByDate(ByVal searchDate As Date) As systemInfo  
    Dim i As Integer  
    For i = 1 To 100  
        If allSystems(i).purchaseDate = searchDate Then Return allSystems(i)  
    Next i  
   ' Process error: system with desired purchase date not found.  
End Function  

結構內的結構Structures Within Structures

結構可以包含其他結構。Structures can contain other structures. 下列範例將說明這點。The following example illustrates this.

Public Structure driveInfo  
    Public type As String  
    Public size As Long  
End Structure  
Public Structure systemInfo  
    Public cPU As String  
    Public memory As Long  
    Public diskDrives() As driveInfo  
    Public purchaseDate As Date  
End Structure  
Dim allSystems(100) As systemInfo  
ReDim allSystems(1).diskDrives(3)  
allSystems(1).diskDrives(0).type = "Floppy"  

您也可以使用這項技術,將一個模組中定義的結構封裝在不同模組中定義的結構內。You can also use this technique to encapsulate a structure defined in one module within a structure defined in a different module.

結構可以包含任意深度的其他結構。Structures can contain other structures to an arbitrary depth.

請參閱See also