0

I am currently thinking of a way to store checkbox values (true/false) in a database, without using a boolean column for each checkbox. Is there a way to store them in a byte that contains the information for, let's say, six checkboxes?

For example:
* Checkbox 1 to 6 all unchecked would be 00000000
* Checkbox 1 to 6 all checked would be 00111111
* Checkbox 1 checked, rest unchecked would be 00000001
* Checkbox 3 and 4 checked, rest unchecked would be 00001100
* etc.

BEFORE EDIT: "In the end there would be one byte column and for each row a different combination for checkboxes checked/unchecked."

AFTER EDIT: Oh I see I've been unclear. I didn't mean that each possible combination of checkboxes is stored in the table in different rows, like a lookup table. It's more like, the whole table contains information from a user entry in a formular and the checkboxes which were checked by the user are just one element of the form (one column of a db row). Therefore each user entry of the form (a row) CAN contain a different byte in the checkboxes column. But if more users chose the same checkboxes, the byte in their row would look the same of course.

Would this make sense as an alternative to a bunch of boolean columns (one for each checkbox)?

Is there a better way to handle checkbox values in the db?

Is there a way to set each bit of a byte manually in C#? I can't seem to find an explanation on how to do this.

Vitriol
  • 49
  • 1
  • 6
  • 1
    sure, a [flagset enum](https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute?view=netframework-4.7.2) stored as a byte, an int or a bigint, and/or some [bit manipulation](https://stackoverflow.com/a/24250656/1132334) – Cee McSharpface Nov 12 '18 at 20:56
  • Why would gluing many pieces if data together into one value be better than "a bunch" of values? – Ňɏssa Pøngjǣrdenlarp Nov 12 '18 at 21:03
  • What you might gain in less space requirements, you will lose for almost every other [system quality attribute](https://en.wikipedia.org/wiki/List_of_system_quality_attributes) – rene Nov 12 '18 at 21:13
  • You could use types such as [`System.Collections.BitArray`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.bitarray) or (maybe better here) [`System.Collections.Specialized.BitVector32`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.bitvector32). – Jeppe Stig Nielsen Nov 12 '18 at 21:22
  • @dlatikay Thanks I'll look those up. – Vitriol Nov 13 '18 at 08:35
  • @Disaffected1070452 and rene I just had this question and try to find a solution I don't know yet, since I am learning. It's for more understanding – Vitriol Nov 13 '18 at 08:38
  • @Jeppe Stig Nielsen Thanks I'll look into those. – Vitriol Nov 13 '18 at 08:38

2 Answers2

1

You have to define the enum size for byte, and must have an attrbute FlagAttribute.

Here is the sample code for it:

[Flags]
public enum BitFields : byte
{
    CheckBox_01 =      0,
    CheckBox_02 = 1 << 0,
    CheckBox_02 = 1 << 1,
    CheckBox_02 = 1 << 2,
    CheckBox_02 = 1 << 3,
    CheckBox_02 = 1 << 4,
    CheckBox_02 = 1 << 5
}

After you can us if like in C++:

BitFields bits = BitFields.None;

bits |= BitFields.CheckBox_01| BitFields.CheckBox_01;
bits &= ~BitFields.CheckBox_04;

when you write this one byte size enum value, you will get the result what you want.

regrads

György Gulyás
  • 1,290
  • 11
  • 37
  • Thank's alot I will try this! :) – Vitriol Nov 13 '18 at 08:38
  • @Vitriol, I get the impression your question is actually about database storage. If that is so, you should use the [TSQL Bit datatype](https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-2017), this way you can treat the values as `bool`s, avoiding bit-wise arithmetic and MS SQL Server will optimize the storage transparently. – Jodrell Nov 13 '18 at 08:48
0

You could store a single byte and map different values to checkboxes being checked. IE: 00000000 no button is checked, 00111111 all 6 buttons are checked. This would be a lot of extra work though and doesn't follow good design principles.

If persistent button state is important then I recommend you make a table called buttonstate, storing the state of each button as a row. this will reduce complexity and allow your application to scale better, make the code more manageable, and a whole other host of benefits.

TLDR; don't overcomplicate this, just store the values as booleans. pre-mature optimization is a bad idea.

spencer.pinegar
  • 442
  • 2
  • 10
  • Hi, thanks for your answer! Apparently I explained something wrong in my question, I tried to make it more clear. My intention isn't to have all possibles combinations of those checkboxes in the table and call it like "checkbox possibilities" or something. The checkbox information are just one part of information of a whole formular. Each user entry is stored in one row and the "which checkboxes are checked?" is one column. – Vitriol Nov 13 '18 at 08:31