Nested structs Learn programming Visual Basic (VB.net)

Lesson:

Arrays, Structures and Strings


Exercise:

Nested structs


Objetive:

Create a struct to store two data for a person:

name and date of birth.

The date of birth must be another struct consisting on day, month and year.

Finally, create an array of persons, ask the user for the data of two persons and display them.


Code:

Imports System
Class exercise90
    Structure person
        Public Name As String
        Public Date As dateBirth
    End Structure

    Structure dateBirth
        Public Day As Integer
        Public Month As Integer
        Public Year As Integer
    End Structure

    Private Shared Sub Main(ByVal args As String())
        Dim d As Integer = 0, m As Integer = 0, y As Integer = 0
        Dim total As Integer = 1
        Dim p As person() = New person(total - 1) {}

        For i As Integer = 0 To total
            Console.Write("Enter name: ")
            Dim Name As String = Console.ReadLine()
            Console.WriteLine()
            p(i).Name = Name
            Console.Write("Enter day: ")
            d = Convert.ToInt32(Console.ReadLine())
            Console.WriteLine()
            p(i).Date.Day = d
            Console.Write("Enter month: ")
            d = Convert.ToInt32(Console.ReadLine())
            Console.WriteLine()
            p(i).Date.Month = m
            Console.Write("Enter year: ")
            d = Convert.ToInt32(Console.ReadLine())
            Console.WriteLine()
            p(i).Date.Year = y
        Next
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.