class question

G

Gunawan

hi all,
I have an issue. It's rather hard to explain. I hope you all could
understand.

I would like to create a class let's say myclass.
in myclass I have some static void function

myclass.function1()
myclass.function2() and so on.

If I need to use function1() I could just type
myclass.function1();

and it works.

I would like to know is there any function that I can use to call a static
function.
so I can call function1 like this
whatfunction("myclass.function1()");

whatfunction("myclass.function2()");

Hopefully you can help me with this.

Regards,
Gun
 
M

Marc Gravell

Depending on the scenario, there are two options... if you know the
names at compile time, but need a way of takling about the method in
an abstract way (interchangeable with a range of such functions) then
a delegate is the answer...

MethodInvoker myMethod = myclass.function1; // note no brackets

then later...

myMethod();

---

The alternative is reflection -
typeof(myclass).GetMethod("function1").Invoke(null, null)

Note that reflaction is generally considered the less preferable
option, and gives you no compile-time checking.

Marc
 
R

Rick Lones

Gunawan said:
I would like to create a class let's say myclass.
in myclass I have some static void function

myclass.function1()
myclass.function2() and so on.

I would like to know is there any function that I can use to call a static
function.
so I can call function1 like this
whatfunction("myclass.function1()");

whatfunction("myclass.function2()");

Some might say this is a bit perverted . . . but just for fun:

using System;
using System.Collections;

namespace WeirdFunctionCall
{
class MyClass
{
delegate void myFunc();
static Hashtable whatfunction = new Hashtable();

[STAThread]
static void Main(string[] args)
{
whatfunction.Add("myclass.function1()", new myFunc(MyClass.f1));
whatfunction.Add("myclass.function2()", new myFunc(MyClass.f2));
whatfunction.Add(" . . . and so on", new myFunc(MyClass.f3));

((myFunc)whatfunction["myclass.function1()"])();
((myFunc)whatfunction["myclass.function2()"])();
((myFunc)whatfunction[" . . . and so on"])();

Console.ReadLine();
}

static void f1() {Console.WriteLine("f1 here . . .");}
static void f2() {Console.WriteLine("f2 here . . .");}
static void f3() {Console.WriteLine("f3 here . . .");}

}
}

I have actually done things kind of like this, don't remember why it seemed like
a good idea at the time. I suppose that in VS2005 you could use a generic
Hashtable<string, myFunc> and avoid the explicit cast when invoking.

HTH,
-rick-
 
G

Gunawan

Rick Lones said:
Gunawan said:
I would like to create a class let's say myclass.
in myclass I have some static void function

myclass.function1()
myclass.function2() and so on.

I would like to know is there any function that I can use to call a
static function.
so I can call function1 like this
whatfunction("myclass.function1()");

whatfunction("myclass.function2()");

Some might say this is a bit perverted . . . but just for fun:

using System;
using System.Collections;

namespace WeirdFunctionCall
{
class MyClass
{
delegate void myFunc();
static Hashtable whatfunction = new Hashtable();

[STAThread]
static void Main(string[] args)
{
whatfunction.Add("myclass.function1()", new myFunc(MyClass.f1));
whatfunction.Add("myclass.function2()", new myFunc(MyClass.f2));
whatfunction.Add(" . . . and so on", new myFunc(MyClass.f3));

((myFunc)whatfunction["myclass.function1()"])();
((myFunc)whatfunction["myclass.function2()"])();
((myFunc)whatfunction[" . . . and so on"])();

Console.ReadLine();
}

static void f1() {Console.WriteLine("f1 here . . .");}
static void f2() {Console.WriteLine("f2 here . . .");}
static void f3() {Console.WriteLine("f3 here . . .");}

}
}

I have actually done things kind of like this, don't remember why it
seemed like a good idea at the time. I suppose that in VS2005 you could
use a generic Hashtable<string, myFunc> and avoid the explicit cast when
invoking.

HTH,
-rick-


Thank you rick, though I'm not quite understand...
Is there any other solution?

Regards,
Gun
 
R

Rick Lones

Gunawan said:
Rick Lones said:
Gunawan said:
I would like to create a class let's say myclass.
in myclass I have some static void function

myclass.function1()
myclass.function2() and so on.

I would like to know is there any function that I can use to call a
static function.
so I can call function1 like this
whatfunction("myclass.function1()");

whatfunction("myclass.function2()");
Some might say this is a bit perverted . . . but just for fun:

using System;
using System.Collections;

namespace WeirdFunctionCall
{
class MyClass
{
delegate void myFunc();
static Hashtable whatfunction = new Hashtable();

[STAThread]
static void Main(string[] args)
{
whatfunction.Add("myclass.function1()", new myFunc(MyClass.f1));
whatfunction.Add("myclass.function2()", new myFunc(MyClass.f2));
whatfunction.Add(" . . . and so on", new myFunc(MyClass.f3));

((myFunc)whatfunction["myclass.function1()"])();
((myFunc)whatfunction["myclass.function2()"])();
((myFunc)whatfunction[" . . . and so on"])();

Console.ReadLine();
}

static void f1() {Console.WriteLine("f1 here . . .");}
static void f2() {Console.WriteLine("f2 here . . .");}
static void f3() {Console.WriteLine("f3 here . . .");}

}
}

I have actually done things kind of like this, don't remember why it
seemed like a good idea at the time. I suppose that in VS2005 you could
use a generic Hashtable<string, myFunc> and avoid the explicit cast when
invoking.

HTH,
-rick-


Thank you rick, though I'm not quite understand...
Is there any other solution?

As Marc said, it depends on your actual problem. It appears from your original
question that you are being handed a string which names a function which is
"known" to you at compile time and you are just looking for a convenient way to
get from the string to the function it identifies. In this case there are a lot
of ways, ranging from some kind of delegate mechanism to something as simple as
a switch statement. (E.g., case "function1": myClass.function1(); break;) My
example above is a somewhat tongue-in-cheek example of using delegates, but it
does work just fine. It uses a Hashtable to map name strings to delegates.

That said, I have to wonder why you would want your functions to be invoked in
this rather indirect way at all. Is there some reason they can't just be
exposed directly as public static methods of of your class?

-rick-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top