Nested Types

CharlesIPTI

Active Member
Licensed User
Longtime User
So how deep can you nest custom types ?

I have an enum for color B4A style I want to make that a type for another custom type...

Current attempt doesnt recognize colors


PHP:
Type t_deviceStatusColor (Blue As Colors, Red As Colors, Orange As Colors, Green As Colors)
Dim devStatusColor As t_deviceStatusColor
devStatusColor.Blue = Colors.RGB(0,0,200):devStatusColor.Red = Colors.RGB(200,0,0):devStatusColor.Orange = Colors.RGB(255,100,0):devStatusColor.Green = Colors.RGB(0,200,0)


Type t_deviceStatusObject(deviceName As String, isActive As Boolean, _
                                    statusColor As t_deviceStatusColor, val3 As String, val4 As String, _
                                    val5 As String, val6 As String)



Original before I wanted to make the colors part of the other type:
PHP:
   Type t_deviceStatusObject(deviceName As String, _
                                          isActive As Boolean, _
                                          val2 As String, _
                                          val3 As String, _
                                          val4 As String, _                     
                                          val5 As String, _
                                          val6 As String)



         Type t_deviceStatusColor (Blue As Int, Red As Int, Orange As Int, Green As Int)
         Dim devStatusColor As t_deviceStatusColor
         devStatusColor.Blue = 0:devStatusColor.Red = 1:devStatusColor.Orange = 2:devStatusColor.Green = 3
 
Last edited:

CharlesIPTI

Active Member
Licensed User
Longtime User
pic

When the color is listed as a string it is a selectable property but when it is a type it doesn't even show up in the properties of the type
 

Attachments

  • Capture.JPG
    Capture.JPG
    46.5 KB · Views: 255
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Addendum Discovery

Create the Magical B4A Enum type as described in the enum post then

try to use that enum type in another code module you have to define its defaults AGAIN in that new module your using it in... the values have to be set again or you just get zeros

You make the type like this
PHP:
Type t_deviceStatusColor (Blue As Int, Red As Int, Orange As Int, Green As Int)

But you have to do this in every module
PHP:
         Dim devStatusColor As t_deviceStatusColor
         devStatusColor.Blue = 0:devStatusColor.Red = 1:devStatusColor.Orange = 2:devStatusColor.Green = 3
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
??? Nested FollowUp

Are you saying --devStatusColor--- should bee seen in other modules because its not recognized. You have to make a local instance of t_deviceStatusColor and init the default values or they are all zero


B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.


Type t_orderBinsControl(lstObO As List) 
Type t_orderBinObj (panelObj As Panel, lstLbls As List)

   Type t_deviceStatusObject(deviceName As String, _
                                          isActive As Boolean, _
                                          statusColor As String, _   '  Desired As t_deviceStatusColor but it FAILs !! 
                                          valueThree As String, _
                                          valueFour As String, _                     
                                          valueFive As String, _
                                          valueSix As String)


Type t_deviceStatusColor (Blue As Int, Red As Int, Orange As Int, Green As Int)

         Dim devStatusColor As t_deviceStatusColor
         devStatusColor.Blue = 0:devStatusColor.Red = 1:devStatusColor.Orange = 2:devStatusColor.Green = 3


End Sub
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Yes Yes

I recall your are correct I re tested and confirmed, this.

but what about my attempt as nesting this type into another type ?
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Something Missing

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

Type t_deviceStatusColor (Blue As Int, Red As Int, Orange As Int, Green As Int)

         Dim devStatusColor As t_deviceStatusColor
         devStatusColor.Blue = 0:devStatusColor.Red = 1:devStatusColor.Orange = 2:devStatusColor.Green = 3

Type t_orderBinsControl(lstObO As List) 
Type t_orderBinObj (panelObj As Panel, lstLbls As List)

Type t_deviceStatusObject(deviceName As String, _
isActive As Boolean, _
statusColor As '''' --- >>> Do I use the t_ Type Name or the instance of that type here   t_deviceStatusColor OR  devStatusColor   , _
valueThree As String, _
valueFour As String, _                     
valueFive As String, _
valueSix As String)

Dim devStatObj As t_deviceStatusObject

End Sub

'''' --- >>> I Have of course tried both ways cited above and the "property" statusColor never appears unless I make it a plain string
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You need to declare statusColor with the name of the type. Not the name of any specific instance of this type:
SS-2012-05-21_17.43.22.png


However the value of the fields will be 0 (there is no relation between the type and any specific instance).

If you want to create an enum then you should do something like:
B4X:
Sub Process_Globals
   Type enum_t (Red As Int, Green As Int, Blue As Int)
   Dim MyEnum As enum_t
   MyEnum.Red = 0 'or any other value you need
   MyEnum.Green = 1
   MyEnum.Blue = 2
   'You should not use enum_t in any other place in your code.
   'You should refer to MyEnum.
   Type ComplexType (a As String, color As Int)
   Dim ct1, ct2 As ComplexType
   ct1.color = MyEnum.Red
   ct2.color = MyEnum.Blue
End Sub
 
Upvote 0

MbedAndroid

Well-Known Member
Licensed User
Longtime User
Nested Types should be allowed, but when i try this it compiles correctly, but gives a runtime error on the device
"null pointer exeption" in the sub activity create

If i dont use the nested type, its ok

What i missed here?

example
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Type Position (Lat,Lon As Float)
   Type GPS (Spot,Target As Position)
   Dim PL As GPS
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   PL.Spot.Lat=1
End Sub
 
Upvote 0
Top