D Dario126 Member Licensed User Longtime User Feb 28, 2013 #1 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?
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?
S sorex Expert Licensed User Longtime User Feb 28, 2013 #2 You can use a list (which is like an array) or a map here's how you can redim/reinitialize the list Code: Copy 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. Code: Copy 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
You can use a list (which is like an array) or a map here's how you can redim/reinitialize the list Code: Copy 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. Code: Copy 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
warwound Expert Licensed User Longtime User Feb 28, 2013 #3 Dario126 said: 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? Click to expand... 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: Code: Copy 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: Code: Copy 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: Code: Copy 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
Dario126 said: 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? Click to expand... 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: Code: Copy 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: Code: Copy 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: Code: Copy 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.
S sorex Expert Licensed User Longtime User Mar 1, 2013 #4 what is this node type? when I dim something there is no node type to select? Upvote 0
warwound Expert Licensed User Longtime User Mar 1, 2013 #5 Dario126 said: 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) Click to expand... It's a custom type. Martin. Upvote 0
Dario126 said: 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) Click to expand... It's a custom type. Martin.
S sorex Expert Licensed User Longtime User Mar 1, 2013 #6 right, used it in FreeBASIC but didn't know B4A also supported this. Good thing to know. Upvote 0
D Dario126 Member Licensed User Longtime User Mar 1, 2013 #7 Thank you warwound and sorex. Both posts are healpfull .. :sign0098: Upvote 0