Android Question Event & Class

jinyistudio

Active Member
Licensed User
Hi
I declare a new class. i have some event in ths class.
Like pgTimer1.Initialize("timer1",25),It hasn't need Targetobj As object.
Could i erase TargetObj in my Initialize function and how to implement it like timer1.Initialize ?

B4X:
'Events declaration
#Region Events
#Event: OnRequest(EvtNum as int,buff() as byte,len as int)
#Event: OnRespone(EvtNum as int,buff() as byte,len as int)
#Event: OnTimeout(EvtNum as int)
#End Region

'Class module
Sub Class_Globals
    Private pgDevName As String = ""
    Private pgEvtName As String = ""
    Private pgBaud As Int
    Private pgProtocol As Int=0 '0=asc 1=rtu
    Private pgTimer1 As Timer
    Private pgTarget As Object
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(TargetObj As Object,EventName As String)
    pgTarget = TargetObj
    pgEvtName = EventName
    pgTimer1.Initialize("timer1",25)
End Sub

Public Sub Open(devname As String,baudrate As Int)
    pgDevName = devname
    pgBaud = baudrate
    CallSub3(modserv,"OpenSerial",pgDevName,pgBaud) ' /dev/ttymxc2
    StartService(modserv)
    pgTimer1.Enabled = True
End Sub

Private Sub timer1_tick
    If Not(modserv.Connected) Then
        Return
    End If
   
    If SubExists(pgTarget,pgEvtName & "_ObRequest") Then
        CallSubDelayed(pgTarget,pgEvtName & "_ObRequest")
    End If
End Sub
 

DonManfred

Expert
Licensed User
Could i erase TargetObj in my Initialize function
no. You need the target when you want to fire any events.
how to implement it like timer1.Initialize ?
just extend your initialize method to get more parameters to the class
B4X:
Public Sub Initialize(TargetObj As Object,EventName As String, ms As Int)
    pgTarget = TargetObj
    pgEvtName = EventName
    pgTimer1.Initialize("timer1",ms)
End Sub
 
Top