List (of ....) . Is in freebasic any form of lists like in visual bsic

General FreeBASIC programming questions.
Post Reply
geminis4941
Posts: 64
Joined: Jul 15, 2009 12:41

List (of ....) . Is in freebasic any form of lists like in visual bsic

Post by geminis4941 »

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
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: List (of ....) . Is in freebasic any form of lists like in visual bsic

Post by MrSwiss »

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

...
cdavidson
Posts: 18
Joined: Sep 10, 2007 12:32

Re: List (of ....) . Is in freebasic any form of lists like in visual bsic

Post by cdavidson »

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.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: List (of ....) . Is in freebasic any form of lists like in visual bsic

Post by fxm »

You can also look at code for a Keyed Linked List by PaulSquires:
viewtopic.php?f=7&t=23902
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: List (of ....) . Is in freebasic any form of lists like in visual bsic

Post by St_W »

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
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: List (of ....) . Is in freebasic any form of lists like in visual bsic

Post by sancho2 »

cdavidson wrote:the FreeBASIC Extended Library http://ext.freebasic.net/ contained a list type
I've had problems trying to use the extended library, however the list does indeed work.
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 order to use the extended library download it and unzip the entire 'ext/' directory to your FB include directory. Many of the files are inter dependant and you don't want to get caught up in trying to write out include paths for them.
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)
Post Reply