Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass a method onto an object

Status
Not open for further replies.

StrangeWill

IS-IT--Management
May 11, 2006
9
0
0
US
This one seems complated to me so here is the idea of what I want:

class MenuItem
{
private ConsoleKey obj_Key;
private METHOD Method;

public void RunMenuItem()
{
Method();
}
}

Basically I want to be able to assign a Method, and be able to call it, I want to be able to assign different Methods to multiple instances of the same object so one MenuItem may call DoAdd() while another may do DoDelete()

I need help with this ASAP!
 
yup. definitely delegates. if you have MSDN installed, search "delegate keyword".
 
I thought this was interesting.
I am not sure how would you use delegates? Could you post a code snippet.
Here is one way
Code:
using System;

class MainClass
{
	Methods myMethods;
	public MainClass()
	{
		//we need a method from another class
		myMethods = new Methods();
	}

	[STAThread]
	static void Main(string[] args)
	{
		MainClass m = new MainClass();
		Console.WriteLine(m.PrintString("MainClass"));			
	}

	private string PrintString(string str)
	{
		return myMethods.PrintString(str);
	}
}

class Methods
{
	public string PrintString(string str)
	{
		return str + " is using a method from the class Methods";			
	}

}
Marty
 
Think backwards.

Instead of the class calling a known method of another class, have the class call an obfuscated method using an interface.

IActionMethod method;

public void RegisterMethod(IActionMethod m)
{
method = m;
}

public void ExecuteMethod()
{
method.DoSomething();
}

//The interface in this case would look like so.

public interface IActionMethod
{
void DoSomething();
}



I use this in case statements all the time. State machines rely on this heavily too!

Delegates are a great way to go if you don't want the calling class to have any idea what happens after it fires its event.
 
A delegate is nothing more than a pointer to a method. Since it's a type in & of itself, you can pass them around like any other variable.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Just want to make sure I am on the same page. JM is this what you are saying.
Code:
using System;

public interface IActionMethod
{
	void DoSomething();
}

class Client
{
	IActionMethod method;

	public void RegisterMethod(IActionMethod m)
	{
		method = m;
	}

	public void ExecuteMethod()
	{
		method.DoSomething();
	}

	[STAThread]
	static void Main(string[] args)
	{
		Client c = new Client();
		ActionMethods ac = new ActionMethods();
		c.RegisterMethod(ac);
		c.ExecuteMethod();
	}
}

public class ActionMethods : IActionMethod
{
	public void DoSomething()
	{
		Console.WriteLine("Dosomehing from ActionMethods : IActionMethod");
	}
}

Chip to your point one could consider a delegate as an interface for one method?

I thank you both very much,
Marty
 
My code in the Main was wrong. I removed the lines that instantiated ActionMethods and registered the interface and placed them into a constructor.
public Client()
{
ActionMethods am = new ActionMethods();
RegisterMethod(am);
}
Now Main knows nothing of the interface or the class it is using.
Thanks again,
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top