Variable variables or Objects by name??

elck

Member
Licensed User
Longtime User
I wonder if b4a supports variable variables, let me explain:

Dim Label1 As Label
Dim a as string
a="Label1"

GetObjectByName(a).text="Here is the text for Label1"


Is there a way to do this?

Sorry if this was discussed earlier, I have searched!
 

elck

Member
Licensed User
Longtime User
Haha! Of course Label1.text works! ;-)
But isn't there something like:

for i=0 to AllObjects.count - 1
if AllObjects.getObject(i).name="Label1" then
....
end if
next
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can't do this either because the views are not known by their name in the system.

What exactly do you want to achieve with these methods ?

You could set the Tag property to the views name and then:
B4X:
Label1.Tag = "Label1"

For i = 0 To Activity.NumberOfViews - 1
    If Activity.GetView(i).Tag = "Label1" Then
        
    End If
Next
Best regards.
 
Upvote 0

elck

Member
Licensed User
Longtime User
Great, exactly what I was looking for, thanks!

Well of course I am experimenting with some dynamically created layout.
Don't know how far I will get with this, but so far it's working
Thanks again.
 
Upvote 0

elck

Member
Licensed User
Longtime User
I thought I was there, but now I think that what I was looking for does not exist

B4X:
'This does not work!!
Sub fillpagefromdb
Dim Cursor1 As Cursor,name As String,value As String,o As View
    Cursor1 = db.sql1.ExecQuery("SELECT * FROM table1")
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
        name=Cursor1.GetString("name")
      value=Cursor1.GetString("data")
      o=GetObjectFromTag(name)
      If (o) Then o.text=value
    Next
    Cursor1.Close
End Sub
Sub GetObjectFromTag(a As String) As View
Dim o As View
o=Null
For i = 0 To Activity.NumberOfViews - 1
    If Activity.GetView(i).Tag = a Then
        o=Activity.GetView(i)
      Continue
    End If
Next
Return o
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What does not work ?
This line looks strange to me :
B4X:
If (o) Then o.text=value
Perhaps I'm wrong, never tried this.
But without knowing what is not working I don't want to spend much time to guess what could be wrong.
Don't you have a small test project to test it ?

Best regards.


Best regards.
 
Upvote 0

elck

Member
Licensed User
Longtime User
This obviously does work, but somehow I find it less appealing
B4X:
Sub fillPageFromDb
Dim Cursor1 As Cursor,name As String,data As String
    Cursor1 = db.sql1.ExecQuery("SELECT * FROM table1")
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
        name=Cursor1.GetString("name")
      data=Cursor1.GetString("data")
      Select name
      Case "label1"
         Label1.Text=data
      Case "titel"
         Label2.text=data
      End Select
    Next
    Cursor1.Close
End Sub
I think the map in Erel's solution needs to be maintained manually, ie. there is no way to generate that map?!
 
Upvote 0

elck

Member
Licensed User
Longtime User

Views only have the 'shared properties of all views.'
So o.text does not exist. That's why this solution does not work.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
All this looks quite complicated to me.
What kind of data do you have in your database?
When I use a database I know what data is in there and design the interface according to tha data.
But it seems I am missing something here.

Best regards.
 
Upvote 0

elck

Member
Licensed User
Longtime User
Maybe I have been working with the HTML DOM object a bit too much in the last couple of years ;-)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…