Home > Software engineering >  Data structure handle as array
Data structure handle as array

Time:04-30

i'm use data structure to store JSON data

 Class node
        Friend ReadOnly position As Integer
        Friend ReadOnly position_Array As Integer

        Public index As Integer = 0

        Friend head As String
        Friend parent As node
        Friend is_array_item As Boolean = False
        '  Friend attrib As node_val
        Friend child_list As New List(Of node_val)
        Friend tail As String
        Sub New(ByRef a As node, ByRef pos As Integer) ', b As node_val)
            parent = a
            '  attrib = b
            position = pos ' a.child_list.Count - 1
        End Sub

    End Class

dim n as new node n.child_list(8).val

dim n as new node
n.child_list(8).val 

i want to this class's object handle as array

n(8).val

how i do this

CodePudding user response:

You can add a Default Property to your node class that will return an item from your child_list:

Class node

    Friend ReadOnly position As Integer
    Friend ReadOnly position_Array As Integer
    Public index As Integer = 0
    Friend head As String
    Friend parent As node
    Friend is_array_item As Boolean = False
    Friend child_list As New List(Of node)
    Friend tail As String

    Sub New(ByRef a As node, ByRef pos As Integer)
        parent = a
        position = pos
    End Sub

    ''' <summary>
    ''' Gets or sets child node.
    ''' </summary>
    ''' <param name="index"></param>
    Default Property Child(ByVal index As Integer) As node
        Get
            If index >= 0 And index < child_list.Count Then
                Return child_list.Item(index)
            Else
                ' index is out of bounds
                Return Nothing
            End If
        End Get
        Set(value As node)
            If index < 0 Then Return
            If index < child_list.Count Then
                child_list.Item(index) = value
            Else
                ' Add node at end of list
                child_list.Add(value)
            End If
        End Set
    End Property

End Class

Please be careful with the setter on the Child property. Items in a list should be inserted sequentially.

  • Related