VB.Net program to demonstrate the structure within a structure

Here, we are going to demonstrate the structure within a structure in VB.Net.
By Nidhi Last updated : April 01, 2023

Structure within a structure in VB.Net

Here, we will create a Student structure that contains a nested structure StuInfo. Here, we set the value of nested structure and then print values on the console screen.

VB.Net code to implement the structure within a structure

The source code to demonstrate structure within the structure is given below. The given program is compiled and executed successfully.

'VB.Net program to demonstrate structure within a structure.

Module Module1
    Structure Student
        Public id As Integer
        Public fee As Integer

        Structure StuInfo
            Public name As String
            Public address As String
        End Structure
    End Structure

    Sub Main()
        Dim stu As New Student()

        stu.id = 101
        stu.fee = 2000

        Dim info As New Student.StuInfo()

        info.name = "Shaurya"
        info.address = "New Delhi"

        Console.WriteLine("Student Information: ")
        Console.WriteLine(vbTab & "Id       : " & stu.id)
        Console.WriteLine(vbTab & "Name     : " & info.name)
        Console.WriteLine(vbTab & "Fee      : " & stu.fee)
        Console.WriteLine(vbTab & "Address  : " & info.name)
    End Sub
End Module

Output

Student Information:
        Id       : 101
        Name     : Shaurya
        Fee      : 2000
        Address  : Shaurya
Press any key to continue . . .

Explanation

In the above program, we created a module Module1. Here, we created a Structure Student that contains data members' id, fees. The Student structure also contains a nested structure StuInfo. The StuInfo structure two public members' name and address.

In the Main() function, we initialized the Student and StuInfo structure and then printed the values of members on the console screen.

Implementing by referencing the inner structure from the outer structure

Expert's solution [by: David Goben]

One can reference the inner structure from the outer structure, combined as a single structure object. To do that, after defining the inner structure, declare a local field in the outer structure of the inner structure's type. This gives the inner structure actualization within the outer structure's object space on the Stack. If we disassemble C++ compiled code, this is what the helper code that the compiler adds to realize C++'s mid-level instructions.

Declare your structure with embedded structure (s), and create a reference to each inner structure, typically right after each inner structure is defined.

Code

Module VBModule
    Structure Student
        Public id As Integer
        Public fee As Integer
        
        Structure StuInfo
            Public name As String
            Public address As String
        End Structure
        
        'The above statement  gives StuInfo structure assigned space 
        'within the Student structure 
        '(otherwise StuInfo would remain an unactualized definition).
        Public mStuInfo As StuInfo 
    End Structure
    
    Sub Main()
        Dim Sdnt As New Student
        Sdnt.mStuInfo.name = "Joe Cool"
        Console.WriteLine(Sdnt.mStuInfo.name)
    End Sub
End Module

Output

Assembly 'a, Version=0.0, Culture=neutral, PublicKeyToken=null' 
saved successfully to '/home/a.out'.
Compilation successful
Compilation took 00:00:00.8126430
Joe Cool

VB.Net Structure Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.