简体   繁体   中英

Method accept three boolean parameter and return string on bases of these boolean values C#

I came with a method which accepts three bool parameters and returns a string value. For example, I am saving 0,1,2 in a table column. I have three boolean variables isView , isAddUpdate and isDelete . When isView is true then save only 0, if isView and isAddUpdate are true than save 0,1 and if all of them are true than save 0,1,2.

Here is my code. Please suggest me a better approach to achieve this.

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

Something like this, perhaps?

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

I removed the try/catch block. I don't see any reason for it.

Maybe a different approach but try using [Flags] enum

Something like this might help in general

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

Something like this if you wanna stick with 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();
}

Suppose isView is true, isAddUpdate is false, and isDelete is true, this will return

"View, Delete"

If you are using a relational database such as SQL Server, MySql or PostgreSQL then i suggest you store those values as bits as it will be cheaper than storing a 3 character string.

But if you need to do it this way or if you are using something like MongoDB then i propose this solution instead, dont store 1,2,3 store binary characters (1 or 0) to accurately represent boolean values.

See my code below for illustration:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM