What is the best way to approach this?

A

Anthony Papillion

Hello Everyone,

I'm writing an application where the user will need to select any
combination of several options represented by a series of checkboxes. I then
need to save certain information in a flat file based on what the user
selected. For example, if the user selected checkbox1 and checkbox4 I will
save text that represent checkbox1 and checkbox4, if they select others, I
will also save text that represents those values.

Each time the application loads, I'm going to load these values and make
decisions based on what the user selected. Simple, I know. Right now, I'm
storing the values in one long colon delimited string like:

checkbox1:checkbox3:checkbox4

which I then read into an array using the Split() method. That works very
well IF the user selects more than one checkbox. But, if the user ONLY
selects one checkbox then the code gets a bit more complicated as I don't
want to store the delimiting colon since I won't need to perform the split.

I know this isn't the most elegant way to accomplish this and I'd like
opinions on how I should best handle this? Do I need to delimit the values
at all and, if so, what is the best way to do so?

Thanks in advance,
Anthony
 
A

Andrew Morton

Anthony said:
Right now, I'm storing the values in one long colon delimited string
like:
checkbox1:checkbox3:checkbox4

which I then read into an array using the Split() method. That works
very well IF the user selects more than one checkbox. But, if the
user ONLY selects one checkbox then the code gets a bit more
complicated as I don't want to store the delimiting colon since I
won't need to perform the split.

There's an option with the Split() method to not return empty items in the
result, so "checkbox1:" would result in {"checkbox1"}, and then you don't
need to treat the single entry differently from multiple entries.

Dim result() as String
result = yourString.Split(":"c, StringSplitOptions.RemoveEmptyEntries)

Andrew
 
A

Anthony Papillion

Andrew Morton said:
There's an option with the Split() method to not return empty items in the
result, so "checkbox1:" would result in {"checkbox1"}, and then you don't
need to treat the single entry differently from multiple entries.

Dim result() as String
result = yourString.Split(":"c, StringSplitOptions.RemoveEmptyEntries)

Andrew

Thank you, Andrew. I'll look into this flag and see if that's what I need.
Looks like it so far though.

Anthony
 
A

Andrew Morton

The alternative is to remove the final ":" when you've built the list,

pseudo-code:

Dim sb as New Stringbuilder
For each checkbox
if checkbox.checked then
sb.Append(checkboxName)
sb.Append(":")
end if
next
if sb.length>1 then
sb.length=sb.length-1
end if

Which does make the file look neater.

Andrew
 
A

Anthony Papillion

Andrew Morton said:
The alternative is to remove the final ":" when you've built the list,

pseudo-code:

Dim sb as New Stringbuilder
For each checkbox
if checkbox.checked then
sb.Append(checkboxName)
sb.Append(":")
end if
next
if sb.length>1 then
sb.length=sb.length-1
end if

Which does make the file look neater.

Andrew

Andrew,

I do think this is the better solution. Thanks again!

Anthony
 
M

Mythran

Anthony Papillion said:
Andrew,

I do think this is the better solution. Thanks again!

Anthony

Or, so you don't have to worry about removing after adding:

Dim sb As New StringBuilder
For Each checkbox In whatever
If checkbox.Checked
If sb.Length > 0
sb.Append(":")
End If
sb.Append(checkboxName)
End If
Next

and if you're using 2.0+ version of the framework:

Dim sb As New StringBuilder
For Each checkbox In whatever
If checkbox.Checked
sb.Append(If(sb.Length > 0, ":", String.Empty) & checkboxName)
End If
Next

Which makes it short and sweet...but I would take it one step further and
write a function to append strings with a delimiter....

HTH,
Mythran
 
B

Branco Medeiros

Hello Everyone,

I'm writing an application where the user will need to select any
combination of several options represented by a series of checkboxes. I then
need to save certain information in a flat file based on what the user
selected.  For example, if the user selected checkbox1 and checkbox4 I will
save text that represent checkbox1 and checkbox4, if they select others, I
will also save text that represents those values.

Each time the application loads, I'm going to load these values and make
decisions based on what the user selected. Simple, I know.  Right now, I'm
storing the values in one long colon delimited string like:

checkbox1:checkbox3:checkbox4

which I then read into an array using the Split() method. That works very
well IF the user selects more than one checkbox. But, if the user ONLY
selects one checkbox then the code gets a bit more complicated as I don't
want to store the delimiting colon since I won't need to perform the split.

I know this isn't the most elegant way to accomplish this and I'd like
opinions on how I should best handle this? Do I need to delimit the values
at all and, if so, what is the best way to do so?

Thanks in advance,
Anthony

I usually use a List(Of String) which later I String.Join:

<aircode>
Dim L as New List(Of String)
For Each C As CheckBox In New CheckBox(){ _
Checkbox1, Checkbox2, Checkbox3, _
Checkbox4, Checkbox5, Checkbox6 _
}
If C.Checked then L.Add(C.Name)
Next
Dim Text As String = String.Join(":", L.ToArray)
</aircode>

HTH

Regards,

Branco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top