Solved

Convert a string to a method call

Posted on 2009-04-13
2
336 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
2 Comments
 
LVL 6

Accepted Solution

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

Assisted Solution

by:Tiggerito
Tiggerito 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now