473,387 Members | 1,592 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.

pass a method as a parameter

hello

can I pass a static method as a parameter to a method?

if the answer is yes how do I do that ? how do I call the method ?

yoramo.
Nov 15 '05 #1
4 21739
On Mon, 12 Jan 2004 08:32:17 +0200, wrote:
hello

can I pass a static method as a parameter to a method?

if the answer is yes how do I do that ? how do I call the method ?

yoramo.


Is this what you mean?

private static string name = "Hello World!";

private static string method1()
{
return name;
}

private void method2(string text)
{
MessageBox.Show(text);
}

private void method3()
{
method2(method1());
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
No. I need a way to call a method that will be pass as a parameter to
another method.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr1ngw7fhhntkfz@localhost...
On Mon, 12 Jan 2004 08:32:17 +0200, wrote:
hello

can I pass a static method as a parameter to a method?

if the answer is yes how do I do that ? how do I call the method ?

yoramo.


Is this what you mean?

private static string name = "Hello World!";

private static string method1()
{
return name;
}

private void method2(string text)
{
MessageBox.Show(text);
}

private void method3()
{
method2(method1());
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
OK, I figured it out what I need is a delegate!!!
"yoramo" <yoramo@[NOSPAM]hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
hello

can I pass a static method as a parameter to a method?

if the answer is yes how do I do that ? how do I call the method ?

yoramo.

Nov 15 '05 #4
You need to use Delegates for this. A Delegate is strongly typed method pointer.

You define a delegate like so:

public delegate void DoSomethingDelegate(int i);

(This is a kind of global delegate. You could define a delegate within a class as well).
Delegates are objects (even thou the decleration above might not indicate as such) and so need to be created before you can use them. They typically take a parameter in their constructor, a method that has the same signature as the delegate.

So lets say you have a method called Calcuate in a class that has the same signature as the delegate above and you wanted to pass this method to another object (or method) you would do this

private void Calculate(int num)
{
//Some code here
}

TheMethodThatRequiresAMethodAsAParamter(new DoSomethingDelegate(Calculate));

public void TheMethodThatRequiresAMethodAsAParamter(DoSomethin gDelegate m)
{

}

Now the parameter m is your reference to the meothd Calculate so you can call calculate simply like so:

m(56);

FYI, you can pass static and even private methods as parameters to a delegate's contructor. You can even have a return type in a delegate instead of void (I won't go into multicast delegates here to keep things simply).
Hope this helps.
--
Shiv R. Kumar
http://www.matlus.com
Nov 15 '05 #5

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

Similar topics

1
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
1
by: Dion Heskett | last post by:
How can I pass a Class as a parameter to in a method ? i.e. Private myMethod( string pram1, Classobject as pram2) { Classobject.DataSource = reader; Classobject.DataBind(); }
5
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
9
by: Jay Douglas | last post by:
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error (Cannot pass '<this>' as a ref or out argument because...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.