Click here to Skip to main content
15,900,258 members
Home / Discussions / C#
   

C#

 
GeneralRe: OOP Advice Pin
Natepizzle25-Dec-11 9:30
Natepizzle25-Dec-11 9:30 
AnswerRe: OOP Advice Pin
PIEBALDconsult25-Dec-11 4:36
mvePIEBALDconsult25-Dec-11 4:36 
GeneralRe: OOP Advice Pin
Natepizzle25-Dec-11 9:33
Natepizzle25-Dec-11 9:33 
GeneralRe: OOP Advice Pin
PIEBALDconsult25-Dec-11 9:57
mvePIEBALDconsult25-Dec-11 9:57 
AnswerRe: OOP Advice Pin
Luc Pattyn25-Dec-11 11:36
sitebuilderLuc Pattyn25-Dec-11 11:36 
AnswerRe: OOP Advice Pin
BillWoodruff25-Dec-11 14:21
professionalBillWoodruff25-Dec-11 14:21 
GeneralRe: OOP Advice Pin
Natepizzle25-Dec-11 22:13
Natepizzle25-Dec-11 22:13 
Questionvalue of using Func and lambdas vs. Delegates vs. Interface ? Pin
BillWoodruff23-Dec-11 23:20
professionalBillWoodruff23-Dec-11 23:20 
This post is somewhat inspired by Blubbo's recent post, here, on creating a set of methods with similar signatures and return values.

I think I can describe what I want to ask more clearly in the context of some "real code:"

Assume you have a WinForm with three NumericUpDown controls, and one ComboBox presenting a list of three choices: "Multiply, Divide, Power:"
C#
// assume all three NumericUpDowns have "Minimum" and "Maximum"
// values set to: Decimal.MinValue, and Decimal.MaxValue
private Func<decimal, decimal, decimal> PE;

private decimal x, y;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    x = numericUpDown1.Value;
    y = numericUpDown2.Value;

    switch (comboBox1.SelectedIndex)
    {
        case 0: // multiply
            PE = (d1, d2) => d1*d2;
            break;
        case 1: // divide
            // probably better to handle divisor == 0 here ?
            PE = (d1, d2) => d1/ d2;
            break;
        case 2: // power
            // need to handle case of raising to the 0th. power here ?
            // as is: zero to the zeroth. power will be returned as #1 !
            PE = (d1, d2) => (decimal) Math.Pow((double) d1, (double) d2);
            break;
        default:
            // throw error here during development
            break;
    }

    try { numericUpDown3.Value = PE(x, y); }
        // handle System.DivideByZero exception
        catch (DivideByZeroException dZero) { }
        // handle System.Overflow exception: Decimal.MinValue or Decimal MaxValue
        catch (OverflowException oFlow) {}
}
What I would appreciate your opinions on are:

1. is there any real performance value inherent in making use of Func and lambdas here ... assuming we "scale" this simple example up to include a very large number of "case" conditions in the "switch" block ?

2. for those of you with understanding of what happens in IL "under the hood:" does anything about assigning those lambdas to the Func "PE" in the "case" statements make the "switch" statement any more complex, or less performant ... once again assuming this example is really scaled up ?

3. since 'throw' is a valid command for exiting a case statement in a switch block, would you ever throw an error in a case statement when possible ?

4. what's your opinion on using something like this compared with the use of Interfaces, as suggested in responses to Blubbo's question ?

thanks ! ... and Merry Xmas ... Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

AnswerRe: value of using Func and lambdas vs. Delegates vs. Interface ? Pin
PIEBALDconsult24-Dec-11 2:57
mvePIEBALDconsult24-Dec-11 2:57 
AnswerRe: value of using Func and lambdas vs. Delegates vs. Interface ? Pin
Eddy Vluggen24-Dec-11 10:00
professionalEddy Vluggen24-Dec-11 10:00 
AnswerRe: value of using Func and lambdas vs. Delegates vs. Interface ? Pin
Shameel25-Dec-11 22:55
professionalShameel25-Dec-11 22:55 
Questionshould I create a filter? Pin
is90057james23-Dec-11 17:46
is90057james23-Dec-11 17:46 
QuestionLooking for developers to help in free and opensource project Pin
Constantinos Coudounaris23-Dec-11 8:38
Constantinos Coudounaris23-Dec-11 8:38 
AnswerRe: Looking for developers to help in free and opensource project Pin
Not Active23-Dec-11 9:12
mentorNot Active23-Dec-11 9:12 
QuestionCrystal Report Designing Issue Pin
AmbiguousName22-Dec-11 22:55
AmbiguousName22-Dec-11 22:55 
AnswerRe: Crystal Report Designing Issue Pin
Richard MacCutchan23-Dec-11 0:45
mveRichard MacCutchan23-Dec-11 0:45 
AnswerRe: Crystal Report Designing Issue Pin
Blue_Boy23-Dec-11 4:18
Blue_Boy23-Dec-11 4:18 
QuestionDetecting silence / low level in a WAV file Pin
SimonwHill22-Dec-11 4:55
SimonwHill22-Dec-11 4:55 
AnswerRe: Detecting silence / low level in a WAV file Pin
Alan Balkany22-Dec-11 5:43
Alan Balkany22-Dec-11 5:43 
AnswerRe: Detecting silence / low level in a WAV file Pin
Albert Holguin22-Dec-11 7:43
professionalAlbert Holguin22-Dec-11 7:43 
GeneralMessage Removed Pin
22-Dec-11 7:57
professionalN_tro_P22-Dec-11 7:57 
GeneralRe: Detecting silence / low level in a WAV file Pin
Albert Holguin22-Dec-11 8:44
professionalAlbert Holguin22-Dec-11 8:44 
QuestionHelp Me ! How to add the video OSD? Pin
is90057james21-Dec-11 23:33
is90057james21-Dec-11 23:33 
QuestionIIS Problem Pin
Zeyad Jalil21-Dec-11 20:37
professionalZeyad Jalil21-Dec-11 20:37 
AnswerRe: IIS Problem Pin
Rajesh Anuhya21-Dec-11 20:51
professionalRajesh Anuhya21-Dec-11 20:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.