[Array] AssociativeArray

  • VB.NET

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von picoflop.

    [Array] AssociativeArray

    Hallo ich habe ein problem wie kann ich in vb.net ein AssociativeArray wie in PHP machen habe mir eine eigene klasse zusammen gebaut in der geht schreiben und lesen nur alle keys auslesen geht net. Würde mich über hilfe freuen.


    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class associative_array
    2. Private Shared array_index As New Dictionary(Of String, Object)
    3. Public Function Item(ByVal TKey, Optional ByVal TValue = "<False>")
    4. If array_index.ContainsKey(TKey) Then
    5. If TValue <> "<False>" Then
    6. array_index.Item(TKey) = TValue
    7. Return True
    8. Else
    9. Return array_index.Item(TKey)
    10. End If
    11. Else
    12. Return False
    13. End If
    14. End Function
    15. Public Function add_dim(ByVal TKey As String)
    16. If array_index.ContainsKey(TKey) Then
    17. Return False
    18. Else
    19. array_index.Add(TKey, New array)
    20. Return True
    21. End If
    22. End Function
    23. Public Function query(ByVal array As String, Optional ByVal TValue As Object = Nothing)
    24. Dim start As Integer = array.IndexOf("[")
    25. Dim ende As Integer = array.IndexOf("]")
    26. Dim search As String = array.Substring(start + 1, ende - start - 1)
    27. array = array.Remove(start, ende - start + 1)
    28. If array_index.ContainsKey(search) Then
    29. If TypeOf array_index.Item(search) Is array Then
    30. If array.Length <> 0 Then
    31. Return query_intern(array, array_index.Item(search), TValue)
    32. Else
    33. Return array_index.Item(search)
    34. End If
    35. Else
    36. If TValue Is Nothing = False Then
    37. array_index.Item(search) = TValue
    38. Return True
    39. Else
    40. Return array_index.Item(search)
    41. End If
    42. End If
    43. Else
    44. If TValue Is Nothing = False Then
    45. If array.Length <> 0 Then
    46. array_index.Add(search, New array)
    47. Return query_intern(array, array_index.Item(search), TValue)
    48. Else
    49. array_index.Add(search, TValue)
    50. Return True
    51. End If
    52. Else
    53. Return False
    54. End If
    55. End If
    56. End Function
    57. Public Function key_colection_query(ByVal array As String)
    58. Dim start As Integer = array.IndexOf("[")
    59. Dim ende As Integer = array.IndexOf("]")
    60. Dim search As String = array.Substring(start + 1, ende - start - 1)
    61. array = array.Remove(start, ende - start + 1)
    62. If array_index.ContainsKey(search) Then
    63. If TypeOf array_index.Item(search) Is array Then
    64. If array.Length <> 0 Then
    65. Return key_colection_query_intern(array, array_index.Item(search))
    66. Else
    67. Return array_index.Item(search).KeyCollection()
    68. End If
    69. End If
    70. End If
    71. End Function
    72. Function key_colection_query_intern(ByVal array As String, ByVal dic As Object)
    73. Dim start As Integer = array.IndexOf("[")
    74. Dim ende As Integer = array.IndexOf("]")
    75. Dim search As String = array.Substring(start + 1, ende - start - 1)
    76. If dic.ContainsKey(search) Then
    77. If TypeOf dic.Item(search) Is array Then
    78. If array.Length <> 0 Then
    79. Return key_colection_query_intern(array, dic.Item(search))
    80. Else
    81. For Each kvp As KeyValuePair(Of String, Object) In dic.Item(search).KeyCollection()
    82. MsgBox(kvp.Key.ToString)
    83. Next
    84. End If
    85. End If
    86. End If
    87. End Function
    88. Function query_intern(ByVal array As String, ByVal dic As Object, Optional ByVal TValue As Object = Nothing)
    89. Dim start As Integer = array.IndexOf("[")
    90. Dim ende As Integer = array.IndexOf("]")
    91. Dim search As String = array.Substring(start + 1, ende - start - 1)
    92. array = array.Remove(start, ende - start + 1)
    93. If dic.ContainsKey(search) Then
    94. If TypeOf dic.Item(search) Is array Then
    95. Return query_intern(array, dic.Item(search), TValue)
    96. Else
    97. If TValue Is Nothing = False Then
    98. dic.Item(search, TValue)
    99. Return True
    100. Else
    101. Return dic.Item(search)
    102. End If
    103. End If
    104. Else
    105. 'MsgBox(TValue.GetType.ToString)
    106. If TValue Is Nothing = False Then
    107. 'MsgBox(array.Length)
    108. If array.Length <> 0 Then
    109. dic.Add(search, New array)
    110. Return query_intern(array, dic.Item(search), TValue)
    111. Else
    112. dic.Add(search, TValue)
    113. Return True
    114. End If
    115. Else
    116. Return False
    117. End If
    118. End If
    119. End Function
    120. Public Function clear_all(ByVal TKey As String, ByVal TValue As Object)
    121. If array_index.ContainsKey(TKey) Then
    122. array_index.Clear()
    123. Return False
    124. Else
    125. array_index.Add(TKey, TValue)
    126. Return True
    127. End If
    128. End Function
    129. Public Function count()
    130. Return array_index.Count
    131. End Function
    132. End Class
    133. Public Class array
    134. Private array As New Dictionary(Of String, Object)
    135. Public Function Item(ByVal TKey, Optional ByVal TValue = Nothing)
    136. If array.ContainsKey(TKey) Then
    137. If TValue Is Nothing = False Then
    138. array.Item(TKey) = TValue
    139. Return True
    140. Else
    141. Return array.Item(TKey)
    142. End If
    143. Else
    144. Return False
    145. End If
    146. End Function
    147. Public Function add(ByVal TKey As String, ByVal TValue As Object)
    148. If array.ContainsKey(TKey) Then
    149. Return False
    150. Else
    151. array.Add(TKey, TValue)
    152. Return True
    153. End If
    154. End Function
    155. Public Function clear(ByVal TKey As String, ByVal TValue As Object)
    156. If array.ContainsKey(TKey) Then
    157. array.Clear()
    158. Return True
    159. Else
    160. Return False
    161. End If
    162. End Function
    163. Function ContainsKey(ByVal Tkey As String)
    164. Return array.ContainsKey(Tkey)
    165. End Function
    166. Public Function KeyCollection()
    167. Dim list As New List(Of String)
    168. For Each kvp As KeyValuePair(Of String, Object) In array
    169. list.Add(kvp.Key.ToString)
    170. MsgBox(kvp.Key.ToString)
    171. Next
    172. Return list
    173. End Function
    174. End Class



    MIt Freundlichen Grüßen
    Rene3233