How to access items inside object-oriented type hierarchies in VB.NET

1 Answer

0 votes
Imports System

Module Program

    ' 1. Define metadata class
    Public Class Metadata
        Public Property Name As String
        Public Property IsActive As Boolean
    End Class

    ' 2. Define a base class for the variant dimensions (union equivalent)
    Public MustInherit Class Dimension
    End Class

    Public Class Circle
        Inherits Dimension
        Public Property Radius As Single
    End Class

    Public Class Rectangle
        Inherits Dimension
        Public Property Width As Single
        Public Property Height As Single
    End Class

    ' 3. Define the main container class holding ID, metadata, and variant dimension
    Public Class Shape
        Public Property Id As Integer
        Public Property Metadata As Metadata
        Public Property Dimension As Dimension
    End Class

    ''' <summary>
    ''' Safely accesses and prints properties from nested classes and variant types
    ''' using idiomatic VB.NET type-checking patterns.
    ''' </summary>
    Public Sub PrintShapeProperties(shape As Shape)
        If shape Is Nothing Then
            Console.WriteLine("Error: Received null shape.")
            Return
        End If

        ' Accessing nested properties using standard dot syntax
        Console.WriteLine($"Shape ID: {shape.Id}")
        Console.WriteLine($"Shape Name: {shape.Metadata.Name}")
        Console.WriteLine($"Status: {If(shape.Metadata.IsActive, "Active", "Inactive")}")

        ' Check runtime type and safely cast to access active variant fields
        If TypeOf shape.Dimension Is Circle Then
            Dim c = DirectCast(shape.Dimension, Circle)
            Console.WriteLine("Type: Circle")
            Console.WriteLine($"Radius: {c.Radius}")
        ElseIf TypeOf shape.Dimension Is Rectangle Then
            Dim r = DirectCast(shape.Dimension, Rectangle)
            Console.WriteLine("Type: Rectangle")
            Console.WriteLine($"Width: {r.Width}, Height: {r.Height}")
        End If

        Console.WriteLine("------------------------------------")
    End Sub

    Sub Main(args As String())
        ' Initialize a Circle shape using object initializers
        Dim myCircle As New Shape With {
            .Id = 101,
            .Metadata = New Metadata With {.Name = "Unit Circle", .IsActive = True},
            .Dimension = New Circle With {.Radius = 5.5F}
        }

        ' Initialize a Rectangle shape
        Dim myRectangle As New Shape With {
            .Id = 102,
            .Metadata = New Metadata With {.Name = "Bounding Box", .IsActive = False},
            .Dimension = New Rectangle With {.Width = 10.0F, .Height = 20.0F}
        }

        ' Print properties
        PrintShapeProperties(myCircle)
        PrintShapeProperties(myRectangle)
    End Sub

End Module
														
														
' run:
'
' Shape ID: 101
' Shape Name: Unit Circle
' Status: Active
' Type: Circle
' Radius: 5.5
' ------------------------------------
' Shape ID: 102
' Shape Name: Bounding Box
' Status: Inactive
' Type: Rectangle
' Width: 10, Height: 20
' ------------------------------------
'

 



answered 2 days ago by avibootz

Related questions

...