Treat return in class in C#

Asked

Viewed 213 times

2

I have a class Table:

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 Flsituacao = 1 return Status "FREE"

If Flsituacao = 2 return Status "BUSY"

It is possible to do this directly in the class?

  • Can you put other relevant parts of the class? I can answer this, but it will be "lucky".

  • The class is very large and I can’t copy because it’s on another computer without internet. What else would be relevant?

  • 1

    I said relevant parts. I gave an answer, but only with this I don’t know if it’s right. Or you can give more details. Note that I did what seems to be what you want, but it’s not right, I don’t know what you should do with the set. See if you think you need to change something in the answer.

2 answers

8


you can set a custom get;set;.

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 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; }    
}

4

It depends on what you call directly in the class. If so, there is no way. But you can do it in 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";
            return "RESERVADO";
        }
    }
}

I put in the Github for future reference.

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

  • I really had no need to implement SET and I ended up taking. The two answers are right. Thanks

  • @leopiazzoli I imagine that both help, but since they are different only one attended you better and that’s what you decided to do, you can choose either of the two.

  • Ah, I see now you’ve chosen the other.

  • I used the answer that has SWITCH in case it adds more situations in the future. But also the SET was unnecessary. :)

  • @leopiazzoli I thought about doing, but in this case it’s exactly the same, except for the fact that it has a value default, I did not do it because in your question there is no that, there is no what you should do if you come a wrong value, if you can never see a wrong value (which is what I considered, by the menso is the right one to do), ie I was in the most correct logic for the case, according to the described.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.