B4R Question Use a type variable in another type variable

Chris160179

Member
Licensed User
Longtime User
Hello everybody,

it seems it is not possible to use a type variable in another type variable.
Like this:
B4X:
    Type WLAN_Type (IP As String, SSID As String, Password As String, Chanel As Byte, BSSID As String)
    Type Settings(Resets As UInt, Summertime As Boolean, WLAN As WLAN_Type)
    Public Settings1 As Settings
    Settings1.WLAN.IP = "192.168.x.x"
I can compile the code, but the ESP8266 crashes immediately. If i set the variable Settings1.WLAN.IP = "192.168.x.x".
The code is simplified here, it should be nested even more often. So that I can then write the complete settings block with the B4R serializer as a byte array into the eeprom.
and i woud like to do somthing like that:
Settings.Room.Floor.Sensor_ID = 1

I used this method instead:
B4X:
Sub Process_Globals
    Type WLAN_Type (IP As String, SSID As String, Password As String, Chanel As Byte, BSSID As String)
    Type Einstellungen (Resets As UInt, Sommerzeit As Boolean, WLAN() As Byte)
    Public Settings As Einstellungen
    Private WLAN1 As WLAN_Type
    Settings.Resets = 0
    Settings.Sommerzeit = True
    WLAN1.IP = "192.168.0.170"
    WLAN1.Chanel = 1

    Private ser As B4RSerializator
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    Dim obj As Object = WLAN1
    Settings.WLAN = ser.ConvertArrayToBytes(obj)
    Dim be(30) As Object              'Count of Objects (not Bytes) for the serializator
    Dim objects() As Object = ser.ConvertBytesToArray(Settings.WLAN, be) 'Wandeln des Byte Arrays in Objecte Array
    WLAN1.IP = objects(0)
End Sub
It works, but i´m not happy with that.

Am I doing something wrong?
Does anyone have a better idea?

Thanks and regards
Chris
 

Chris160179

Member
Licensed User
Longtime User
Hello Erel,
hello everybody,

@Erel:
thank you for the quick help.
I checked your Code and it works as expected.


My code works also:
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    
    Type System_Type (Resets As UInt, Sommerzeit As Boolean)
    Type WLAN_Type (IP As String, SSID As String, Password As String, Chanel As Byte, BSSID As String)
    Type Einstellungen (System As System_Type, WLAN As WLAN_Type)
    Public Settings As Einstellungen
    Private System1 As System_Type
    Private WLAN1 As WLAN_Type
    Settings.System = System1
    Settings.WLAN = WLAN1
    Settings.System.Resets = 0
    Settings.System.Sommerzeit = True
    Settings.WLAN.IP = "192.168.0.170"   
    Settings.WLAN.SSID = "Wireless1"
    Settings.WLAN.Password = "@Oktober160"
    Settings.WLAN.Chanel = 1
    Settings.WLAN.BSSID = "CCCE1E1DC20B"
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    'Debug complete
    Log ("Settings.System.Resets = 0: ", Settings.System.Resets)
    Log ("Settings.System.Sommerzeit = True: ", Settings.System.Sommerzeit)
    Log ("WLAN.IP = ""192.168.0.170"": = " , Settings.WLAN.IP)
    Log ("WLAN.SSID = ""Wireless1"": = " , Settings. WLAN.SSID)
    Log ("WLAN.Password = ""@Oktober160"": = " , Settings.WLAN.Password)
    Log ("WLAN.Chanel = 1: = " , Settings.WLAN.Chanel)
    Log ("WLAN.BSSID = ""CCCE1E1DC20B"": = " , Settings.WLAN.BSSID)
    'Debug
    Log ("1. WLAN1.IP = ""192.168.0.170"": ", Settings.WLAN.IP)
    'Change the IP
    Settings.WLAN.IP = "192.168.0.171"
    Log ("2. WLAN1.IP = ""192.168.0.171"": ", Settings.WLAN.IP)
'    test(0)                                                                            'It works, because the Sub "AppStart" in not finished and the Variables are not destroyed
    CallSubPlus ("test", 100, 0)                                                   
End Sub


Sub test (Tag As Byte)
    Log ("3. WLAN1.IP = ""192.168.0.171"": ", Settings.WLAN.IP)
End Sub

Output:
B4X:
AppStart
Settings.System.Resets = 0: 0
Settings.System.Sommerzeit = True: 1
WLAN.IP = "192.168.0.170": = 192.168.0.170
WLAN.SSID = "Wireless1": = Wireless1
WLAN.Password = "@Oktober160": = @Oktober160
WLAN.Chanel = 1: = 1
WLAN.BSSID = "CCCE1E1DC20B": = CCCE1E1DC20B
1. WLAN1.IP = "192.168.0.170": 192.168.0.170
2. WLAN1.IP = "192.168.0.171": 192.168.0.171
3. WLAN1.IP = "192.168.0.171": 192.168.0.171


But:

