0
votes

In VB, I want to assign a (shared) method to a variable
And I'm asking if there is some namespace operator like :: in C++ and Java

Class C
    Public Shared Function m(a as Integer) as Integer
        Return  a * 2  
    End Function
End Class
                
Public Sub Main()
        
    Dim localM As Func(Of Integer, Integer) = C.m
        
    Console.WriteLine(localM(5))
End Sub

I know I could use function(a) C.m(a)
but that is not pretty
and I don't want to create a new function every time
and is also sensitive to argument changes

as for why would I do that
I'm actually passing it to function as an argument
(someFunction(3, C::m))

2
I actually just solved it by making methods into attributes (Func(of Integer, Integer)). But the question still stands, as this will not always be an option - Superior
That's not an attribute, Func is a delegate. It is always an option. - Hans Passant
@HansPassant how is it not an attribute if it is an object representing function? But never mind names. It is not always an option, as I can't change classes that I didn't create. I still don't know how to use the Delegate to pass the method as an argument. - Superior
A rose by any other name... except Attribute already has a very specific meaning in the .Net world that is different from how you used it. - Joel Coehoorn

2 Answers

2
votes

You can do this:

Sub Main() 
    Dim localM As Func(Of Integer, Integer) = AddressOf C.m
    Console.writeline(localM(5))
End Sub

You can see it work here:

https://dotnetfiddle.net/Uxl0Y7

I didn't change anything in the sample class, except to complete the function so it returns a value (required for the function to compile) and to fix your lousy capitalization.


I'm actually passing it to function as an argument

In that case:

Sub Main() 
    Dim localM As Func(Of Integer, Integer) = AddressOf C.m
    Foo(localM)

    ' Or

    Foo(AddressOf C.m)
End Sub

Sub Foo(theMethod As Func(Of Integer, Integer))
    Console.WriteLine(theMethod(5))
End Sub

In either case, the trick is to do two things:

  1. Define either an Action (for a Sub) or Func (for a Function) that matches the signature of the target method. In this case, it's Func(Of Integer, Integer). Any method up to 8 arguments can be mapped this way, but you do have to know the signature in advance.
  2. Use AddressOf to capture the reference to the method.
1
votes

Is this what you are looking for?

Class C
    Public Shared Add1 As Func(Of Integer, Integer) = Function(value As Integer)
                                                          Return value + 1
                                                      End Function

End Class

to use

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim foo As Integer = C.Add1(3)
    Debug.WriteLine(foo)
End Sub

edit

Class C
    Public Shared Add1 As Func(Of Integer, Integer) = Function(value As Integer)
                                                          Return value + 1
                                                      End Function

    Public Shared DebugStr As Action(Of String, String) = Sub(first As String, second As String)
                                                              Debug.WriteLine("One-{0}  Two-{1}",
                                                                                first, second)
                                                          End Sub

End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim foo As Integer = C.Add1(7)
    Debug.WriteLine(foo)

    C.DebugStr("Quick", "Fox")
End Sub