項目

構造体を初期化する

概要

構造体を初期化する方法は、初期化用の変数を定義して代入します。

Option Compare Database
Option Explicit

'--------------------------------------------------------------
' 構造体定義領域
'--------------------------------------------------------------
Private Type testStructure
    bytItem As Byte
    intItem As Integer
    lngItem As Long
    strItem As String * 10
End Type

'--------------------------------------------------------
' 名称    : initStructure
'
' 機能    : 構造体の初期化
'--------------------------------------------------------
Private Sub initStructure()

    Dim tStructure     As testStructure
    Dim initTStructure As testStructure '初期化用
    
    '適当に値を設定
    With tStructure
        .bytItem = 255
        .intItem = 32767
        .lngItem = 2147483647
        .strItem = "test"
    End With
    
    '構造体の内容をイミディエイトウィンドウへ表示
    Call dispStructure(tStructure)
    
    '(1)構造体初期化 (初期化用変数代入)
    tStructure = initTStructure
    
    '構造体の内容をイミディエイトウィンドウへ表示
    Call dispStructure(tStructure)
    
End Sub

'--------------------------------------------------------------
' 構造体の内容をイミディエイトウィンドウへ表示
'--------------------------------------------------------------
Private Sub dispStructure(tStructure As testStructure)

    With tStructure
        Debug.Print "【tStructure】"
        Debug.Print .bytItem
        Debug.Print .intItem
        Debug.Print .lngItem
        Debug.Print .strItem
        Debug.Print
    End With

End Sub