Solved

Convert a string to a method call

Posted on 2009-04-13
2
341 Views
Last Modified: 2013-12-17
I have a form application with a "drop down list" and  "Select button".
There are 3 functions
void foo1()
{
 ...
}

void foo2()
{
...
}
void foo3()
{
...
}

Is it possible to:
1) Convert method names to string so that these strings can be added to "drop down list" as "foo1", "foo2", "foo3"
2) Once a value is selected in the drop down, is it possible to change the selected string to method call:
    i.e. "foo1" as foo1()
          "foo2" as foo2()
         "foo3" as foo3()

My basic understanding of C# says, it is not possible. But I want to find from experts, if there is a way to do this.

Thanks,
0
Comment
Question by:ukalyan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
Cebik earned 250 total points
ID: 24133454
0
 
LVL 23

Assisted Solution

by:Tony McCreath
Tony McCreath earned 250 total points
ID: 24136387
Here's a more structured way to do it using delegates.

Delegates are a way to reference methods and call them later
/// <summary>
/// Define the signature of the methods that can be placed in the combo. e.g.
/// void fooX()
/// </summary>
public delegate void DropDownMethod();
 
/// <summary>
/// A class to be used as items in the combo box
/// </summary>
public class DropDownMethodItem
{
	public DropDownMethodItem(string name, DropDownMethod method)
	{
		Name = name;
		Method = method;
	}
 
	/// <summary>
	/// Display text
	/// </summary>
	public string Name;
 
	/// <summary>
	/// A deligate pointing to the method this item will call
	/// </summary>
	public DropDownMethod Method;
 
	/// <summary>
	/// This controls the display text
	/// </summary>
	/// <returns></returns>
	public override string ToString()
	{
		return Name;
	}
}
 
#region methods to call 
public void foo1()
{
	MessageBox.Show("foo1 called");
}
 
public void foo2()
{
	MessageBox.Show("foo2 called");
}
 
public void foo3()
{
	MessageBox.Show("foo3 called");
}
#endregion
 
/// <summary>
/// Polulates the combo box with each method. In my case I click a button to populate
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonPopulate_Click(object sender, EventArgs e)
{
	comboBox1.Items.Add(new DropDownMethodItem("foo1", new DropDownMethod(foo1)));
	comboBox1.Items.Add(new DropDownMethodItem("foo2", new DropDownMethod(foo2)));
	comboBox1.Items.Add(new DropDownMethodItem("foo3", new DropDownMethod(foo3)));
}
 
/// <summary>
/// Call the selected method when its item is selected
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
	((DropDownMethodItem)comboBox1.SelectedItem).Method();
}

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 85
Groupbox Control ? 2 40
Error import assembly 19 31
.NET Redemption/Outlook Examples 3 35
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question