Results 1 to 5 of 5

Thread: type-safe function

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    type-safe function

    "Delegates are type-safe Function Pointers or Callbacks"
    what the hell does that mean?It seems to be boring issue but I have to know coz it deals with event and handlers.
    thanx in advance.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    A delegate outlines the specific parameters of the method, the method signature:

    Public Delegate Sub OnClose(sender as object, e as ClosingEventArgs)

    These means that only a function that has that same signature can be passed in as the parameter. Unlike in in some cases with C++ or VB6 (I think) and the AddressOf method that could be passed in any method signature even though it would fail if not correct. These would be more generic callbacks.

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I would be grateful if you passed me a very easy example of that.
    thanx for your reply Edneeis.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well here is an example, its not the easiest but since callbacks aren't used a lot i think this is easy enough. I have some threading in there just to have a reason for a callback.

    VB Code:
    1. 'FORM CODE STARTS HERE NEEDS ONE BUTTON
    2.     Public Sub WhenDone(ByVal final As ThreadedHandler)
    3.         'since this will be passed through the delegate you ensure that it is type safe
    4.         'and know that the right object will be passed
    5.         MsgBox("I'm finished here is the button text:" & ControlChars.NewLine & final.ID)
    6.     End Sub
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         'the constructor sets the callback and wait time
    10.         Dim test As New ThreadedHandler(AddressOf WhenDone, 750)
    11.         'run the method
    12.         test.Execute()
    13.     End Sub
    14.  
    15.  
    16. 'CLASS CODE STARTS HERE
    17. Public Class ThreadedHandler
    18.  
    19.     'delegate with type safe parameters
    20.     Public Delegate Sub OnDone(ByVal final As ThreadedHandler)
    21.     Private _cb As OnDone
    22.     Private _id As Guid
    23.     Private _ms As Integer
    24.  
    25.     'provides a means fo identifying the same object
    26.     Public ReadOnly Property ID() As String
    27.         Get
    28.             Return _id.ToString
    29.         End Get
    30.     End Property
    31.  
    32.     'starts a threaded wait method
    33.     Public Sub Execute()
    34.         Dim t As New Threading.Thread(AddressOf Wait)
    35.         t.Start()
    36.     End Sub
    37.  
    38.     'this will wait and when done make a callback
    39.     'should be started on a new thread
    40.     Private Sub Wait()
    41.         Dim curThread As Threading.Thread
    42.         curThread.CurrentThread.Sleep(_ms)
    43.         _cb.Invoke(Me)
    44.     End Sub
    45.  
    46.     'make new id and pass a callback and the wait time
    47.     Public Sub New(ByVal callback As OnDone, ByVal interval As Integer)
    48.         _cb = callback
    49.         _id = Guid.NewGuid
    50.         _ms = interval
    51.     End Sub
    52.  
    53. End Class

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Edneeis I owe you .You're such great man .
    I will try it now.
    Thank you soooo much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width