Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Tutorial

Using mdTypes [EN]

von RedakteurMODSeite 4 von 12

While working with collections you may have to serialize its content. All collections can be casted to a normal String. You can use BefehlsreferenzeintragCast, BefehlsreferenzeintragStr or the toString() method. While creating String representations is really easy, deserializing these Strings back to lists and maps may be harder. For this purpose we have a helper class:
- md/helper/mdCollectionsHelper

mdCollectionsHelper contains only a few static methods:

createList(String) As mdList(String)
createMap(String) As mdMap(String, String)
encode(String) As String
decode(String) As String

So, createList will generate a list out of the String, createMap a map. Because the representation of these Strings, it's important not to conflict with some special characters. To avoid problems, encode all Strings in you map containing these signs !"[]{}=,\r\n". On the other side you can use decode to get the correct value back.

Let's check out an example of how it might work:

'Include mdCollectionsHelper (it will include both mdList and mdMap for us)
#Include Once "md/helper/mdCollectionsHelper.bi"

'Prepare mdMap - mdCollectionsHelper will use this
mdMapDeclare(String, String)

'Sample class
Type MyClass
    Declare Constructor ()
    Declare Constructor (ByRef m As mdMap(String, String))

    Declare Operator Cast() As String

    As String label
    As Integer value
    As Double weight
End Type

Constructor MyClass()

End Constructor

'This constructor will get a map and fill the values of this class with the content of the map
Constructor MyClass(ByRef m As mdMap(String,  String))
    This.label = mdCollectionsHelper.decode(m.get("label")) 'if this data may be encoded, decode it again (see cast operator)
    This.value = ValInt(m.get("value"))
    This.weight = Val(m.get("weight"))
End Constructor

'The cast operator will create a map representation of the data in this class
Operator MyClass.Cast() As String
    Dim As String temp
    Dim As mdMap(String, String) m
    temp = m.put("label", mdCollectionsHelper.encode(This.label)) 'encoding is not necessary but not a bad idea for string data
    temp = m.put("value", Str(This.value))
    temp = m.put("weight", Str(This.weight))
    Return m
End Operator

Operator = (ByRef lhs As MyClass, ByRef rhs As MyClass) As Boolean
    Return Str(lhs) = Str(rhs)
End Operator

'Prepare sample data - I've filled it into an array and then into a list, but it's just an example
mdListDeclare(MyClass)

Dim As mdList(MyClass) listOfMyClass
Dim As MyClass firstArray(0 To 3)

For i As Integer = LBound(firstArray) To UBound(firstArray)
    firstArray(i).label = "label" & Str(i)
    firstArray(i).value = i
    firstArray(i).weight = i * 1.1

    listOfMyClass.add(firstArray(i))
Next

'Let's see the output for our created list - at this point we could send the data via internet or save it into a file or whatever...
Dim As String stringRepresentation = listOfMyClass
Print "List as string: " & stringRepresentation
Print

'Get a list back of the string and prepare a new array
Dim As mdList(String) listOfMaps = mdCollectionsHelper.createList(stringRepresentation)
Dim As MyClass newArray(0 To listOfMaps.size() - 1)

'Fill the array with the data of our string representation
For i As Integer = LBound(newArray) To UBound(newArray)
    newArray(i) = Type<MyClass>(mdCollectionsHelper.createMap(listOfMaps.get(i)))
    Print i, newArray(i)
Next

Sleep

 

Gehe zu Seite Gehe zu Seite  1  2  3  4  5  6  7  8  9  10  11  12  
Zusätzliche Informationen und Funktionen
  • Das Tutorial wurde am 17.04.2014 von RedakteurMOD angelegt.
  • Die aktuellste Version wurde am 31.07.2019 von RedakteurMOD gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen