0

I would like to organize a class as follows (pseudocode)

Class MyClass

  sub New(MethodEnabled as integer)

   if MethodEnabled = 0
       make MyMethod() to be Sub0()
   else
       make MyMethod() to be Sub1()
   end if

  end sub


  sub MyMethod()
     invoke(either sub0  or sub1 depending on "MethodEnabled" value)
  end sub 


  sub Sub0()
    some code here
  end sub

  sub Sub1()
    some code here
  end sub

End Class

What I wish is to have the sub "MyMethod()" to invoke (or "to be") either Sub0() or Sub1(), depending on how the class was constructed (either with MethodEnabled=0, or MethodEnabled=1).

I gather intuitively that I can do that by using delegates and invoke, but I am not much clear on how to actually do it, in practice.

Can anybody show me how I can possibly do this in the most elegant way. C# of VB.NET examples would be equally great. Thank you!

Pam
  • 474
  • 1
  • 4
  • 13

1 Answers1

2

You need to either declare a delegate type or use System.Action, and then use an instance of that delegate type:

Class [MyClass]

    Private Delegate Sub myDelegate()
    Private myDelegateInstance As myDelegate
    'or you could just leave out 'myDelegate' and use 'System.Action'

    Sub New(ByVal MethodEnabled As Integer)

        If MethodEnabled = 0 Then
            myDelegateInstance = AddressOf Sub0
        Else
            myDelegateInstance = AddressOf Sub1
        End If

    End Sub

    Sub MyMethod()
        myDelegateInstance()
    End Sub

    Sub Sub0()
        'some code here
    End Sub

    Sub Sub1()
        'some code here
    End Sub

End Class
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • Thank you Dave. That's great, even simpler than I imagined. – Pam Mar 23 '14 at 17:04
  • 1
    +1. Just a side note, if you are calling a delegate, better be using `myDelegateInstance.Invoke()`, in this case it will work anyway, but in some cases you can avoid dumb errors. – Victor Zakharov Mar 23 '14 at 17:05
  • Just one thing, how would it be leaving out 'myDelegate' ? – Pam Mar 23 '14 at 17:11
  • Thank you Neolisk. Can you explain a bit what kind of errors you have in mind?. Do I have just to replace myDelegateInstance() with myDelegateInstance.Invoke(), or do you mean something else? – Pam Mar 23 '14 at 17:13
  • 1
    @Pam: To leave out 'myDelegate', just omit that delegate declaration and for 'myDelegateInstance' use the .NET 'System.Action' type instead of 'myDelegate'. – Dave Doknjas Mar 23 '14 at 17:14
  • @Dave wow! that's amazingly simple. Did not know this "action" stuff! Would there be any chance to also remove the further call from MyMethod() and have it directly defined as one of the 2 subs ? – Pam Mar 23 '14 at 17:23
  • 1
    @Pam: Sure - just remove 'MyMethod' and make 'myDelegateInstance' public: Public myDelegateInstance As System.Action. You probably would want to rename 'myDelegateInstance' to something else - perhaps that can then be called 'MyMethod'. – Dave Doknjas Mar 23 '14 at 17:28
  • @Dave Fantastic. Totally beautiful! Way more than I hoped. – Pam Mar 23 '14 at 17:39