How to declare, initialize, and print a medium complexity, varied, feature‑rich data structure in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

' Address class with simple fields
Public Class Address
    Public Property Street As String
    Public Property City As String
    Public Property Zip As Integer
End Class

' Status enumeration
Public Enum Status
    Active
    Inactive
    Unknown
End Enum

' Person class with nested class, arrays, lists, and nullable fields
Public Class Person
    Public Property HomeAddress As Address
    Public Property FavoriteNumbers As Integer()
    Public Property Nickname As String
    Public Property BirthDate As Date
    Public Property Notes As String
    Public Property MiddleName As String
    Public Property Hobbies As List(Of String)
    Public Property Status As Status

    Public Sub New()
        HomeAddress = New Address()
        FavoriteNumbers = New Integer(4) {}
        Nickname = Nothing
        BirthDate = New Date(1990, 1, 1)
        Notes = "No notes"
        MiddleName = Nothing
        Hobbies = New List(Of String)()
        Status = Status.Unknown
    End Sub
End Class

Module Program

    Sub PrintPerson(p As Person)
        Console.WriteLine("Address: " &
                          p.HomeAddress.Street & ", " &
                          p.HomeAddress.City & " " &
                          p.HomeAddress.Zip)

        Console.Write("Favorite Numbers: ")
        For Each n In p.FavoriteNumbers
            Console.Write(n & " ")
        Next
        Console.WriteLine()

        Console.WriteLine("Nickname: " &
                          If(p.Nickname IsNot Nothing, p.Nickname, "(none)"))

        Console.WriteLine("Birth Date: " &
                          p.BirthDate.ToString("yyyy-MM-dd"))

        Console.WriteLine("Notes: " & p.Notes)

        Console.WriteLine("Middle Name: " &
                          If(p.MiddleName IsNot Nothing, p.MiddleName, "(none)"))

        Console.Write("Hobbies: ")
        For Each h In p.Hobbies
            Console.Write(h & ", ")
        Next
        Console.WriteLine()

        Console.WriteLine("Status: " & p.Status.ToString())
    End Sub

    Sub Main()
        Dim p As New Person()

        p.HomeAddress.Street = "Fifth Avenue"
        p.HomeAddress.City = "New York"
        p.HomeAddress.Zip = 423900

        p.FavoriteNumbers = {7, 13, 21, 42, 99}

        p.Nickname = "Orion"

        p.BirthDate = New Date(1026, 6, 15)

        p.Notes = "Voss likes C/C++ and structs"

        p.MiddleName = "Pulse‑9"

        p.Hobbies.Add("Coding")
        p.Hobbies.Add("Dreaming")
        p.Hobbies.Add("Walking")
        p.Hobbies.Add("Sci-Fi Movies")

        p.Status = Status.Active

        PrintPerson(p)
    End Sub

End Module

 
                                                                     
' run:
'
' Address: Fifth Avenue, New York 423900
' Favorite Numbers: 7 13 21 42 99 
' Nickname: Orion
' Birth Date: 1026-06-15
' Notes: Voss likes C/C++ and structs
' Middle Name: Pulse‑9
' Hobbies: Coding, Dreaming, Walking, Sci-Fi Movies, 
' Status: Active
'

 



answered Sep 3 by avibootz

Related questions

...