I ask if anyone knows if exists the type declaration list(of..) like in Visual Basic, and the "for each" instruccion. If not exists what is the
best form of implementation?
Example
dim L1 as List ( of double)
for i as integer = 1 to 100
L1.add(cdbl(i))
next i
for each d as double in L1.Items
print d
next
List (of ....) . Is in freebasic any form of lists like in visual bsic
-
- Posts: 64
- Joined: Jul 15, 2009 12:41
Re: List (of ....) . Is in freebasic any form of lists like in visual bsic
No, no such thing, however an array of double can be used:
Code: Select all
Dim As Double L1(0 To 99) ' 100 elements
For i As UInteger = 0 To 99
L1(i) = CDbl(i + 1)
Next
...
Re: List (of ....) . Is in freebasic any form of lists like in visual bsic
You could implement your own list type or google for 'freebasic list type', 'lists in freebasic', etc. For example look here for one example https://gist.github.com/StringEpsilon/a ... 229af7f734. Also, the FreeBASIC Extended Library http://ext.freebasic.net/ contained a list type though I have not used that library for a long time and do not know if it is still in development.
Re: List (of ....) . Is in freebasic any form of lists like in visual bsic
You can also look at code for a Keyed Linked List by PaulSquires:
viewtopic.php?f=7&t=23902
viewtopic.php?f=7&t=23902
Re: List (of ....) . Is in freebasic any form of lists like in visual bsic
mdTypes also includes several collection implementations as well as other useful basics. See https://www.freebasic-portal.de/tutoria ... n-108.html for more details on how to use it. Download: http://users.freebasic-portal.de/mod/do ... dTypes.zip
Re: List (of ....) . Is in freebasic any form of lists like in visual bsic
I've had problems trying to use the extended library, however the list does indeed work.cdavidson wrote:the FreeBASIC Extended Library http://ext.freebasic.net/ contained a list type
Documentation is there but it is not geared for the beginning user and I found myself scouring the code trying to figure out issues.
There are examples that are a big help in understanding usage.
Here is an example of using List and creating a ForEach callback method to process the list items:
Code: Select all
#Include once "ext/containers/list.bi"
#Ifndef FALSE
#Define FALSE 0
#EndIf
#Ifndef TRUE
#Define TRUE Not FALSE
#EndIf
Type MyDoubles
double_list As ext.fbext_List( ((Double)) )
Declare sub ForEach(ByVal callback As Sub(ByRef d As Double))
End Type
Sub MyDoubles.ForEach(ByVal callback As Sub(ByRef d As Double))
Var iterator = this.double_list.begin
while iterator <> this.double_list.End_()
callback( *(iterator.get()))
iterator.increment()
Wend
End Sub
Sub ProcessListItem(ByRef d As Double)
Print d
End Sub
Dim As MyDoubles md
md.double_list.pushback(1) ' add a few doubles
md.double_list.pushback(2)
md.double_list.pushback(3)
md.double_list.pushback(7)
md.ForEach(@ProcessListItem)
Sleep
In this case, List.bi undefines TRUE and FALSE and you will get errors unless you redefine them as you can see in the previous code. (In the final code I posted here I don't use TRUE or FALSE, but I did in testing)