Click here to Skip to main content
15,565,747 members
Home / Discussions / C#
   

C#

 
Questioncall enum as argument in Method? Pin
Member 145823347-Sep-19 9:55
Member 145823347-Sep-19 9:55 
AnswerRe: call enum as argument in Method? Pin
Dave Kreskowiak7-Sep-19 11:31
mveDave Kreskowiak7-Sep-19 11:31 
GeneralRe: call enum as argument in Method? Pin
BillWoodruff7-Sep-19 16:36
professionalBillWoodruff7-Sep-19 16:36 
GeneralRe: call enum as argument in Method? Pin
Dave Kreskowiak7-Sep-19 18:21
mveDave Kreskowiak7-Sep-19 18:21 
GeneralRe: call enum as argument in Method? Pin
BillWoodruff7-Sep-19 20:04
professionalBillWoodruff7-Sep-19 20:04 
GeneralRe: call enum as argument in Method? Pin
Member 145823347-Sep-19 17:32
Member 145823347-Sep-19 17:32 
GeneralRe: call enum as argument in Method? Pin
BillWoodruff7-Sep-19 20:02
professionalBillWoodruff7-Sep-19 20:02 
AnswerRe: call enum as argument in Method? Pin
BillWoodruff7-Sep-19 16:29
professionalBillWoodruff7-Sep-19 16:29 
I am going to guess that what you are after is something like this: using a Dictionary whose Key is an Enum value, and whose Value is executable code defined in a deferred form (Func, Action, Delegate, Lambda Expression):
using System;
using System.Collections.Generic;

namespace YourNameSpace
{
    public enum Fruit
    {
        NoFruit, Apple, Orange
    }

    public class FruitCalc
    {
        private Dictionary<Fruit, Func<int, int, Fruit>> CalcFruitDict
            = new Dictionary<Fruit, Func<int, int, Fruit>>
            {
                // lambda notation used here
                {Fruit.Apple, (int napples, int noranges) => napples > noranges ? Fruit.Apple : Fruit.NoFruit },
                {Fruit.Orange, (int napples, int noranges) => napples < noranges ? Fruit.Orange : Fruit.NoFruit },
            };

        public Fruit Calculate(Fruit fruit, int napples, int noranges)
        {
            if (fruit == Fruit.NoFruit)
            {
                throw new InvalidOperationException("Sorry, no fruit means no fruit");
            }

            // check integer parameters for valid range ?

            return CalcFruitDict[fruit](napples, noranges);
        }
    }
}
Usage example:
FruitCalc fcalc = new FruitCalc();

Fruit f1 = fcalc.Calculate(Fruit.Apple, 12, 4);
Fruit f2 = fcalc.Calculate(Fruit.Orange, 14, 24);

Fruit f3 = fcalc.Calculate(Fruit.Apple, 2, 4);
Fruit f4 = fcalc.Calculate(Fruit.Orange, 14, 2);

// force an error
Fruit f5 = fcalc.Calculate(Fruit.NoFruit, 12, 4);
Note: the case where number of apples equals number of oranges is not handled here.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

AnswerRe: call enum as argument in Method? Pin
BillWoodruff7-Sep-19 20:01
professionalBillWoodruff7-Sep-19 20:01 
AnswerRe: call enum as argument in Method? Pin
OriginalGriff7-Sep-19 21:03
mvaOriginalGriff7-Sep-19 21:03 
AnswerRe: call enum as argument in Method? Pin
Gerry Schmitz8-Sep-19 9:09
mveGerry Schmitz8-Sep-19 9:09 
AnswerRe: call enum as argument in Method? Pin
Sharp Ninja9-Sep-19 5:56
Sharp Ninja9-Sep-19 5:56 
QuestionVlookup a Value on a CSV file C# Pin
Member 1457406731-Aug-19 10:52
Member 1457406731-Aug-19 10:52 
AnswerRe: Vlookup a Value on a CSV file C# Pin
OriginalGriff31-Aug-19 20:55
mvaOriginalGriff31-Aug-19 20:55 
AnswerRe: Vlookup a Value on a CSV file C# Pin
BillWoodruff1-Sep-19 13:00
professionalBillWoodruff1-Sep-19 13:00 
Questionreading text file as an array by using streamreader Pin
Member 1457152028-Aug-19 20:37
Member 1457152028-Aug-19 20:37 
AnswerRe: reading text file as an array by using streamreader Pin
OriginalGriff28-Aug-19 20:56
mvaOriginalGriff28-Aug-19 20:56 
AnswerRe: reading text file as an array by using streamreader Pin
Richard MacCutchan28-Aug-19 22:27
mveRichard MacCutchan28-Aug-19 22:27 
QuestionRe: reading text file as an array by using streamreader Pin
Maciej Los28-Aug-19 22:42
mveMaciej Los28-Aug-19 22:42 
AnswerRe: reading text file as an array by using streamreader Pin
Maciej Los28-Aug-19 23:32
mveMaciej Los28-Aug-19 23:32 
AnswerRe: reading text file as an array by using streamreader Pin
BillWoodruff29-Aug-19 0:04
professionalBillWoodruff29-Aug-19 0:04 
AnswerRe: reading text file as an array by using streamreader Pin
Gene M10-Sep-19 15:48
Gene M10-Sep-19 15:48 
AnswerRe: reading text file as an array by using streamreader Pin
Lutosław12-Sep-19 10:35
Lutosław12-Sep-19 10:35 
QuestionTool to add code in my existing class Pin
Bastien Vandamme28-Aug-19 6:48
Bastien Vandamme28-Aug-19 6:48 
AnswerRe: Tool to add code in my existing class Pin
Richard MacCutchan28-Aug-19 6:54
mveRichard MacCutchan28-Aug-19 6:54 

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.