「VB.NET」Newでクラスのインスタンスを生成する

2022年1月19日

書式
Public Class クラス名
Public メンバー変数名 As データ型
End Class
Dim インスタンス名 As New クラス名
インスタンス名.メンバー変数名 = 値

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Module Module1
'クラスStudentの宣言
Public Class Student
'ユーザー名
Public username As String
'年齢
Public age As Integer
'大学名
Public colleage As String
End Class
Public Sub Main()
'Studentクラスのインスタンスを作成
Dim st As New Student
st.username = "山田 太郎"
st.age = 32
st.colleage = "東京工業大学"
Console.WriteLine("名前: " & st.username + " 年齢: " & st.age.ToString + " 大学: " & st.colleage)
Console.ReadKey()
End Sub
End Module
Module Module1 'クラスStudentの宣言 Public Class Student 'ユーザー名 Public username As String '年齢 Public age As Integer '大学名 Public colleage As String End Class Public Sub Main() 'Studentクラスのインスタンスを作成 Dim st As New Student st.username = "山田 太郎" st.age = 32 st.colleage = "東京工業大学" Console.WriteLine("名前: " & st.username + " 年齢: " & st.age.ToString + " 大学: " & st.colleage) Console.ReadKey() End Sub End Module
Module Module1
    'クラスStudentの宣言
    Public Class Student
        'ユーザー名
        Public username As String
        '年齢
        Public age As Integer
        '大学名
        Public colleage As String
    End Class
    Public Sub Main()
        'Studentクラスのインスタンスを作成
        Dim st As New Student
        st.username = "山田 太郎"
        st.age = 32
        st.colleage = "東京工業大学"

        Console.WriteLine("名前: " & st.username + " 年齢: " & st.age.ToString + " 大学: " & st.colleage)

        Console.ReadKey()
    End Sub

End Module

実行結果
名前: 山田 太郎 年齢: 32 大学: 東京工業大学

VB.net

Posted by arkgame