Treat return in class in C #

2

I have a Mesa class:

public class Mesa
{
   public int Cdmesa { get; set; }
   public int Flsituacao { get; set; }
   public string Status { get; set; }       
}

I would like to return the Status with the following conditions:

  

If Status = 1 return Status "FREE"

     

If Status = 2 return Status "OCCUPIED"

Is it possible to do this directly in the class?

    
asked by anonymous 28.10.2016 / 17:39

2 answers

8

You can define a get; set; custom.

public class Mesa
{
    public int Cdmesa { get; set; }
    public int Flsituacao { get; set; }
    public string Status 
    { 
        get
        {
            switch (this.Flsituacao)
            {
                case 1: return "LIVRE";
                case 2: return "OCUPADO";
                default: return "SITUACAO NÃO ENCONTRADA"
            }
        }
        set
        {
            switch (value)
            {
                case "LIVRE": this.Flsituacao = 1; break;
                case "OCUPADO": this.Flsituacao = 2; break;
            }
        }
    }       
}

But in this case, the most rational thing is to use an enumerator.

public enum MesaSituacao : int
{
    [Display(Name = "LIVRE")]
    Livre = 1,
    [Display(Name = "OCUPADO")]
    Ocupado = 2,
    [Display(Name = "RESERVADO")]
    Reservado = 3,
}

public class Mesa
{
    public int Cdmesa { get; set; }
    public MesaSituacao Flsituacao { get; set; }    
}
    
28.10.2016 / 17:47
4

Depends on what you call directly in the class. If so, there is no way. But you can do it on the property.

public class Mesa {
    public int Cdmesa { get; set; }
    public int Flsituacao { get; set; }
    public string Status { 
        get {
            if (Flsituacao == 1) {
                return "LIVRE";
            }
            if (Flsituacao ==  2) {
                return "OCUPADO";
            }
        }
    }
}

I will not kick what set should do since the question does not say what it is. If you have to send the text and it has to set Flsituacao I see no advantage in having both of them but thinking for the future.

    
28.10.2016 / 17:50