方法接受三个布尔参数,并在这些布尔值C#的基础上返回字符串



i带有一种接受三个布尔参数并返回字符串值的方法。例如,我在表列中保存0,1,2。我有三个布尔变量isViewisAddUpdateisDelete。当isViewtrue时,仅保存0,如果isViewisAddUpdate比保存0,1为TRUE,并且如果所有这些都是正确的,则比保存0,1,2。

这是我的代码。请建议我一种更好的方法来实现这一目标。

public string getActions(bool isView, bool isAddupdate, bool isDelete)
{
    try
    {
        var _val = "";
        if (isView && isAddupdate && isDelete)
            _val = "0,1,2";
        if (isView && isAddupdate && !isDelete)
            _val = "0,1";
        if (isView && !isAddupdate && !isDelete)
            _val = "0";
        if (!isView && !isAddupdate && !isDelete)
            _val = "";
        if (!isView && !isAddupdate && isDelete)
            _val = "2";
        if (!isView && isAddupdate && isDelete)
            _val = "1,2";
        return _val;
    }
    catch (Exception ex)
    {
        throw ex; 
    }
}  

也许是?

public string getActions(bool isView, bool isAddupdate, bool isDelete)
{
    var codes = new List<int>();
    if (isView) codes.Add(0);
    if (isAddupdate) codes.Add(1);
    if (isDelete) codes.Add(2);
    return string.Join(",", codes);
}
public string getActions(bool isView, bool isAddupdate, bool isDelete)
{
    string[] values = new string[3];
    if (isView)
        values[0] = "0";
    if (isAddupdate)
        values[1] = "1";
    if (isDelete)
        values[2] = "2";
    return String.Join(",", values.Where(s => !string.IsNullOrEmpty(s)));
}

我删除了try/catch块。我没有看到任何原因。

也许是另一种方法,但请尝试使用[Flags]枚举

类似的东西一般可能会有所帮助

[Flags]
. public enum Actions
. {
.   None = 0,
.   View = 1,
.   AddUpdate = 2,
.   Delete = 4
. }

如果您想坚持使用Bool Inputs

这样的东西
public string getActions(bool isView, bool isAddUpdate, bool isDelete)
{
    var a = isView ? Actions.View : Actions.None;
    a |= isAddUpdate ? A.AddUpdate : Actions.None;
    a |= isDelete ? Actions.Delete : Actions.None;
    return a.ToString();
}

假设 isView是正确的, isAddUpdate是错误的, isDelete是正确的,这将返回

"View, Delete"

如果您使用的是关系数据库,例如SQL Server,MySQL或PostgreSQL,那么我建议您将这些值存储为比较比较的位,因为它比存储3个字符字符串便宜。

但是,如果您需要以这种方式进行操作或使用MongoDB之类的东西,那么我建议使用此解决方案,不要存储1,2,3存储二进制字符(1或0)以准确表示布尔值。

请参阅下面的代码以获取插图:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var stringvalue = getActions(false, true, false); //this result you store to your db
            Console.WriteLine(stringvalue);
            Console.ReadLine();
            var deserialised = DeserialiseActions(stringvalue); //you would have retrieved this from the database
        }
        public static string getActions(bool isView, bool isAddupdate, bool isDelete)
        {
            return $"{Convert.ToSByte(isView).ToString()}{Convert.ToSByte(isAddupdate).ToString()}{Convert.ToSByte(isDelete).ToString()}";
        }
        public static ActionsCollection DeserialiseActions(string dataValue)
        {
            return new ActionsCollection
            {
                IsView = bool.Parse(dataValue[0].ToString()),
                IsUpdate = bool.Parse(dataValue[1].ToString()),
                IsDelete = bool.Parse(dataValue[2].ToString())
            };
        }
    }
    class ActionsCollection
    {
        public bool IsView { get; set; }
        public bool IsUpdate { get; set; }
        public bool IsDelete { get; set; }
    }
}

最新更新