German Frage zu callsub, callsub2 etc.

kpmais

Member
Licensed User
Longtime User
Ich arbeite mich gerade in B4A ein und hänge seitens meines Verständnisses am Beispiel für Polymorphismen fest.
Hier gehts speziell um die Verwendung von callsub2 in dem Beispiel.
Hier das Beispiel:
B4X:
'Class Square module
Sub Class_Globals
  Private mx, my, mWidth As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Sub Initialize (Shapes As List, x As Int, y As Int, length As Int)
  mx = x
  my = y
  mLength = length
  Shapes.Add(Me)
End Sub

Sub Draw(c As Canvas)
Private r As Rect
  r.Initialize(mx, my, mx + mLength, my + mLength)
  c.DrawRect(r, Colors.Red, False, 1dip)
End Sub

'Class Circle module
Sub Class_Globals
  Private mx, my, mRadius As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Sub Initialize (Shapes As List, x As Int, y As Int, radius As Int)
  mx = x
  my = y
  mRadius = radius
  Shapes.Add(Me)

End Sub

Sub Draw(cvs As Canvas)
  cvs.DrawCircle(mx, my, mRadius, Colors.Blue, False, 1dip)
End Sub

'Main Modul

Sub Process_Globals
  Public Shapes As List
End Sub

Sub Globals
  Private cvs As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
  cvs.Initialize(Activity)
  Private Square1, Square 2 As Square
  Private Circle1 As Circle
  Shapes.Initialize
  Square1.Initialize(Shapes, 110dip, 110dip, 50dip)
  Square2.Initialize(Shapes, 10dip, 10dip, 100dip)
  Circle1.Initialize(Shapes, 50%x, 50%y, 100dip)
  DrawAllShapes
End Sub

Sub DrawAllShapes
  For i = 0 To Shapes.Size - 1
    CallSub2(Shapes.Get(i), "Draw", cvs)
 Next
 Activity.Invalidate
End Sub

Erläuterung zu Callsub:

Method_636.png
CallSub (Component As Object, Sub As String) As Object​

Calls the given sub. CallSub can be used to call a sub which belongs to a different module.

However the sub will only be called if the other module is not paused. In that case an empty string will be returned.
You can use IsPaused to test whether a module is paused.
This means that one activity cannot call a sub of a different activity. As the other activity will be paused for sure.
CallSub allows an activity to call a service sub or a service to call an activity sub.
Note that it is not possible to call subs of code modules.
CallSub can also be used to call subs in the current module. Pass Me as the component in that case.
Example:
CallSub(Main, "RefreshData")

So wie ich es verstehe, bezeichnet 'Component' den Ort (Main, Modul etc.) an dem die Sub byCall liegt.
Entsprechend dem letzten Beispiel, die Sub 'RefreshData' liegt in der Main und wird dort gefunden und aufgerufen.

In dem Beispiel zum Polymorphismus oben wird als Component eine Liste übergeben.
Der Inhalt der Liste wird dann in der aufgerufenen Sub 'Draw' als Parameter verwendet.

Der Ablauf ist mir völlig schleierhaft.
Bitte erkläre mir mal jemand, was da wie vor sich geht.
 

kpmais

Member
Licensed User
Longtime User
Danke an alle, die sich die Köpfe zerbrochen oder einfach nur geschüttelt haben .... :)) ...,

... ich habe es endlich verstanden ...
 
Top