Android Question Simplified syntax with Custom Types

Tom_707

Member
Licensed User
When I have a Sub with a standard type as parameter:
B4X:
Sub Temp(Input As Int)
   Dim x As Int
  
   x = Input
End Sub
I can pass values either, (1) using a variable or (2) directly, as follows:
B4X:
Dim y As Int = 3

Temp(y)
--OR--
Temp(3)


When using a Sub with a custom type as a parameter:
B4X:
Type Coordinates (x As Int, y As Int)

Sub Temp(Input As Coordinates)

   Dim x, y As Int

   x = Input.x
   y = Input.y
End Sub
How do I pass values to the sub directly, without using a variable? Is something like this possible:
B4X:
Temp((3, 4))
 

agraham

Expert
Licensed User
Longtime User
You need to pass an instance of the type that already contains the required values. If you have a CreateType Sub you could use that

B4X:
Type MyType (Item1 As Int, Item2 As Int)

...

Public Sub CreateMyType (Item1 As Int, Item2 As Int) As MyType
    Dim t1 As MyType
    t1.Initialize
    t1.Item1 = Item1
    t1.Item2 = Item2
    Return t1
End Sub

...

Sub Temp(Input As MyType)
   Dim x, y As Int
   x = Input.x
   y = Input.y
End Sub

...

Temp(CreateMyType (3,4))
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Step 1: Generate Create Type Sub

1hITEYnYZ4.gif


Step 2:
B4X:
Temp(CreateCoordinates(3, 4))
 
Upvote 0

Tom_707

Member
Licensed User
Not sure if I need to start a new thread, but I have a related question.
@agraham, when using your reflection library, is it possible to pass Custom Types as arguments in RunMethod2? For example:
B4X:
Type Coordinates (x As Int, y As Int)

Dim cellCoordinates As Coordinates
Dim refView As Reflector
Dim SomeView, v As View

SomeView.Tag = cellCoordinates
. 
. 
refView.Target = Activity
v = refView.RunMethod2("FindViewWithTag",
    cellCoordinates, <--needs to be string?
    "lang.java.<?>"  <--what type?
Is this possible? What is the correct syntax?
 
Upvote 0
Top