B4R Question Changing one variable changes another one

pecket

Member
Hi.

I'm asking here since this probably is just related to the different handling of variables in B4R compared to the rest of the products. I'm having two different variables, with *slightly* different content. Despite this, if I change one of them, the other one changes too. If I however assign completely different values initially to them, this problem doesn't occur. It seems the compiler somehow interprets them as linked/identical despite not even having matching strings. Looks like it ignores any characters and only looks at the numbers.
An example where the problem occurs:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public Testnr1() As Byte = "000000"
    Public Testnr2() As Byte = "!!!!!!!!!!!!!!!!!!!!000000"   'The exclamation marks is not a typo. Same problem occurs with hyphens or letters too by the way. The amount is just for show, 1 or 20 doesn't matter.
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Log("Testnr1: ", Testnr1)
    Log("Testnr2: ", Testnr2)
    For i = 0 To 5
        Testnr1(i) = Rnd(48,57)   'Btw, how can I do a simple random 0-9 and assign as a character?
        Testnr2(i+20) = Rnd(48,57)   'Tried several solutions but ended up with using ASCII. In this case it works, but in others it wouldn't.
    Next
    Log("Testnr1: ", Testnr1)
    Log("Testnr2: ", Testnr2)   
End Sub

This would give me the result:
AppStart
Testnr1: 000000
Testnr2: !!!!!!!!!!!!!!!!!!!!000000
Testnr1: 728818
Testnr2: !!!!!!!!!!!!!!!!!!!!728818

On the other hand, if I change the last bytes of the initial value of these variable a bit further (by initiating Testnr1() with "000001" instead for example), this problem does not occur:
AppStart
Testnr1: 000001
Testnr2: !!!!!!!!!!!!!!!!!!!!000000
Testnr1: 454680
Testnr2: !!!!!!!!!!!!!!!!!!!!728818

So it seems that it's the first characters in the byte array is not taken into consideration by the compiler. Correct me if I'm wrong. I just spent a whole day trying to figure out what was happening in my code. On the plus side, I got to tidy up some of my leftover spaghetti quite a bit.
 

pecket

Member
But are there any risks with this, as long as I'm very aware of what I'm doing? Still sounds strange that it mixes these together, despite them being completely different. Saves some RAM though I guess.
 
Upvote 0

pecket

Member
Yeah, you're right of course. I'll update my code to use this instead. Was just wondering if there was any way to make the variables data "non-shared" when assigning them initially. Felt more handy, but if there's any risks with writing to the data, even if they're initially different as well, then I'll use GlobalStore/Queue/Stack instead. Thanks mate!
 
Upvote 0
Top