# Converting a boolean list to string, and vice versa

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:
break;
case false:
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?

-
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 –  Mark Aug 21 '14 at 6:10

To Save:

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


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

-
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 –  user51552 Aug 21 '14 at 1:12
The order of arguments to string.Join is reversed. You can also just write string.Join(",", bs.Select(b => b ? '1' : '0')). –  mjolka Aug 21 '14 at 4:42
Good catch @mjolka - I updated the answer with your suggestions / fix... –  Matt Murrell Aug 21 '14 at 4:52
.Select(x=> x == '1') –  NPSF3000 Aug 21 '14 at 9:33
@NPSF3000, it should be "1", not '1' since you have a string[] –  TopinFrassi Aug 21 '14 at 13:42
• 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.
-
when the name of blenabled is changed to a enabledBools then instead of t the variable should be enabledBool because that makes sense. –  Malachi Aug 22 '14 at 13:29