Buchempfehlung
Windows System Programming
Windows System Programming
Das Kompendium liefert viele interessante Informationen zur Windows-Programmierung auf Englisch. [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 1 von 12

mdTypes is a collection of FreeBASIC classes which are based on Java classes.
If these classes are Externer Link!generic in Java, the FreeBASIC class will use macros to allow some kind of generics, too.

To start with, mdTypes supports the collection framework of Java. Collections are groups of objects, like lists or maps. Currently we have these classes:

list classes:
- md/util/mdCollection(T)
-- md/util/mdList(T)
---- md/util/mdVector(T)
------ md/util/mdStack(T)
-- md/util/mdSet(T)
-- md/util/mdQueue(T)

map classes:
- md/util/mdMap(K, V) (using inner class mdMapEntry(K, V))
--- md/util/mdDictionary(K, V)
----- md/util/mdHashtable(K, V)
------- md/util/mdProperties

To iterate through the list classes these iterators are available:
- md/util/mdIterator(T)
--- md/util/mdArrayIterator(T)
- md/util/mdEnumeration(T)

Differences:
mdCollection is the base type for list classes. Usually you will use mdList instead of mdCollection.
mdSet does not allow duplicates, while mdList does.
mdVector is a list with a capacity, if reached, no further elements can be added.
mdStack is like mdVector, but with some additional methods. A stack is a "Last In - First Out" list.
mdQueue is a "First In - First Out" list. It also has a capacity.

An easy example for using a simple mdList:

'Imports
#Include Once "md/util/mdList.bi"

'Tell FB, that you want to use mdList with String variables - this will expand the intern macro
mdListDeclare(String)

'Declare a list
Dim As mdList(String) list

'Add some data
list.add("Hallo")
list.add("World")

'Iterate with simple for loop and print out data
For i As Integer = 0 To list.size() - 1
    Print list.get(i),
Next

Sleep

As you can see, it is really simple to create a list with mdTypes. Note, that the get() method is only available in mdList and its derived classes.

All FreeBASIC types can be used with mdTypes, except for Integer. This limitation comes from some methods, that would have same method signatures (this means same name and same parameter list) in case of Integer, and this is not allowed. Instead, you can use Long or mdInteger (I will explain this one later).
Besides, also pointer types are allowed:

'Imports
#Include Once "md/util/mdList.bi"

'Tell FB, that you want to use mdList with Byte Ptr variables - this will expand the intern macro
mdListDeclare(Byte, Ptr)

'Declare a list
Dim As mdList(Byte, Ptr) list

'Create and add some data
Dim As Byte Ptr a = New Byte
Dim As Byte Ptr b = New Byte

*a = 5
*b = 7

list.add(a)
list.add(b)

'Iterate with simple for loop and print out data
For i As Integer = 0 To list.size() - 1
    Print *list.get(i),
    Delete list.get(i)
Next

Sleep

Note the comma between the type and "Ptr" at "mdList(Byte, Ptr)" and "mdListDeclare(Byte, Ptr)". This is obligatory.

But there's another scenario. Having a list, which can operate with almost all FreeBASIC types and even pointers is nice, but what about my own UDT!? It's possible, too!

#Include Once "md/util/mdList.bi"

Type MyClass
    Declare Constructor ()
    Declare Operator Cast() As String
    As Integer myVar
End Type
'Must have a default constructor or no explicit constructor at all!
Constructor MyClass ()

End Constructor
'Must implement cast to string for string representation
Operator MyClass.Cast() As String
    Return Str(myVar)
End Operator

'Operator for comparision must exist!
Operator = (ByRef lhs As MyClass, ByRef rhs As MyClass) As Integer
    Return lhs.myVar = rhs.myVar
End Operator

mdListDeclare(MyClass)
Dim As mdList(MyClass) list

Dim As MyClass foo
foo.myVar = 0 : list.add(foo)
foo.myVar = 1 : list.add(foo)

For i As Integer = 0 To list.size() - 1
    Print list.get(i).myVar
Next

Sleep

To support all possible methods of mdTypes, your own class have to support some operators. First of all, there must be an empty constructor. Then, you have to declare a Cast operator, which returns a String. At last, you have to define the comparision between two objects of your class. The exact implementation is up to you, but it's important to have these.

More examples are included with the mdTypes package.

 

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