I need help on ArrayList and AddArrayList

claudio

Member
Licensed User
Longtime User
:sign0085:

I have this code:
'-----------------------------------------------------
Sub Globals
Dim c(0)
Dim p(0)
End Sub

Sub bTest_Click

AddArrayList("al")

c() = GetControls("Main.Form1")
al.add(c())
Msgbox(c(0))
c() = GetControls("fTest")
al.add(c())
Msgbox(c(0))

p() = al.item(0)
Msgbox(p(0))
p() = al.item(1)
Msgbox(p(0))

End Sub
'-----------------------------------------------------

There are 2 problems:

1 - if i call this sub twice, the second time i get an error because
"al" is already instanced, there are a way to know it or i must put a
flag?

2 - I add to "al" 2 different arrays, but when i retrive the values with
p() = al.item(0) and p() = al.item(1) i get the same result. Why?

thanks
claudio
 

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do?

1) The AddArrayList("al") must be in the App_Start routine, so it is instanced only one time.

2) Do you need to check the controls for each Form ?
If you want the Controls for all Forms you schould use
c() = GetControls("")

3) al.Add(c()) doesn't do anything, you can only add single values.
If you replace in the first MsgBox line
MsgBox(c(0)) by MsgBox(al.Item(0) you will see nothing

Best regards.
 

claudio

Member
Licensed User
Longtime User
What exactly do you want to do?

1) The AddArrayList("al") must be in the App_Start routine, so it is instanced only one time.

2) Do you need to check the controls for each Form ?
If you want the Controls for all Forms you schould use
c() = GetControls("")

3) al.Add(c()) doesn't do anything, you can only add single values.
If you replace in the first MsgBox line
MsgBox(c(0)) by MsgBox(al.Item(0) you will see nothing

Best regards.

1) ok, but for example AddButton() has the Form argument so i can search it at runtime with GetControls() function, with AddArrayList i have no way to inspect it at runtime.

What i'm tryng to do is to add arrays or structures to ArrayList to simulate dynamic arrays.

But it seams which when i add more then once the c() array to the ArrayList i change each item, in other words the array is passed by reference and there are no way to add dynamically created arrays.

Best regards
Claudio
 

klaus

Expert
Licensed User
Longtime User
1) ok, but for example AddButton() has the Form argument so i can search it at runtime with GetControls() function, with AddArrayList i have no way to inspect it at runtime.
What do you mean with 'inspect it at runtime' ?

Could you post your test sbp file, so we see better what you are doing.

Best regards.
 

claudio

Member
Licensed User
Longtime User
What do you mean with 'inspect it at runtime' ?

Could you post your test sbp file, so we see better what you are doing.

Best regards.

For "inspect at runtime" i mean to test if i have a variable instanced. For example when i add a button to a form with AddButton("Form1","Button1") i can find it with

B4X:
controls() = GetControls("Form1")
For i = 0 To ArrayLen(controls())-1
  If controls(i).Name = "Button1" Then 
    msgbox("Yeah! found it")
  End If
Next

But there is nothing similar for ArrayList.

As you can see, i'm still exploring the best practices in using b4ppc, thanks for help.

Claudio
 

klaus

Expert
Licensed User
Longtime User
If you use
controls() = GetControls("")
you will also get the ArrayList.

Attached a small program to show it.

Sorry, but I still don't understand the reason for the "inspecting at runtime".

Best regards.
 

Attachments

  • ArrayList.sbp
    1 KB · Views: 162

Zenerdiode

Active Member
Licensed User
B4X:
controls() = GetControls("Form1")
For i = 0 To ArrayLen(controls())-1
  If controls(i).Name = "[COLOR="Red"]Button1[/COLOR]" Then 
    msgbox("Yeah! found it")
  End If
Next

Please be aware that any control name returned by Basic4PPC is always all in lower case - regardless if you use capitals adding your control at design time or runtime. Try:

B4X:
...
  If controls(i).Name = "[COLOR="Red"]b[/COLOR]utton1" Then 
...
 

claudio

Member
Licensed User
Longtime User
If you use
controls() = GetControls("")
you will also get the ArrayList.

Attached a small program to show it.

Sorry, but I still don't understand the reason for the "inspecting at runtime".

Best regards.

Ok, the whole thing is if i can use ArrayList as a full functional dynamic array as i can find in other languages. As i can see, i can't, no problem i will find other ways to do things.

About "inspecting at runtime", is a common programming technique in dynamic languages, is called introspection.

Sometimes is useful, for example if i can retrive the names of controls i can automatically load fields from database record into textfields with the same name of db fields and at the end save back only the modified fields.

Fortunately i can do this task wit b4ppc and i really appreciated the quick and easy development cycle, but would be nice to have some more power in other fields like arrays usage and arrays as local variables.

Thanks for your help
Best regards.
Claudio
 
Top