Android Question [Solved ]Array elements are not equivalent to the ojects they reference

Martincg

Member
Licensed User
Longtime User
B4X:
Dim arLblTemps() as AutoTextSizeLabel

arLblTemps = Array As AutoTextSizeLabel (lblGiteT, lblPoolT, lblOutsideT, lblFreezerT, lblBungT)
   
'These next 4 lines work OK   
   lblGiteT.width = 180 * scalex
   lblGiteT.height = 40 * scaley
   lblGiteT.left = 23 * scalex
   lblGiteT.top = 317 * scaley
   
   
  'These lines fail with "object must be initialized" 
   For i = 0 To arLblTemps.length - 1
       arLblTemps(i).width = 180 * scalex  'fails with i = 0
       arLblTemps(i).height = 40 * scaley
   Next

Why does not arLblTemps(0) behave the same as lblGiteT?
I tried adding
arLblTemps(i).Initialize(Panel1,"jj")
at the start of the for loop but no different.
 

emexes

Expert
Licensed User
Based on your description, presumably the "Array As AutoTextSizeLabel (lblGiteT, lblPoolT, lblOutsideT, lblFreezerT, lblBungT)" object is being created once at initialization, and then the "arLblTemps =" assignment is just setting arLblTemps to point to that object, rather than creating a new array with pointers to the 5 discrete variables. In fact, that makes me wonder if the initialized-once Array objects can be changed, because if they can, then the next time the "arLblTemps =" is done, it would be to an object of different value. The Java output from the B4A compiler might clarify precisely what is being done.

If you need to get your code working right now*, you could try:
B4X:
Dim arLblTemps(5) as AutoTextSizeLabel

arLblTemps(0) = lblGiteT
arLblTemps(1) = lblPoolT
arLblTemps(2) = lblOutsideT
arLblTemps(3) = lblFreezerT
arLblTemps(4) = lblBungT

Log("Before = " & arLblTemps(0))

'if these lines work
lblGiteT.width = 180 * scalex
lblGiteT.height = 40 * scaley
lblGiteT.left = 23 * scalex
lblGiteT.top = 317 * scaley

Log("After = " & arLblTemps(0))

'then these should work too (at least for element 0 aka lblGiteT)
For i = 0 To arLblTemps.length - 1
    Log("Before " & i & " = " & arLblTemps(i).Width)
    arLblTemps(i).width = 180 * scalex
    arLblTemps(i).height = 40 * scaley
    Log("After " & i & " = " & arLblTemps(i).Width)
Next


* rather than waiting for a reply from an expert who *knows* what is going on
 
Upvote 0

Martincg

Member
Licensed User
Longtime User
Thank you for your suggestion emexes. I am a beginner and a lot of things are not clear. If what you suggests works it will be good but no less confusing for me. I will try it later but meanwhile I have changed the Autotextsizelabels fpr standard label so I can progress, though I would like to understand more.
(With standard labels the approach works fine, though I have to calculate what text size to use.)
 
Upvote 0

emexes

Expert
Licensed User
If what you suggests works it will be good but no less confusing for me.
I agree that your technique from post #1 looks like it should work. I don't have an Android phone handy, so instead I tried doing something similar in B4J (which uses Java just like B4A, and operates in much the same way) and it all worked like you expected.

So now I am thinking: is it possible that you ran your "using variables" and "using array" examples separately, and the LoadLayout was skipped for the "using array" example? That would explain the variables not being initialized when you ran the "using array" example.

Anyway, what I did (in B4J) was:
B4X:
Sub Process_Globals
   
    Private fx As JFX
    Private MainForm As Form
   
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
   
    Label1.Text = "One"
    Label2.Text = "Two"
    Label3.Text = "Three"
    Label4.Text = "Four"
    Label5.Text = "Five"
   
    MainForm.Show
   
    Sleep(1000)
   
    Dim LabelArray() As Label = Array As Label(Label1, Label2, Label3, Label4, Label5)
   
    For I = 1 To LabelArray.Length - 1
        LabelArray(I).Left = LabelArray(0).Left
    Next   
   
    Sleep(1000)
       
    For I = 0 To LabelArray.Length - 1
        LabelArray(I).Alignment = "CENTER_LEFT"
    Next
   
    Sleep(1000)
   
    Dim GermanNumberWord() As String = Array As String("Ein", "Zwei", "Drei", "Vier", "Fünf")
   
    For I = 0 To LabelArray.Length - 1
        LabelArray(I).Text = GermanNumberWord(I)
    Next
               
End Sub
and it worked as expected.
 
Upvote 0

Martincg

Member
Licensed User
Longtime User
I replaced the autoTextSizeLabels with labels and my code worked so I guess it is something to do with the AutoTextSizeLabel class but I can't see what. There is a function in the class called initialize but it is an empty sub.

Thanks for the other information. I thought I could count to ten in German but I see I had got the word for five wrong. For the last 50 years I believed it was something else, maybe because my hearing is not good. I looked up what I thought it was and I see that it is a rude expression, so it's good that I now know better.
 
Upvote 0

emexes

Expert
Licensed User
I guess it is something to do with the AutoTextSizeLabel class but I can't see what.
That is indeed curious. Does the problem still occur if you fill the array "manually" rather than using "= Array As" ?

I thought I could count to ten in German but I see I had got the word for five wrong. For the last 50 years I believed it was something else
Lol. That happens to me about once a month too, usually when watching tv quiz programs where I find that some phrase that I'd "learned" from context to mean one thing, actually means something completely different. But my memory is about par with your hearing, and prevents me from citing a specific example just now ;-)
 
Upvote 0

Martincg

Member
Licensed User
Longtime User
I'm afraid I've done no more than make a note of your suggestion in my program, and I've been busy developing it. Because I deleted the AutoTextSizeLabels it is possible that the error was due to some spelling mistake that I couldn't see and which I won't be able to reproduce. I say that because today I had a similar error and it was due to the fact that the view I used was still declared but had it's spelling corrected in the designer which had then been used to generate the new view, but the program still had a reference to the old name. I'm going to mark this as solved and assume it was something like that.
 
Upvote 0
Top