473,325 Members | 2,872 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,325 software developers and data experts.

Call a method based on a parameter

Hi there

in c# what is a good way of selecting which method to call based on a
string?

For example, my method can accept a string parameter (there are 50 or so
valid strings that can be passed in), and based on this string I should call
some other specific handler method.

Is there some smart mapping I can make between strings and methods? Some
sort of array like:

"string_one", method_1,
"string_two", method_2
etc

Then my method could match the the passed-in string with the array, and call
the appropriate method.
public void HandleData(string parameter)
{
// find string in array which matches parameter...
// call the associated method....
}
Thanks for any advice,
Peter
Jan 26 '06 #1
2 1545
you can use string -> delegate map

Jan 26 '06 #2
Peter Kirk wrote:
in c# what is a good way of selecting which method to call based on a
string?

For example, my method can accept a string parameter (there are 50 or so
valid strings that can be passed in), and based on this string I should call
some other specific handler method.

Is there some smart mapping I can make between strings and methods? Some
sort of array like:

"string_one", method_1,
"string_two", method_2
etc


You can do this very easily if all the methods you want to call have
the same signature, by using a delegate. Here's a sample (in .NET 2.0
which makes things slightly prettier, but it's the same principle in
..NET 1.1, just with more casting etc):

using System;
using System.Collections.Generic;

class Calculator
{
delegate int MathsOperation (int x, int y);

Dictionary<string, MathsOperation> lookup;

public Calculator()
{
lookup = new Dictionary<string, MathsOperation>();
lookup["+"] = Plus;
lookup["*"] = Times;
}

public int Compute (string op, int x, int y)
{
return lookup[op] (x, y);
}

int Plus (int x, int y)
{
return x+y;
}

int Times (int x, int y)
{
return x*y;
}
}

class Test
{
static void Main()
{
Calculator calculator = new Calculator();
Console.WriteLine (calculator.Compute ("+", 3, 5));
Console.WriteLine (calculator.Compute ("*", 10, 2));
}
}

Jan 26 '06 #3

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

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
1
by: dennis luehring | last post by:
i try to "publish" some of my very stupid methods (i need this for an interpreter) function_one procedure_one here is the code --------- class core {
5
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
1
by: Jeffrey Kingsley | last post by:
I would like to cancel a call to BeginInvoke of a delegate (i.e. kill the thread the call was made on). This would be done when a timeout occures for the waitone call to the WaitHandle object of...
13
by: jac | last post by:
Hae, I have a windows form with a ComboBox an other things. On that combobox I have an eventhandler on de selectedindexchanged. But somewhere in my code want to do excecute the same code that...
4
by: Zuel | last post by:
Hi Folks. So I have a small problem. My DoPostBack function is not writen to the HTML page nor are the asp:buttons calling the DoPostBack. My Goal is to create a totaly dynamic web page where...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
3
by: Rich Robinson | last post by:
Hi, I have a web service method which takes a DateTime type as a parameter. The service is UK based, and the dates are passed in to the service in the UK format dd/MM/yyyy. On a recent...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.