IF Alternative C #

2

I would like to know an alternative to avoid the excessive use of IF within this code

if (paq.Lunes)
    this.lu.Attributes.Add("class", "select");
if (paq.Martes)
    this.ma.Attributes.Add("class", "select");
if (paq.Miercoles)
    this.mi.Attributes.Add("class", "select");
if (paq.Jueves)
    this.jue.Attributes.Add("class", "select");
if (paq.Viernes)
    this.vie.Attributes.Add("class", "select");
if (paq.Sabado)
    this.sa.Attributes.Add("class", "select");
if (paq.Domingo)
    this.dom.Attributes.Add("class", "select");

if (!paq.aplicaTransporte)
    this.transporte.Visible = false;
if (!paq.aplicaEntrada)
    this.entrada.Visible = false;
if (!paq.aplicaGuia)
    this.guia.Visible = false;
if (!paq.aplicaComida)
    this.comida.Visible = false;
if (!paq.aplicaSeguro)
    this.seguro.Visible = false;
if (!paq.aplicaTransporteMaritimo)
    this.maritimo.Visible = false;
if (!paq.aplicaBebidasAlcoholicas)
    this.alcohol.Visible = false;

This is the class

public class PaqueteTour
{
public Boolean Lunes { get; set; }
    public Boolean Martes { get; set; }
    public Boolean Miercoles { get; set; }
    public Boolean Jueves { get; set; }
    public Boolean Viernes { get; set; }
    public Boolean Sabado { get; set; }
    public Boolean Domingo { get; set; }
{
        //
        // TODO: Agregar aquí la lógica del constructor
        //
        precio = new preciotourlistado();
        promocion = new PromoHotel();
    }
}

I'm working on .NET C #, I've thought about using functions like .map or some of those but I can not find a way to implement it, because this information comes from an API.

    
asked by Ernesto Emmanuel Yah Lopez 08.10.2018 в 02:57
source

2 answers

5

An alternative to successive ifs is the conditional switch .

From what I see in your code paq refers to a enum , if necessary, you could do the following using switch

var paq = TuEnum.Lunes;
switch (paq)
    {          
        case TuEnum.Lunes:
        //Aquí iría la lógica en caso de ser lunes
        break;

        case TuEnum.Martes:
        //Aquí en caso de ser martes
        break;

        default:
        //Por último aquí iría la lógica si ninguno de los otros casos se cumplió.
        break;
    }

Anyway, I recommend you read the Microsoft documentation , which is not very long, uses several examples that are very easy to understand.

Regarding the second part of your code, where you evaluate, for example:

if (!paq.aplicaTransporte)
this.transporte.Visible = false;

You are using a totally unnecessary if , it would be the same to write this.transporte.Visible = paq.aplicaTransporte;

EDIT : To use enum instead of Boolean you should do the following:

Modify the definition of your class by something like the following

public class PaqueteTour
{
     public Dias DiaDeLaSemana;
     public enum Dias { Lunes,Martes,Miercoles,Jueves,Viernes,Sabado,Domingo };
}

Then to set it, you would do it in the following way: DiaDeLaSemana = Dias.Martes for example.

Finally, when you check your value, you could do it with the switch as explained above, or also with a if , which would be as follows

if(DiaDeLaSemana == Dias.Martes)
{
    //Lógica si es martes
}
    
answered by 08.10.2018 / 04:22
source
0

You could use bitwise to save multi selection in a single field.

I do not know if you know how this concept works, but basically it is based on saving the values 1,2,4,8,16 etc

Enum, Flags and bitwise operators

Create Enumeration as Bit Flags

then you could have an enum for the week to be

enum SemanaEnum{
   Lunes = 1,
   Marte = 2,
   Miercoles = 4,
   Jueves = 8,
   Viernes = 16,
   Sabado = 32,
   Domingo = 128,
}

then using the OR logical can join several of these values in a single field, you do not need a property day of the week

SemanaEnum result = SemanaEnum.Martes | SemanaEnum.Jueves;

The idea is that after you evaluate you can determine if this selection or not

if ((paq.Semana & SemanaEnum.Marte) == SemanaEnum.Marte){
   //codigo
}

Of course seeing it like this does not prevent writing if, but if a Dictionary like being

Dictionary<SemanaEnum, object> lista = new Dictionary<SemanaEnum, object>(){
   {SemanaEnum.Lunes, this.lu},
   {SemanaEnum.Marte, this.ma},
    //resto
}

foreach(var item in lista){
  if ((paq.Semana & item.Key) == item.Key){
      item.Value.Attributes.Add("class", "select");
   }
}

This way, you avoid the ifs and make your ams dynamic code

Note: I put the value of the Dictionary as object because I did not know what type it is, but the correct thing is that you put the corresponding type

    
answered by 08.10.2018 в 16:42