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

3

I'm creating a project where I look for the database information, in this case it's a DBF file, and the field is a Logical type. However I want to create a property that can receive a variable (either int, string or bool) and write to the private variable bool.

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

I tried to create 3 properties with each data type, but when I use it it says there is more than one property with the same name. (Which would be in this 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 looks 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, and string and Bool. Is there any way to do this?

  

Edit: Would the result look like this?

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

}

    
asked by anonymous 04.08.2016 / 17:25

2 answers

5

Can not overload ( overload ) properties. You'll have to create a class that encapsulates this behavior, something like this:

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 should add some type of validation to the builders.

Constructor overload allows you to construct LogicalValue objects from an int , string , or bool / p>

Your code looks like this:

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

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

}

The usage would look like this:

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

bool valorVip = dbPort.Vip.Value;// valorVip é igual a true
    
04.08.2016 / 18:10
1

You can create two properties one of type Objct and another bool, the first one will serve as the basis for the arrow's 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;
        }
    }
}
    
04.08.2016 / 19:14