473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

class question

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
Nov 27 '07 #1
4 1381
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
Nov 27 '07 #2
Gunawan wrote:
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, myFuncand avoid the explicit cast when invoking.

HTH,
-rick-
Nov 28 '07 #3
"Rick Lones" <Wr******@YcharterZ.netwrote in message
news:PI***********@newsfe05.lga...
Gunawan wrote:
>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, myFuncand avoid the explicit cast when
invoking.

HTH,
-rick-

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

Regards,
Gun
Nov 29 '07 #4
Gunawan wrote:
"Rick Lones" <Wr******@YcharterZ.netwrote in message
news:PI***********@newsfe05.lga...
>Gunawan wrote:
>>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, myFuncand 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-
Nov 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
2
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in...
9
by: needin4mation | last post by:
I have a .aspx file and it's src. I also have a third file, a class that the src references. Everything is in the same directory/folder. Everything has the same namespace. How do I reference the...
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
2
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B {void m(){...}} class B2 : B {...} Just to clarify...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.