Easiest Way to Do This

Hey guys,

I’ve got 3 checkboxes that I want to take and create a binary representation from their settings.

So if the first and last box are checked, I want to generate 101.

I know how I could do this by checking the state of each box and generating a series of strings (“0” or “1”) and then concatenating them all together at the end.

I’m just wondering if there’s a better way that I don’t see at this time…

What about making an array of the three objects (mBox(2)), with a boolean property (mBoxState(2))?
In the three objects open event: me.value = mBoxState(index)
In the three objects action event: mBoxState(index) = me.value
Then for refreshing purposes, (and similarly for saving/retreiving their status in prefs file):
for i as integer = 0 to 2
mBox(i).value = mBoxValue(i)
next

@Carlo: You forgot the join at the end …

I should have been more specific. I want to save the state of the checkboxes to a database. I could do a column for each but I have three sets of these and rather than add 9 columns I figured I’d write each one as basically their binary word.

So I made a subclass of checkbox with a string property called “BinaryValue”

Then in the ValueChanged event, I added:

If me.Value Then
   BinaryValue = "1"
Else
   BinaryValue = "0"
End If

RaiseEvent ValueChanged

Now, when I want the binary representation of the three it’s just:

 Dim b as string = checkbox1.binaryvalue+checkbox2.binaryvalue+checkbox3.binaryvalue

If there’s an easier way, I’m all ears…

You could make a Control Set out of them, I guess, and have them set the value of an array when changed. But honestly, if it will only ever be three checkboxes, this is a good solution.

Yeah, a control set could work, but yeah, it’s just going to be 3 at this point. Well, 3 sets of 3. I could make it into one big 9 bit word, but it’s easier working with 3 sets of 3 bits for how I want to save/recall things.

Don’t forget that a CheckBox can have THREE states (but only TWO values):

CB1 Open event: me.State = CheckBox.CheckedStates.Checked
CB2 Open event: me.State = CheckBox.CheckedStates.Indeterminate
CB3 Open event: me.State = CheckBox.CheckedStates.Unchecked

MsgBox Str( CB1.State ) + Str( CB2.State ) + Str( CB3.State ) -> “120”

From experience, you will probably want a column for each one. Appending data into a single column may seem like a good idea but every time I’ve ever done it I’ve regretted it later. 9 columns isn’t such a big deal.

1 column 1 value

You do the same amount of work and increase the risk * the number of values in the column.
It’s no harder to read/write 3 values/3 columns than to parse/write - Read/parse 3 values/1 column.

Thanks for the suggestions guys. Based on the comments and number of “likes” it looks like people think I should do a column per checkbox. So I’m easy. I’ll take your suggestions! :slight_smile:

I’d suggest trying to find a way to do it as rows of data instead of columns because have 3 checks now in columns and wanting to add one 2 or 30 then means more columns
More rows (or fewer rows) is easy as its just more or less rows of data

Norman, do you mean another database table?
something like id, description and boolean value?

Jon’s trying to store some UI state

And I’d hazard an educated guess that over time storing more & more UI state will be useful (but thats just a guess)

So designing something that lets you retain UI state and add / remove items fairly easily is an advantage (kind of like adding & removing things from a project file)

A database that has key value pairs lets you store whatever you want (now some db’s may force you to have key + many columns for values - integer, string etc) or you serialize everything to a string and save key + stringvalue and reconstitute it from that (the IDE basically does this for VCP and XML projects)

That way you can have as many (or as few) keys + values as you want and IF you need more you dont have to redesign the db to add one more item

OK,
I understand…
I’m studying a little the wordpress blogs structure this days,
and it seems to handle a lot things this way, having much things saved as key + value, where the value can be a json string containig multiple properties.
that’s a good way to do, at least where the info are not something searchable directly with a sql query
thanks Norman, your suggestion are always precious

Giulio

not sure I’d do values as numerous json properties as it certainly reduces searchability in many ways

for instance the IDE might save a window as
window.propertyname + value
for each property on a window
each control on a window might be
winow.controlname(index if its a control array).propertyname + value

this is sort of off the top of my head but you get the idea