Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Problem using dynamic integer array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Resolved [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
    ManagePC - the all-in-one PC management and inventory tool

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    Last edited by dbasnett; May 16th, 2008 at 05:20 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    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
    ManagePC - the all-in-one PC management and inventory tool

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] [2005] Problem using dynamic integer array

    edit - go with list. i was thinking of something else.
    Last edited by dbasnett; May 16th, 2008 at 05:42 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width