From: Mythran on

"Anthony Papillion" <papillion(a)gmail.com> wrote in message
news:uaJuF90mKHA.4436(a)TK2MSFTNGP02.phx.gbl...
>
> "Andrew Morton" <akm(a)in-press.co.uk.invalid> wrote in message
> news:7rtablFubvU1(a)mid.individual.net...
>> 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

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


From: Branco Medeiros on
On Jan 21, 6:24 am, "Anthony Papillion" <papill...(a)gmail.com> wrote:
> 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