Results 1 to 6 of 6

Thread: The power of object oriented programming

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    The power of object oriented programming

    Say I want to create a function

    Try 5 times

    The function takes a function pointer to another function

    function dosomething() as boolean

    end function

    function tryfivetime (thefunction as what?)
    for i = 1 to 5
    dosomething
    next i
    end function

    Can I do that? I can't do that on vb6 right, but can I do that on vb.net?

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: The power of object oriented programming

    There are a couple of ways you can do that. I don't know your scenario, but one way to do this is to use interfaces. So you have an interface that defines the functions that will be available, then classes that implement the interface could be passed to the function and you know they will implement the interface for you:

    Code:
    Public Interface IDoTheJob
        Function DoSomething() As Boolean
    End Interface
    
    Public Class MyClass2
        Implements IDoTheJob
    
        Public Function DoSomething() As Boolean Implements IDoTheJob.DoSomething
            Return True
        End Function
    End Class
    
    Public Class MyClass3
        Implements IDoTheJob
    
        Public Function DoSomething() As Boolean Implements IDoTheJob.DoSomething
            Return False
        End Function
    End Class
    Then to use it:

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim a As New MyClass2
            Dim b As New MyClass3
    
            TryFiveTimes(a)
            TryFiveTimes(b)
        End Sub
    
    
        Public Function TryFiveTimes(ByVal x As IDoTheJob) As Boolean
            For i As Integer = 0 To 5
                x.DoSomething()
            Next
        End Function

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: The power of object oriented programming

    In .NET a function pointer is replaced by a delegate, which is an object that contains a reference to a method rather than just a raw pointer. You need to define a delegate that has the same signature as the method you want to execute:
    vb.net Code:
    1. Public Delegate Function ReturnsBooleanMethodInvoker() As Boolean
    You can now create instances of that type by passing a reference to a method with that signature:
    vb.net Code:
    1. Dim invoker As New ReturnsBooleanMethodInvoker(AddressOf DoSomething)
    You can then pass that object to a method that has a parameter of that type:
    vb Code:
    1. Public Sub TryFiveTimes(ByVal task As ReturnsBooleanMethodInvoker)
    2.     For count = 1 To 5
    3.         task.Invoke()
    4.     Next
    5. End Sub
    Note that the .NET Framework already contains a lot of delegate types, some generic and some not. If one of them suits your circumstances then you can use it instead of declaring your own.

    .NET 4.0 is also going to introduce covariance and contravariance. I've read a bit on these but not comprehensively, but I believe that one of the advantages is that you can declare the method parameter as a more general delegate type and still pass your more specific type. You just won't be able to access the parameters and return value when you invoke it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: The power of object oriented programming

    Do you want to call a function specified by a pointer with a function? If so, you use a delegate:
    vb.net Code:
    1. Delegate Sub MyDelegate(ByVal arg As String)
    2. Private Sub InvokeDoSomething()
    3.         DoSomething(New MyDelegate(AddressOf DoSomethingElse))
    4. End Sub
    5. Private Sub DoSomething(ByVal method As MyDelegate)
    6.         method.Invoke("test")
    7. End Sub
    8. Private Sub DoSomethingElse(ByVal arg As String)
    9.         MessageBox.Show("SomethingElse called with arg=""" & arg & """")
    10. End Sub
    To test, call InvokeDoSomething() and you should see a messagebox saying "SomethingElse called with arg="test"". DoSomething called DoSomethingElse without its name.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: The power of object oriented programming

    Ah I see. And that delegate I bet will support templates too.

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: The power of object oriented programming

    Templates? Do you mean arguments? If so, yes.

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