techknight Well-Known Member Licensed User Longtime User Dec 12, 2018 #1 I am attempting to do a 2 dimensional label array (think of a grid of labels). But it doesnt work. Here is my feeble attempt: B4X: Expand Collapse Copy '2 Dimensional Array Sub CollectNodes2D(Tags() As String) As Label() Dim TagsIndex As Int Dim lblsList(Tags.length) As List Dim MaxListSize As Int For TagsIndex = 0 To Tags.Length-1 lblsList(TagsIndex).Initialize For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1 Dim n As Node = OutputWindow.RootPane.GetNode(I) If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n) Next If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it. Next Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array For TagsIndex = 0 To Tags.Length - 1 For i = 0 To lbls.Length - 1 Dim lbl As Label = lblsList(TagsIndex).Get(i) lbls(TagsIndex, i) = lbl Next Next Return lbls End Sub But it gets an error: main.java:402: error: incompatible types: LabelWrapper[][] cannot be converted to LabelWrapper[] if (true) return (anywheresoftware.b4j.objects.LabelWrapper[])(_lbls); Thoughts?
I am attempting to do a 2 dimensional label array (think of a grid of labels). But it doesnt work. Here is my feeble attempt: B4X: Expand Collapse Copy '2 Dimensional Array Sub CollectNodes2D(Tags() As String) As Label() Dim TagsIndex As Int Dim lblsList(Tags.length) As List Dim MaxListSize As Int For TagsIndex = 0 To Tags.Length-1 lblsList(TagsIndex).Initialize For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1 Dim n As Node = OutputWindow.RootPane.GetNode(I) If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n) Next If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it. Next Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array For TagsIndex = 0 To Tags.Length - 1 For i = 0 To lbls.Length - 1 Dim lbl As Label = lblsList(TagsIndex).Get(i) lbls(TagsIndex, i) = lbl Next Next Return lbls End Sub But it gets an error: main.java:402: error: incompatible types: LabelWrapper[][] cannot be converted to LabelWrapper[] if (true) return (anywheresoftware.b4j.objects.LabelWrapper[])(_lbls); Thoughts?
stevel05 Expert Licensed User Longtime User Dec 12, 2018 #2 Can't test it but I would think that the sub signature should be: B4X: Expand Collapse Copy Sub CollectNodes2D(Tags() As String) As Label(,) Upvote 0
Can't test it but I would think that the sub signature should be: B4X: Expand Collapse Copy Sub CollectNodes2D(Tags() As String) As Label(,)
techknight Well-Known Member Licensed User Longtime User Dec 13, 2018 #3 That did work, but a new problem surfaced. the .Length function. the I = 0 to lbls.Length now is broken because its a 2D array. Problem is, I am unsure how to fix that. Upvote 0
That did work, but a new problem surfaced. the .Length function. the I = 0 to lbls.Length now is broken because its a 2D array. Problem is, I am unsure how to fix that.
Erel B4X founder Staff member Licensed User Longtime User Dec 13, 2018 #4 It will be simpler to return a List with arrays of labels as items. Upvote 0
techknight Well-Known Member Licensed User Longtime User Dec 13, 2018 #5 Not sure how to do that. But after a little bit of thinking, I came to this and it worked finally: B4X: Expand Collapse Copy Sub CollectNodes2D(Tags() As String) As Label(,) Dim TagsIndex As Int Dim lblsList(Tags.length) As List Dim MaxListSize As Int Dim ListSize(Tags.Length) As Int For TagsIndex = 0 To Tags.Length-1 lblsList(TagsIndex).Initialize For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1 Dim n As Node = OutputWindow.RootPane.GetNode(I) If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n) Next ListSize(TagsIndex) = lblsList(TagsIndex).Size 'Grab our list size for this particular Tag. If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it. Next Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array For TagsIndex = 0 To Tags.Length-1 For i = 0 To ListSize(TagsIndex) - 1 Dim lbl As Label = lblsList(TagsIndex).Get(i) lbls(TagsIndex, i) = lbl Next Next Return lbls End Sub It may not be the best or most efficient piece of code out there, many ways to skin the cat, but it works for what I needed it for. I figured I would post it in case someone else wants to use it. The reason I needed to do that, is so I could do this: B4X: Expand Collapse Copy lblMats = CollectNodes2D(Array As String("1", "2", "3", "4", "5", "6", "7", "8")) lblMats(7,4).Text = "Hello" Upvote 0
Not sure how to do that. But after a little bit of thinking, I came to this and it worked finally: B4X: Expand Collapse Copy Sub CollectNodes2D(Tags() As String) As Label(,) Dim TagsIndex As Int Dim lblsList(Tags.length) As List Dim MaxListSize As Int Dim ListSize(Tags.Length) As Int For TagsIndex = 0 To Tags.Length-1 lblsList(TagsIndex).Initialize For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1 Dim n As Node = OutputWindow.RootPane.GetNode(I) If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n) Next ListSize(TagsIndex) = lblsList(TagsIndex).Size 'Grab our list size for this particular Tag. If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it. Next Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array For TagsIndex = 0 To Tags.Length-1 For i = 0 To ListSize(TagsIndex) - 1 Dim lbl As Label = lblsList(TagsIndex).Get(i) lbls(TagsIndex, i) = lbl Next Next Return lbls End Sub It may not be the best or most efficient piece of code out there, many ways to skin the cat, but it works for what I needed it for. I figured I would post it in case someone else wants to use it. The reason I needed to do that, is so I could do this: B4X: Expand Collapse Copy lblMats = CollectNodes2D(Array As String("1", "2", "3", "4", "5", "6", "7", "8")) lblMats(7,4).Text = "Hello"