Android Question Byte Array within type - compare not working

Arf

Active Member
Licensed User
I've got a type that contains some byte arrays, with MAC addresses in them:
B4X:
Type UnitMACStruct(PCmac(12) As Byte, TABLETmac(12) As Byte)
Dim UnitInfo As UnitInfoStruct
Dim ThisMAC(12) As Byte
ThisMAC = bc.StringToBytes(serial1.Address.Replace(":",""),"UTF8")
So ThisMAC is a 12 byte array containing a MAC address with :'s removed.
I know that the contents of UnitInfo.TABLETmac contains the same MAC address, although if I look at the one byte array in the watch window, the data is represented in a different way, I guess due to being within a type.

Problem is, when I do this:
B4X:
If ThisMAC <> UnitMAC.TABLETmac Then
the test always comes out true, indicating the array contents are different.
They are the same though - if I extract both array contents to strings, the strings are the same.

I could just compare the strings, but in the interest of keeping things simpler an understanding whats occurring, can anyone see why the test above is not working?
 

Troberg

Well-Known Member
Licensed User
My guess: You are not comparing the contents of the arrays, you are comparing the object references to the arrays. As these are two different arrays containing the same data, the test will come out as not equal.

Just for fun, try comparing this:

Dim ThatMac() as byte = ThisMac
If ThisMac=ThatMac then...

I'll bet that it will evaluate as true, as you are then comparing the same object.

In other words, you need to manually compare the contents.

Also, it's a 13 byte array. Remember that it's 0-based.
 
  • Like
Reactions: Arf
Top