Click here to Skip to main content
15,898,703 members
Please Sign up or sign in to vote.
2.56/5 (3 votes)
See more:
i am making a calculator where i want simply pass a method in case? but still i am not able to do that? i mean i dont want perform any kind of action in case that's why i have make method but it not working/
can u plz guid me.. Thanks for in afvance...
Posted
Updated 1-Jan-14 13:30pm
v4
Comments
ZurdoDev 1-Jan-14 14:18pm    
I have not tried it but if it doesn't work add 1 line of code where you store the result in a variable and then switch on that.
Ron Beyer 1-Jan-14 14:26pm    
Please show us what you are trying to do because its not immediately clear from your description.
Adam Zgagacz 1-Jan-14 15:03pm    
It should work without a problem. Please show us the code. Perhaps problem is somewhere else.
Sergey Alexandrovich Kryukov 1-Jan-14 15:12pm    
First, you can simply read the help page syntax: the answer is more than obvious. Secondly, you could try to write the code. Again, it's way easy to find out. And finally, any calculator with buttons and case statement is something ugly, bloated and useless, even for exercise. I would invite you to find out how to make it without that ugliness...
—SA

 
Share this answer
 
You can do anything in a Case block inside a Switch/Case clause that you can do in any method. Of course, you can't define a class, method, or struct in a Switch/Case block, just as you can't define those inside a standard method (unless you did some very esoteric stuff with reflection or Lambda Expressions, and so forth) ... but, why would you ever want to do that ?

Use of a Switch/Case clause is almost a standard in the classic Calculator example that many .NET books give: for example, if I create a set of Buttons, and set the text of each one of them to some basic calculation, and then assign the same Click EventHandler to all of them:
C#
// assume you have a TextBox, 'tBoxResult, 
// where you want the result of the calculation displayed:
private void CalcBtnClick(object sender, EventArgs e)
{
    Button buttonClicked = sender as Button;

    // make sure we have a valid button !
    if(buttonClicked == null) throw new ArgumentException("argument passed to CalcBtnClick is not Type 'Button");

    // code here to get current operands to be processed,
    // if necessary convert them from strings to double
    // for testing only: assume arguments are in:
    double arg1 = 10.0;
    double arg2 = 20.0;

    switch (buttonClicked.Text)
    {
        case "+":
            // add
            tBoxResult.Text = (arg1 + arg2).ToString();
            break;
        case "-":
            // subtract
            tBoxResult.Text = (arg1 - arg2).ToString();
            break;
        case "*":
            // multiply
            tBoxResult.Text = (arg1 * arg2).ToString();
            break;
    }
}
There is a more "elegant" way to do this using a Dictionary holding Buttons as Keys, and Funcs (methods ... in the form of Lambda Expressions) as Values, but it is a very advanced use of .NET. On request I will demonstrate that here.
 
Share this answer
 
Comments
Karthik_Mahalingam 2-Jan-14 0:42am    
5exactly..
A swtich expression or case label must be a bool,char,string,integral,enum only
methods ,any other types cannot be used.

reference :
link [^]
 
Share this answer
 
v2
Comments
BillWoodruff 2-Jan-14 2:18am    
A switch expression can use evaluation, including executing basic operators on value Types, looking up values in reference Types (that are integral), etc. The key requirement is that they resolve to an integral Type which is "legal" to use as the selector.

Requirements for the Case expression are more stringent: it must be a constant, and no evaluation is permitted.

This is legal:

List<int> lstInt = new List<int> { 1, 5, 8, 11};

private int test(int ndx)
{
return lstInt[ndx];
}

private void SomeControl_KeyDown(object sender, KeyEventArgs e)
{
switch (test(2))
{
case 8:
MessageBox.Show("8");
break;
}
}
Karthik_Mahalingam 2-Jan-14 3:08am    
agreed,
for case label ??? :-)
BillWoodruff 3-Jan-14 0:40am    
By using the term "case expression" in my comment above, I meant "case label," which must be a ValueType (string, integer), or Constant Type (Constant, Enum).

Under-the-hood, Switch/Case clauses are transformed into highly optimized IL code: if the case statement is a series of integers increasing regularly in value, the IL is essentially a jumpo table, which is going to evaluate directly ... as in: "fast."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900