Click here to Skip to main content
15,891,375 members

Passing a function name as parameter to function

MK-Gii asked:

Open original thread
Hello Guys,
I am having a hard time with passing a function name as a parameter to another function.
I have the following setup:
1. There are 2 classes (A and B)
2. I am trying to write a function in class A which would be executing methods from class B.

I know I can pass a class instance and then run the method from that instantiated class... but I just can find a way to also pass a method name as well (don't want to type the name manually).
I found a good example over here:
http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp[^]
But the problem is that this example shows how to pass method name of the same class (in other words you can access the method directly without having to instantiate the class).... in my case it's a bit different story - I have to instantiate the class....

Thank you in advance guys!

Modestas
Tags: C#, Delegates

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900