Create a property for a bool variable that handles string and int

Asked

Viewed 985 times

3

I am creating a project in which I search the database information, in this case it is a DBF file, and the field is of the Logical type. However, I want to create a property that can receive a variable (either int, string or bool) and save to the bool private variable.

I already have methods that receive int (0/1) and transform into (True/False) and that receive string (T/F | V/F | S/N) and transform into (True/False). But I would like to do the treatment when "setando" the value of the field.

I tried to create 3 properties with each type of data, but when I use it says that there is more than one property with the same name. (Which would be in that case).

public override int Vip
{
    get { return clsGeneric.convertFromBool(vip); }
    set { vip = clsGeneric.convertToBool(value); }
}
public override string Vip
{
    get { return clsGeneric.convertFromBool(vip,clsGeneric.TypeRetBool.TF); }
    set { vip = clsGeneric.convertToBool(value); }
}
public override bool Vip
{
    get { return vip; }
    set { vip = value; }
}

And now it’s like this:

public override TipoGenérico Vip
{
    get { return vip; }
    set
    {
        if (value.GetType() == typeof(string))
        {
            vip = clsGeneric.convertToBool((string)value);
        }
        else if (value.GetType() == typeof(int))
        {
            vip = clsGeneric.convertToBool(value);
        } else {
            vip = value;
        }
    }
}

I wanted to make the property accept both Int, string and Bool. Is there any way to do this?

Edit: The result would be this way?

Class DBPort
{
    private bool vip;
    public LogicalValue Vip
    {
        get { return vip; }
        set { vip = value.Value; }
    }
    public DBPort()
    {
        Vip = false;
    }

}

  • That way it doesn’t work?

2 answers

5


It is not possible to overload(Overload) property.
You have to create a class that encapsulates this behavior, anything like that:

public class LogicalValue
{

    public bool Value { get; }

    public LogicalValue(int value)
    {
        Value = clsGeneric.convertToBool(value);
    }


    public LogicalValue(string value)
    {
        Value = clsGeneric.convertToBool(value);
    }

    public LogicalValue(bool value)
    {
        Value = value;
    }
}

Note: You must add some kind of validation to the constructors.

The overload of the constructor allows to build objects Logicalvalue from a int, string or bool.

Your code would look like this:

Class DBPort
{
    public LogicalValue Vip { get; set;}

    public DBPort()
    {
        Vip = new LogicalValue(false);
    }

}

The use would be thus:

DBPort dpPort = new DBPort();
dbPort.Vip = new LogicalValue(true);ou (1) ou ("S")

bool valorVip = dbPort.Vip.Value;// valorVip é igual a true
  • I don’t understand how I can use this class to set the value in the property

  • Your property instead of the type bool becomes LogicalValue. To "set it" do nomeClasse.NomeProp = new LogicalValue(aqui pode ser ser bool ou int ou string); to get the value do bool valor = nomeClasse.NomeProp.Value;

  • I apologize for the ignorance, but I still do not understand, in question I put the code (sorry if I am not in the patterns here)

  • See the Reply Edition.

1

You can create two properties one of type Objct and the other bool, the first will serve as the basis for arrow the value of the other.

namespace stackoverflow
{
    public partial class AlterarTipo : System.Web.UI.Page
    {
        private bool vip { get; set; }
        public  object Vip
        {
            get { return vip; }
            set
            {
                if (value.GetType() == typeof(string))
                {
                    vip = Convert.ToBoolean(value);
                }
                else if (value.GetType() == typeof(int))
                {
                    vip = Convert.ToBoolean(value);
                }
                else {
                    vip = Convert.ToBoolean(value);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Vip = "false";
            Vip = 1;
            Vip = true;
            Vip = false;
        }
    }
}
  • This answer is not good. It violates SOLID and get is insecure.

  • @Gypsy omorrisonmendez, do a better one, I’ll be glad to see how it can be done.

  • Has been done. My suggestion is to strongly type the property Vip, instead of a object.

Browser other questions tagged

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