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?
-
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– Mark2014年08月21日 06:10:22 +00:00Commented Aug 21, 2014 at 6:10
3 Answers 3
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()
...
-
\$\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– user515522014年08月21日 01:12:02 +00:00Commented Aug 21, 2014 at 1:12
-
\$\begingroup\$ The order of arguments to
string.Join
is reversed. You can also just writestring.Join(",", bs.Select(b => b ? '1' : '0'))
. \$\endgroup\$mjolka– mjolka2014年08月21日 04:42:01 +00:00Commented Aug 21, 2014 at 4:42 -
\$\begingroup\$ Good catch @mjolka - I updated the answer with your suggestions / fix... \$\endgroup\$Matt Murrell– Matt Murrell2014年08月21日 04:52:53 +00:00Commented Aug 21, 2014 at 4:52
-
\$\begingroup\$
.Select(x=> x == '1')
\$\endgroup\$NPSF3000– NPSF30002014年08月21日 09:33:19 +00:00Commented Aug 21, 2014 at 9:33 -
\$\begingroup\$ @NPSF3000, it should be "1", not '1' since you have a string[] \$\endgroup\$IEatBagels– IEatBagels2014年08月21日 13:42:57 +00:00Commented Aug 21, 2014 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 suggestboolList
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 uset
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.
-
1\$\begingroup\$ when the name of
blenabled
is changed to aenabledBools
then instead oft
the variable should beenabledBool
because that makes sense. \$\endgroup\$Malachi– Malachi2014年08月22日 13:29:13 +00:00Commented Aug 22, 2014 at 13:29
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:
-
1\$\begingroup\$ Correction:
BitArray
is resizable,BitVector32
is always 32 bits. \$\endgroup\$Pieter Witvoet– Pieter Witvoet2019年07月23日 11:57:01 +00:00Commented Jul 23, 2019 at 11:57 -
1\$\begingroup\$ Fortunately answers are dynamic, so they can be fixed. ;) \$\endgroup\$Pieter Witvoet– Pieter Witvoet2019年07月23日 14:15:55 +00:00Commented Jul 23, 2019 at 14:15
-
4\$\begingroup\$ @PieterWitvoet And we have good C# community here, helping each other out! :) \$\endgroup\$dfhwze– dfhwze2019年07月23日 14:18:00 +00:00Commented Jul 23, 2019 at 14:18
-
5\$\begingroup\$ ...and an archaeologist ;-) \$\endgroup\$t3chb0t– t3chb0t2019年07月23日 14:40:07 +00:00Commented Jul 23, 2019 at 14:40
-
1\$\begingroup\$ Neat!
BitArray
appears to be serializable too! \$\endgroup\$RubberDuck– RubberDuck2019年07月24日 09:49:58 +00:00Commented Jul 24, 2019 at 9:49
Explore related questions
See similar questions with these tags.