|
-
Oct 28th, 2009, 09:37 PM
#1
Thread Starter
Fanatic Member
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?
-
Oct 28th, 2009, 09:43 PM
#2
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
-
Oct 28th, 2009, 10:17 PM
#3
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:
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:
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:
Public Sub TryFiveTimes(ByVal task As ReturnsBooleanMethodInvoker) For count = 1 To 5 task.Invoke() Next 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.
-
Oct 28th, 2009, 10:28 PM
#4
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:
Delegate Sub MyDelegate(ByVal arg As String) Private Sub InvokeDoSomething() DoSomething(New MyDelegate(AddressOf DoSomethingElse)) End Sub Private Sub DoSomething(ByVal method As MyDelegate) method.Invoke("test") End Sub Private Sub DoSomethingElse(ByVal arg As String) MessageBox.Show("SomethingElse called with arg=""" & arg & """") End Sub
To test, call InvokeDoSomething() and you should see a messagebox saying "SomethingElse called with arg="test"". DoSomething called DoSomethingElse without its name.
-
Dec 7th, 2009, 12:47 PM
#5
Thread Starter
Fanatic Member
Re: The power of object oriented programming
Ah I see. And that delegate I bet will support templates too.
-
Dec 7th, 2009, 02:03 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|