7
\$\begingroup\$

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?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ When you say 'save space' do you mean in characters in a file or memory? If you mean the latter you'd be misteken, as an int (actually System.Int32) takes you 4 bytes whereas a bool (System.Boolean) only uses 1 byte. Reference: msdn.microsoft.com/en-us/library/eahchzkf.aspx \$\endgroup\$
    – Mark
    Commented Aug 21, 2014 at 6:10

3 Answers 3

8
\$\begingroup\$

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()...

\$\endgroup\$
7
  • \$\begingroup\$ I convert them to integers to save a bit of space, since there is up to 96 different values. It's not much, but I figure it helps. :P \$\endgroup\$
    – user51552
    Commented Aug 21, 2014 at 1:12
  • \$\begingroup\$ The order of arguments to string.Join is reversed. You can also just write string.Join(",", bs.Select(b => b ? '1' : '0')). \$\endgroup\$
    – mjolka
    Commented Aug 21, 2014 at 4:42
  • \$\begingroup\$ Good catch @mjolka - I updated the answer with your suggestions / fix... \$\endgroup\$ Commented Aug 21, 2014 at 4:52
  • \$\begingroup\$ .Select(x=> x == '1') \$\endgroup\$
    – NPSF3000
    Commented Aug 21, 2014 at 9:33
  • \$\begingroup\$ @NPSF3000, it should be "1", not '1' since you have a string[] \$\endgroup\$
    – IEatBagels
    Commented Aug 21, 2014 at 13:42
7
\$\begingroup\$
  • 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.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ when the name of blenabled is changed to a enabledBools then instead of t the variable should be enabledBool because that makes sense. \$\endgroup\$
    – Malachi
    Commented Aug 22, 2014 at 13:29
5
\$\begingroup\$

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:

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Correction: BitArray is resizable, BitVector32 is always 32 bits. \$\endgroup\$ Commented Jul 23, 2019 at 11:57
  • 1
    \$\begingroup\$ Fortunately answers are dynamic, so they can be fixed. ;) \$\endgroup\$ Commented Jul 23, 2019 at 14:15
  • 4
    \$\begingroup\$ @PieterWitvoet And we have good C# community here, helping each other out! :) \$\endgroup\$
    – dfhwze
    Commented Jul 23, 2019 at 14:18
  • 5
    \$\begingroup\$ ...and an archaeologist ;-) \$\endgroup\$
    – t3chb0t
    Commented Jul 23, 2019 at 14:40
  • 1
    \$\begingroup\$ Neat! BitArray appears to be serializable too! \$\endgroup\$
    – RubberDuck
    Commented Jul 24, 2019 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.