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
MIt Freundlichen Grüßen
Rene3233
VB.NET-Quellcode
- Public Class associative_array
- Private Shared array_index As New Dictionary(Of String, Object)
- Public Function Item(ByVal TKey, Optional ByVal TValue = "<False>")
- If array_index.ContainsKey(TKey) Then
- If TValue <> "<False>" Then
- array_index.Item(TKey) = TValue
- Return True
- Else
- Return array_index.Item(TKey)
- End If
- Else
- Return False
- End If
- End Function
- Public Function add_dim(ByVal TKey As String)
- If array_index.ContainsKey(TKey) Then
- Return False
- Else
- array_index.Add(TKey, New array)
- Return True
- End If
- End Function
- Public Function query(ByVal array As String, Optional ByVal TValue As Object = Nothing)
- Dim start As Integer = array.IndexOf("[")
- Dim ende As Integer = array.IndexOf("]")
- Dim search As String = array.Substring(start + 1, ende - start - 1)
- array = array.Remove(start, ende - start + 1)
- If array_index.ContainsKey(search) Then
- If TypeOf array_index.Item(search) Is array Then
- If array.Length <> 0 Then
- Return query_intern(array, array_index.Item(search), TValue)
- Else
- Return array_index.Item(search)
- End If
- Else
- If TValue Is Nothing = False Then
- array_index.Item(search) = TValue
- Return True
- Else
- Return array_index.Item(search)
- End If
- End If
- Else
- If TValue Is Nothing = False Then
- If array.Length <> 0 Then
- array_index.Add(search, New array)
- Return query_intern(array, array_index.Item(search), TValue)
- Else
- array_index.Add(search, TValue)
- Return True
- End If
- Else
- Return False
- End If
- End If
- End Function
- Public Function key_colection_query(ByVal array As String)
- Dim start As Integer = array.IndexOf("[")
- Dim ende As Integer = array.IndexOf("]")
- Dim search As String = array.Substring(start + 1, ende - start - 1)
- array = array.Remove(start, ende - start + 1)
- If array_index.ContainsKey(search) Then
- If TypeOf array_index.Item(search) Is array Then
- If array.Length <> 0 Then
- Return key_colection_query_intern(array, array_index.Item(search))
- Else
- Return array_index.Item(search).KeyCollection()
- End If
- End If
- End If
- End Function
- Function key_colection_query_intern(ByVal array As String, ByVal dic As Object)
- Dim start As Integer = array.IndexOf("[")
- Dim ende As Integer = array.IndexOf("]")
- Dim search As String = array.Substring(start + 1, ende - start - 1)
- If dic.ContainsKey(search) Then
- If TypeOf dic.Item(search) Is array Then
- If array.Length <> 0 Then
- Return key_colection_query_intern(array, dic.Item(search))
- Else
- For Each kvp As KeyValuePair(Of String, Object) In dic.Item(search).KeyCollection()
- MsgBox(kvp.Key.ToString)
- Next
- End If
- End If
- End If
- End Function
- Function query_intern(ByVal array As String, ByVal dic As Object, Optional ByVal TValue As Object = Nothing)
- Dim start As Integer = array.IndexOf("[")
- Dim ende As Integer = array.IndexOf("]")
- Dim search As String = array.Substring(start + 1, ende - start - 1)
- array = array.Remove(start, ende - start + 1)
- If dic.ContainsKey(search) Then
- If TypeOf dic.Item(search) Is array Then
- Return query_intern(array, dic.Item(search), TValue)
- Else
- If TValue Is Nothing = False Then
- dic.Item(search, TValue)
- Return True
- Else
- Return dic.Item(search)
- End If
- End If
- Else
- 'MsgBox(TValue.GetType.ToString)
- If TValue Is Nothing = False Then
- 'MsgBox(array.Length)
- If array.Length <> 0 Then
- dic.Add(search, New array)
- Return query_intern(array, dic.Item(search), TValue)
- Else
- dic.Add(search, TValue)
- Return True
- End If
- Else
- Return False
- End If
- End If
- End Function
- Public Function clear_all(ByVal TKey As String, ByVal TValue As Object)
- If array_index.ContainsKey(TKey) Then
- array_index.Clear()
- Return False
- Else
- array_index.Add(TKey, TValue)
- Return True
- End If
- End Function
- Public Function count()
- Return array_index.Count
- End Function
- End Class
- Public Class array
- Private array As New Dictionary(Of String, Object)
- Public Function Item(ByVal TKey, Optional ByVal TValue = Nothing)
- If array.ContainsKey(TKey) Then
- If TValue Is Nothing = False Then
- array.Item(TKey) = TValue
- Return True
- Else
- Return array.Item(TKey)
- End If
- Else
- Return False
- End If
- End Function
- Public Function add(ByVal TKey As String, ByVal TValue As Object)
- If array.ContainsKey(TKey) Then
- Return False
- Else
- array.Add(TKey, TValue)
- Return True
- End If
- End Function
- Public Function clear(ByVal TKey As String, ByVal TValue As Object)
- If array.ContainsKey(TKey) Then
- array.Clear()
- Return True
- Else
- Return False
- End If
- End Function
- Function ContainsKey(ByVal Tkey As String)
- Return array.ContainsKey(Tkey)
- End Function
- Public Function KeyCollection()
- Dim list As New List(Of String)
- For Each kvp As KeyValuePair(Of String, Object) In array
- list.Add(kvp.Key.ToString)
- MsgBox(kvp.Key.ToString)
- Next
- Return list
- End Function
- End Class
MIt Freundlichen Grüßen
Rene3233