Converting a boolean list to string, and vice versa

Posted on

Problem

I am currently using this to convert a List of booleans to a string

    var tempbllist = new List<int>();
        foreach (var t in blenabled)
            switch (t)
            {
                case true:
                    tempbllist.Add(1);
                    break;
                case false:
                    tempbllist.Add(0);
                    break;
            }

        Settings.Default.blEnabled = String.Join(",",
            tempbllist.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToList());

(converts them to 1s and 0s to save space)

and this to convert them back

if (Settings.Default.blEnabled.Contains(","))
            blenabled =
                new List<bool>(
                    Settings.Default.blEnabled.Split(',').Select(Int32.Parse).Select(i => i == 1).ToList());

What is a better way of doing this?

Solution

How about this:

To Save:

Settings.Default.blEnabled = string.Join(",", blenabled.Select(x=>x?"1":"0"));

To Load:

if(!string.IsNullOrWhitespace(Settings.Default.blEnabled))
  blenabled = Settings.Default.blEnabled
    .Split(',')
    .Select(x=>Convert.ToBoolean(int.Parse(x)))
    .ToList();

Is there some requirement that you save them as integers? It could be further simplified by just using bool.ToString() and bool.Parse()

  • tempbllist? Why are we dsmvwlng? I’m sure you can find something better in the greater context of your code, but lacking that context, I can at least suggest boolList instead.
  • Seriously, what’s with these variable names? What’s a blen-a-bled? Ohhhh you mean boolEnabled? But it’s an enumerable of some kind, wouldn’t if be more clear if it was plural? Maybe… enabledBools instead?

        foreach (var t in blenabled)
    
  • I almost forgot t…. Single letter variable names are frustrating. It’s worse when people use t because it’s an awful lot like <T>.

Yeah. That was harsh. I know and I’m sorry that I seem to have lost my filter for a moment. Naming is hard. Really, really hard, but it’s also incredibly important. Names in the code tell other devs what you were thinking when you wrote the code. They’re better than comments when it’s done right. Heck, good names replace comments when it’s done right. This is not naming done right.

Ok. I’m done picking on the naming. I’ve just one more axe to grind.

  • Switches are great. Seriously, I think they’re the best thing since peanut butter and pickle sandwiches, but they’re not always the right tool for the job. We’ve got more than a hammer and not every problem is a nail. A simple If...Else is perfect here. Why? Because there will only ever be two paths. It’s true or it’s false. There will never be another case.

I challenge the need for a list of booleans. There are alternatives available in the .NET Framework that deal with a sequence of booleans.


If the flags are static and fixed:

[Flags]
enum IGreetYou : uint
{
    None  = 0,
    Hi    = 1 << 0,
    Hello = 1 << 1,
    Yow   = 1 << 2
}

with a simple conversion to string as:

var value = IGreetYou.Hi | IGreetYou.Hello;
var asString = Convert.ToString((uint)value, 2);   // 11

If the size of the collection is dynamic:


If the size of the collection is fixed:

Leave a Reply

Your email address will not be published. Required fields are marked *