「VB.NET」構造体(Structure)の値を表示するサンプル

構文
1.構造体の定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Structure 構造体の名称
 Public 変数名 As データの型
Public Sub メソット名(ByVal 変数名 As データの型,...)
処理コード
End Sub
End Structure
Structure 構造体の名称  Public 変数名 As データの型 Public Sub メソット名(ByVal 変数名 As データの型,...) 処理コード End Sub End Structure
Structure 構造体の名称
 Public 変数名 As データの型
 Public Sub メソット名(ByVal 変数名 As データの型,...)
  処理コード
 End Sub
End Structure

構造体はコンストラクタやメソッドも使用できます。
構造体は値型で、クラスは参照型です。
2.構造体を使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Dim 変数名 As 構造体名
変数名.メソット名()
Dim 変数名 As 構造体名 変数名.メソット名(値)
Dim 変数名 As 構造体名
変数名.メソット名(値)

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Structure Students '構造体の定義
'変数の宣言
Public userID As String
Public age As Integer
'構造体のメソッド
Public Sub setInfo(ByVal uid As String, ByVal stage As Integer)
userID = uid
age = stage
End Sub
End Structure
Module Module1
Public Sub Main()
'構造体を宣言し
Dim ss As Students
'構造体の初期化メソッドを呼び出す
ss.setInfo("test007", 26)
'構造体の値を出力する
Console.WriteLine("ユーザーID: " & ss.userID)
Console.WriteLine("年齢:" & ss.age)
Console.ReadKey()
End Sub
End Module
Structure Students '構造体の定義 '変数の宣言 Public userID As String Public age As Integer '構造体のメソッド Public Sub setInfo(ByVal uid As String, ByVal stage As Integer) userID = uid age = stage End Sub End Structure Module Module1 Public Sub Main() '構造体を宣言し Dim ss As Students '構造体の初期化メソッドを呼び出す ss.setInfo("test007", 26) '構造体の値を出力する Console.WriteLine("ユーザーID: " & ss.userID) Console.WriteLine("年齢:" & ss.age) Console.ReadKey() End Sub End Module
Structure Students '構造体の定義
    '変数の宣言
    Public userID As String
    Public age As Integer

    '構造体のメソッド
    Public Sub setInfo(ByVal uid As String, ByVal stage As Integer)
        userID = uid
        age = stage
    End Sub
End Structure

Module Module1
    Public Sub Main()

        '構造体を宣言し
        Dim ss As Students
        '構造体の初期化メソッドを呼び出す
        ss.setInfo("test007", 26)

        '構造体の値を出力する
        Console.WriteLine("ユーザーID: " & ss.userID)
        Console.WriteLine("年齢:" & ss.age)

        Console.ReadKey()

    End Sub

End Module

実行結果
ユーザーID: test007
年齢:26

VB.net

Posted by arkgame