Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below enum. its value will compared with some input parameter value.

I need your help in writing condition checks.

C#
public enum DiscountMode
    {
        NoInput,
        Amount,
        Percentage,
        SplDiscount,
        
    }

C#
public someobject CalculateDiscount(int DisType, int DisValue)
{
if(DisType == DiscountMode.Amount || DisType == DiscountMode.Percentage)
{
//do logic
}
else if(DisType == DiscountMode.SplDiscount || DisType == DiscountMode.FullDiscount)
{
//do logic
}
else if(DisType == DiscountMode.NoInput)
{
//do logic
}

}


How to achieve this logic.

What I have tried:

I have tried like above but enum convert & operator related errors showing.
Posted
Updated 21-Sep-22 7:55am
Comments
Richard MacCutchan 21-Sep-22 11:24am    
If you get errors in your code or execution than please provide the exact text of the msseages, and the line of code that causes the error. No one here can guess what you mean by "errors showing.
.

Cast it:
C#
public enum DiscountMode
{
	NoInput,
	Amount,
	Percentage,
	SplDiscount,
	FullDiscount,
}
public class Program
{
	public static void Main()
	{
	CalculateDiscount(1, 666);	
	}
	public static void CalculateDiscount(int DisType, int DisValue)
	{
		DiscountMode disType = (DiscountMode) DisType;
		if(disType == DiscountMode.Amount || disType == DiscountMode.Percentage)
		{
			//do logic
		}
		else if(disType == DiscountMode.SplDiscount || disType == DiscountMode.FullDiscount)
		{
			//do logic
		}
		else if(disType == DiscountMode.NoInput)
		{
			//do logic
		}
	}
}
 
Share this answer
 
Yeah, just cast it, and try a switch as well.

C#
switch ( (DiscountMode) DisType )
{
  case DiscountMode.Amount :
  case DiscountMode.Percentage :
  {
    //do logic
    break ;
  }

  case DiscountMode.SplDiscount :
  case DiscountMode.FullDiscount :
  {
    //do logic
    break ;
  }

  case DiscountMode.NoInput :
  {
    //do logic
    break ;
  }
}
 
Share this answer
 
v2
Comments
OriginalGriff 21-Sep-22 13:59pm    
I did start by recommending that, but took it out before I posted because the clauses ORed, and C# won't let you drop though cases.

So you'd need to call methods in the cases - which is a good idea anyway - but it can add a level of complication I'm not sure the OP is ready for yet ...
PIEBALDconsult 21-Sep-22 14:26pm    
"C# won't let you drop though cases." -- Fall-through, no, fall-through was never a good idea. But you can jump from one to another, but there's no need to in this case.
Virendra S from Bangalore, Karnataka 21-Sep-22 14:17pm    
If I go with switch case statements, there will be code duplications in my requirement because I have 3 logical parts with 5 possible inputs.
With If..Else I need to write calculation logic 3 times only. in case of Switch statement I have write for 5 times..
Let me if I'm wrong..
PIEBALDconsult 21-Sep-22 14:21pm    
Not a concern. I'll update.
Virendra S from Bangalore, Karnataka 22-Sep-22 3:46am    
Perfect!!. Now this makes sense to me.

I would use the new switch syntax that was introduced in c#8. It removes the need for if, then, else statements and gives good separation of concerns. The method of choosing is separate to the result of the choice. Something like this.

C#
public static void CalculateDiscount(int DisType, int Disvalue)
   {
       DiscountMode discountMode = (DiscountMode)DisType;
       double discountedAmount = discountMode switch
       {
           DiscountMode.Amount => CalculateDiscountPercent(Disvalue),
           DiscountMode.Percentage => CalculateDiscountPercent(Disvalue),
           DiscountMode.SplDiscount => CalculateDiscountFull(Disvalue),
           DiscountMode.FullDiscount => CalculateDiscountFull(Disvalue),
           DiscountMode.NoInput => CalculateDiscountNoInput(Disvalue),
           _ => throw new ArgumentException(message: "Invalid enum value",
                                               paramName: nameof(discountMode))
       };
       //do something with  discountedAmount or return it
   }
   //in this type of switch statement the referenced methods cannot return void
   private static double CalculateDiscountPercent(int disValue)
   {
       return disValue * 0.9;
   }

 
Share this answer
 

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