Alternative to array

Dario126

Member
Licensed User
Longtime User
I have some array with custom type fields, totally 10 fields:

Type Node(Name As String, Q As Int, p As Int, vcalc As Float, vreal As Float, tStart As Long, tEnd As Long, tEndCalc As Long, LogDist As Int, NodeType As String)

Then this is used to define array:
Dim Nodes(20) As Node


This works fine, but problem is when there is need for redim this array to more or less nodes. If I re-dim it again by using following command, all data in existing nodes are lost:
Dim Nodes(21) As Node


As I understand List's and Map's are better regarding dynamic re-dimensioning, but it can not hold data structure as above, or am I wrong?

Is it better to use DB for this use?
 

sorex

Expert
Licensed User
Longtime User
You can use a list (which is like an array) or a map

here's how you can redim/reinitialize the list

B4X:
Dim nodes As List
nodes.Initialize 
nodes.Add("aaa")
nodes.Add("bbb")
nodes.Initialize
nodes.Add("aaa")
Msgbox(nodes.size & " - " & nodes.Get(0),"")

returns 1 - aaa


bad part is that it trashes all "content" during the init,
good thing is that it had a removeat command which can be seen as a redim.

B4X:
Dim nodes As List
nodes.Initialize 
nodes.Add("aaa")
nodes.Add("bbb")
'nodes.Initialize
nodes.RemoveAt(0)
Msgbox(nodes.size & " - " & nodes.Get(0),"")

returns 1 - bbb


in your case you probably are better of with sqlite instead multiple lists
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
As I understand List's and Map's are better regarding dynamic re-dimensioning, but it can not hold data structure as above, or am I wrong?

A B4A List is a List of type Object.
It sounds like you want a List of type Node and that is not supported in B4A.

You can create a List and add instances of type Node:

B4X:
Dim MyNodes As List
MyNodes.Initialize

Dim Node1 As Node
Node1.Initialize
MyNodes.Add(Node1)

Dim Node2 As Node
Node2.Initialize
MyNodes.Add(Node)

Then when you get a Node from the List you have to assign it to an instance of Node:

B4X:
Dim MyNode As Node
MyNode=MyNodes.Get(0)
Log(MyNode.Name)

But you can't avoid assigning the Object you get from a List to a Node, this won't work:

B4X:
Log(MyNodes.Get(0).Name)

So a List will work but involved extra code to assign the Object you get from a List to an instance of the required type.

Can't say whether a database would be 'better' as it all depends on your application.

Martin.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
what is this node type? when I dim something there is no node type to select?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I have some array with custom type fields, totally 10 fields:

Type Node(Name As String, Q As Int, p As Int, vcalc As Float, vreal As Float, tStart As Long, tEnd As Long, tEndCalc As Long, LogDist As Int, NodeType As String)

It's a custom type.

Martin.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
right, used it in FreeBASIC but didn't know B4A also supported this. Good thing to know.
 
Upvote 0
Top