[RESOLVED] [2005] Problem using dynamic integer array
I am using a 3rd-party class to create Scheduled Tasks. One of the methods of this class expected an Integer array to represent the days of the month.
For example
[VBCODE]
Dim trig As New MonthlyTrigger(Me.StartTime.Value.Hour, Me.StartTime.Value.Minute, {1,2,3,4}, MonthsOfTheYear.January)
[/CODE]
I have a form with 31 checkboxes to represent the days of the month and the user can select any or all of them. So I would like to create a sub called, say, GetMonthDays, that would check which checkboxes are checked and then build an integer array from it. However, I'm stuck.
I can use the text value of the checkbox and simply Cint/DirectCast it but how to add it to the integer array? Is there a better way to do this?
Code:
Private Function GetDaysOfTheMonth() As Integer()
Dim dom As Integer() = Nothing
For Each C As Control In gb_MonthsDays.Controls
If TypeOf C Is CheckBox Then
Dim cb As CheckBox = DirectCast(C, CheckBox)
If cb.Checked Then ??????????
End If
Next
Return dom
End Function
Re: [2005] Problem using dynamic integer array
Code:
Dim dom() As Integer = {} 'changed
If cb.Checked Then
Array.Resize(dom, dom.Length + 1)
dom(dom.Length - 1) = Integer.Parse(cb.Text)
End If
'check dom length, if 0 none checked
Re: [2005] Problem using dynamic integer array
Use a list(of integer) to get the checked days, then export that list to an integer array. You can name you checkboxes "checkbox0, checkbox1".... or assign an integer value to the Tag property of each chackbox. When you loop thru the checkboxes, you read back this integer value and add it to your list. When done, call list.ToArray to export the array. This example using the Tag property of the checkboxes. If you serialized the name, you can read the name and use string.substring to get the number.
Code:
Private Function GetDaysOfTheMonth() As Integer()
Dim dom As New List(Of Integer)
For Each C As Control In gb_MonthsDays.Controls
If TypeOf C Is CheckBox Then
Dim cb As CheckBox = DirectCast(C, CheckBox)
If cb.Checked Then
dom.Add(CInt(cb.Tag))
End If
Next
Return dom.ToArray
End Function
Re: [2005] Problem using dynamic integer array
Ah, the ToArray method, that's what I need. I'll use the Text property instead of the Tag property as it's 1, 2, 3 etc for each checkbox
Re: [RESOLVED] [2005] Problem using dynamic integer array
edit - go with list. i was thinking of something else.