This Code did not work:
I only changed the Log line.
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    
    Type System_Type (Resets As UInt, Sommerzeit As Boolean)
    Type WLAN_Type (IP As String, SSID As String, Password As String, Chanel As Byte, BSSID As String)
    Type Einstellungen (System As System_Type, WLAN As WLAN_Type)
    Public Settings As Einstellungen
    Private System1 As System_Type
    Private WLAN1 As WLAN_Type
    Settings.System = System1
    Settings.WLAN = WLAN1
    Settings.System.Resets = 0
    Settings.System.Sommerzeit = True
    Settings.WLAN.IP = "192.168.0.170"   
    Settings.WLAN.SSID = "Wireless1"
    Settings.WLAN.Password = "@Oktober160"
    Settings.WLAN.Chanel = 1
    Settings.WLAN.BSSID = "CCCE1E1DC20B"
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    'Debug complete
    Log ("Settings.System.Resets = 0: ", Settings.System.Resets)
    Log ("Settings.System.Sommerzeit = True: ", Settings.System.Sommerzeit)
    Log ("WLAN.IP = ""192.168.0.170"": = " , Settings.WLAN.IP)
    Log ("WLAN.SSID = ""Wireless1"": = " , Settings. WLAN.SSID)
    Log ("WLAN.Password = ""@Oktober160"": = " , Settings.WLAN.Password)
    Log ("WLAN.Chanel = 1: = " , Settings.WLAN.Chanel)
    Log ("WLAN.BSSID = ""CCCE1E1DC20B"": = " , Settings.WLAN.BSSID)
    'Debug
    Log ("1. WLAN1.IP = ""192.168.0.170"": ", Settings.WLAN.IP)
    'Change the IP 
    Settings.WLAN.IP = "192.168.0.171"
'   Log ("2. WLAN1.IP = ""192.168.0.171"": ", Settings.WLAN.IP)
'   test(0)                                                                            'It works, because the Sub "AppStart" in not finished and the Variables are not destroyed
    CallSubPlus ("test", 100, 0)                                                   
End Sub


Sub test (Tag As Byte)
    Log ("3. WLAN1.IP = ""192.168.0.171"": ", Settings.WLAN.IP)
End Sub

Output:
B4X:
AppStart
Settings.System.Resets = 0: 0
Settings.System.Sommerzeit = True: 1
WLAN.IP = "192.168.0.170": = 192.168.0.170
WLAN.SSID = "Wireless1": = Wireless1
WLAN.Password = "@Oktober160": = @Oktober160
WLAN.Chanel = 1: = 1
WLAN.BSSID = "CCCE1E1DC20B": = CCCE1E1DC20B
1. WLAN1.IP = "192.168.0.170": 192.168.0.170
3. WLAN1.IP = "192.168.0.171":

I do not understand that.
Usually it is not possible to change global (non-primitive) variables.

1. Can I change the global variables now if they have been declared as type variable or not?

2. Is there a way how I can change global string variables?

3. If I use byte arrays instead of string variables for the IP, can I change the IP within a type variable?
Somthing like this:

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial

    Type System_Type (Resets As UInt, Sommerzeit As Boolean)
    Type WLAN_Type (IP() As Byte, SSID() As Byte, Password() As Byte, Chanel As Byte, BSSID() As Byte)
    Type Einstellungen (System As System_Type, WLAN As WLAN_Type)
    Public Settings As Einstellungen
    Private System1 As System_Type
    Private WLAN1 As WLAN_Type
    Settings.System = System1
    Settings.WLAN = WLAN1
    Settings.System.Resets = 0
    Settings.System.Sommerzeit = True
    'set dafault
        Settings.WLAN.IP = "192.168.0.170"   
        Settings.WLAN.SSID = "Wireless1"
        Settings.WLAN.Password = "@Oktober160"
        Settings.WLAN.Chanel = 1
        Settings.WLAN.BSSID = "CCCE1E1DC20B"
        'or somthing like this
'        Settings.WLAN.IP = Array As Byte(192, 168, 0, 170)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    Log ("1. WLAN.IP = ""192.168.0.170"": = " , Settings.WLAN.IP)
    'change
    Settings.WLAN.IP = "192.168.0.171"   
    Log ("2. WLAN.IP = ""192.168.0.170"": = " , Settings.WLAN.IP)
'    test(0)                                                                            'It works, because the Sub "AppStart" in not finished and the Variables are not destroyed
    CallSubPlus ("test", 100, 0)
End Sub


Sub Test (Tag As Byte)
    Log ("3. WLAN.IP = ""192.168.0.170"": = " , Settings.WLAN.IP)
End Sub

According to the output of the logs, it works.
But does it really work?

Thanks a lot
Chris
 
Upvote 0

Chris160179

Member
Licensed User
Longtime User
The purpose of the project is as follows:
I load settings from an SQL database into the global variables and would then like to use and change the global variables in the program.
Later the data are either saved in EEprom or written back to the SQL database.
 
Upvote 0

Chris160179

Member
Licensed User
Longtime User
Hello Erel,

OK thank you.
I am testing the GlobalStore for my application.

I had experimented with it in the past, but I wasn't sure if it would work the way I needed it to.

greeting
Chris
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…